diff --git a/src/state/queries/notifications/grouped/__tests__/cursor.test.ts b/src/state/queries/notifications/grouped/__tests__/cursor.test.ts new file mode 100644 index 000000000..b9e2c8b36 --- /dev/null +++ b/src/state/queries/notifications/grouped/__tests__/cursor.test.ts @@ -0,0 +1,44 @@ +import {describe, expect, it} from '@jest/globals' + +import {getNextCursor} from '#/state/queries/notifications/grouped/cursor' +import {type NotificationView} from '#/state/queries/notifications/grouped/types' + +// Only the length of a page's notifications matters here +const SOME = [{}] as NotificationView[] +const NONE: NotificationView[] = [] + +function pages(...notifications: NotificationView[][]) { + return notifications.map((page, index) => ({ + cursor: `cursor-${index}`, + notifications: page, + })) +} + +describe('getNextCursor', () => { + it('continues from the last page', () => { + expect(getNextCursor(pages(SOME, SOME))).toBe('cursor-1') + }) + + it('continues through a few empty pages', () => { + expect(getNextCursor(pages(SOME, NONE, NONE, NONE, NONE))).toBe('cursor-4') + }) + + it('gives up after five empty pages in a row', () => { + expect(getNextCursor(pages(SOME, NONE, NONE, NONE, NONE, NONE))).toBe( + undefined, + ) + expect(getNextCursor(pages(NONE, NONE, NONE, NONE, NONE))).toBe(undefined) + }) + + it('starts counting again after a page with notifications', () => { + expect(getNextCursor(pages(NONE, NONE, NONE, NONE, SOME, NONE, NONE))).toBe( + 'cursor-6', + ) + }) + + it('ends when the server sends no cursor', () => { + expect(getNextCursor([{cursor: undefined, notifications: SOME}])).toBe( + undefined, + ) + }) +}) diff --git a/src/state/queries/notifications/grouped/cursor.ts b/src/state/queries/notifications/grouped/cursor.ts new file mode 100644 index 000000000..de93e0485 --- /dev/null +++ b/src/state/queries/notifications/grouped/cursor.ts @@ -0,0 +1,27 @@ +import {type GroupedNotificationsPage} from './types' + +/** + * How many pages in a row can come back empty before the feed is treated as + * finished, as `useAutoPagination` allows. + */ +const MAX_EMPTY_PAGES = 5 + +/** + * The cursor for the page after `pages`, or undefined once the feed is done. + * + * The server can keep returning a cursor for pages whose groups all get + * dropped while hydrating. On web the end of the list stays in view, so it + * would ask for page after page forever. Giving up after a run of empty pages + * ends it, and the screen shows the end of the feed, or its empty state. + */ +export function getNextCursor( + pages: Pick[], +) { + let emptyPages = 0 + for (let i = pages.length - 1; i >= 0; i--) { + if (pages[i].notifications.length > 0) break + emptyPages++ + } + if (emptyPages >= MAX_EMPTY_PAGES) return undefined + return pages.at(-1)?.cursor +} diff --git a/src/state/queries/notifications/grouped/index.ts b/src/state/queries/notifications/grouped/index.ts index 7b4b57e02..a6c097490 100644 --- a/src/state/queries/notifications/grouped/index.ts +++ b/src/state/queries/notifications/grouped/index.ts @@ -7,6 +7,7 @@ import { import {useModerationOpts} from '#/state/preferences/moderation-opts' import {STALE} from '#/state/queries' +import {getNextCursor} from '#/state/queries/notifications/grouped/cursor' import { createGroupActorsPage, getNextActorDids, @@ -153,7 +154,7 @@ export function useGroupedNotificationsQuery({ return {...hydratePage(res, {viewerDid}), requestedAt} }, initialPageParam: undefined as string | undefined, - getNextPageParam: lastPage => lastPage.cursor, + getNextPageParam: (_lastPage, pages) => getNextCursor(pages), select(data) { return selectNotifications(data, { moderationOpts,