diff --git a/packages/messages/src/hooks/__tests__/useNotificationsListQuery.test.ts b/packages/messages/src/hooks/__tests__/useNotificationsListQuery.test.ts index c2fc954b9..00b46ce15 100644 --- a/packages/messages/src/hooks/__tests__/useNotificationsListQuery.test.ts +++ b/packages/messages/src/hooks/__tests__/useNotificationsListQuery.test.ts @@ -122,4 +122,35 @@ describe('useNotificationsListQuery', () => { expect(fetchNotificationList).not.toHaveBeenCalled() }) + + it('passes only the cursor query param (not the full next URL) when fetching the next page', async () => { + const nextCursor = 'cD0xMjM0NTY3' + vi.mocked(fetchNotificationList).mockResolvedValueOnce({ + count: 1, + next: `https://mobile-api.perawallet.app/v2/devices/test-device-id/notifications/?cursor=${nextCursor}&page_size=20`, + previous: null, + results: [], + }) + + const { result } = renderHook(() => useNotificationsListQuery(), { + wrapper: createWrapper(), + }) + + await waitFor(() => expect(result.current.isSuccess).toBe(true)) + + vi.mocked(fetchNotificationList).mockResolvedValueOnce({ + count: 0, + next: null, + previous: null, + results: [], + }) + + await result.current.fetchNextPage() + + expect(fetchNotificationList).toHaveBeenLastCalledWith( + 'test-network', + 'test-device-id', + nextCursor, + ) + }) }) diff --git a/packages/messages/src/hooks/useNotificationsListQuery.ts b/packages/messages/src/hooks/useNotificationsListQuery.ts index 35f4ba7c6..377d2206b 100644 --- a/packages/messages/src/hooks/useNotificationsListQuery.ts +++ b/packages/messages/src/hooks/useNotificationsListQuery.ts @@ -21,6 +21,7 @@ import { } from '../api/notifications' import type { PeraNotification } from '../models' import { getNotificationsListQueryKey } from './querykeys' +import { Maybe, Nullable } from '@perawallet/wallet-core-shared' const mapNotificationResponseToNotification = ( response: NotificationResponse, @@ -34,6 +35,15 @@ const mapNotificationResponseToNotification = ( icon: response.icon ?? null, }) +const extractCursor = (url: Nullable): Maybe => { + if (!url) return undefined + try { + return new URL(url).searchParams.get('cursor') ?? undefined + } catch { + return undefined + } +} + export const useNotificationsListQuery = () => { const { network } = useNetwork() const deviceID = useDeviceID(network) @@ -47,12 +57,8 @@ export const useNotificationsListQuery = () => { pageParam as string | undefined, ), initialPageParam: '', - getNextPageParam: lastPage => { - return lastPage.next - }, - getPreviousPageParam: firstPage => { - return firstPage.previous - }, + getNextPageParam: lastPage => extractCursor(lastPage.next), + getPreviousPageParam: firstPage => extractCursor(firstPage.previous), enabled: !!deviceID?.length, select: useCallback((data: InfiniteData) => { return data.pages.flatMap((p: NotificationsListResponse) =>