Request pages until we receive more items

This commit is contained in:
Dan Abramov
2024-07-31 16:25:51 +01:00
parent c75bb65bef
commit c0c1572719
2 changed files with 10 additions and 28 deletions
+5 -14
View File
@@ -59,7 +59,6 @@ export function useNotificationFeedQuery(opts?: {
const moderationOpts = useModerationOpts()
const unreads = useUnreadNotificationsApi()
const enabled = opts?.enabled !== false
const lastPageCountRef = useRef(0)
const gate = useGate()
// false: force showing all notifications
@@ -121,29 +120,21 @@ export function useNotificationFeedQuery(opts?: {
},
})
const lastItemsCountRef = useRef(0)
useEffect(() => {
const {isFetching, hasNextPage, data} = query
if (isFetching || !hasNextPage) {
return
}
// avoid double-fires of fetchNextPage()
if (
lastPageCountRef.current !== 0 &&
lastPageCountRef.current === data?.pages?.length
) {
return
}
// fetch next page if we haven't gotten a full page of content
let count = 0
for (const page of data?.pages || []) {
count += page.items.length
}
if (count < PAGE_SIZE && (data?.pages.length || 0) < 6) {
query.fetchNextPage()
lastPageCountRef.current = data?.pages?.length || 0
if (count > lastItemsCountRef.current) {
lastItemsCountRef.current = count
return
}
query.fetchNextPage()
}, [query])
return query