Skip to content

Commit 7a729e1

Browse files
committed
add max fetches
1 parent f102e9b commit 7a729e1

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

ui/src/hooks/queries/use-infinite-entries.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import type { Entry } from '~/types/bindings';
99
import { findEntryItem } from '~/utils/entries';
1010
import { useQueryState } from '../../contexts/query-state-context';
1111

12+
/**
13+
* How many times are we willing to fetch more pages in order to populate the entry list up to the currently-viewed entry?
14+
*/
15+
const MAX_FETCHES = 25;
16+
1217
export const useInfiniteEntries = () => {
1318
const state = useQueryState();
1419
const viewport = useViewport();
@@ -18,10 +23,10 @@ export const useInfiniteEntries = () => {
1823
queryFn: fetchParams =>
1924
getEntries({
2025
feed: state.params.feed_uuid,
21-
view: state.getView(),
22-
sort: state.getSort(),
2326
folder: state.params.folder_slug,
2427
cursor: fetchParams.pageParam as undefined | string,
28+
view: state.getView(),
29+
sort: state.getSort(),
2530
}),
2631
getNextPageParam: last => last.next_cursor,
2732
initialPageParam: undefined,
@@ -31,37 +36,39 @@ export const useInfiniteEntries = () => {
3136

3237
const allEntries = () => query.data?.pages.flatMap(page => page.data) || [];
3338

34-
const fetchMore = leading(
35-
debounce,
36-
() => {
37-
// Only fetch more if we have more to fetch and we're not already fetching
38-
if (!query.hasNextPage || query.isFetchingNextPage) return;
39+
const canFetchMore = () => query.hasNextPage && !query.isFetchingNextPage;
3940

40-
query.fetchNextPage();
41-
},
42-
100,
43-
);
41+
// Only fetch more if we have more to fetch and we're not already fetching
42+
const fetchMore = leading(debounce, () => canFetchMore() && query.fetchNextPage(), 100);
43+
44+
const getNextCursor = () => query.data?.pages[query.data?.pages.length - 1].next_cursor;
4445

4546
const [init, setInit] = createSignal(false);
47+
const [fetches, setFetches] = createSignal(0);
48+
const [lastCursor, setLastCursor] = createSignal(getNextCursor());
4649

4750
// All the entries up until the current entry (and maybe fetch more on mobile)
4851
createEffect(() => {
49-
if (init() || !state.params.entry_uuid || !query.hasNextPage) return;
52+
if (init() || !state.params.entry_uuid || getNextCursor() === lastCursor() || !canFetchMore()) return;
53+
54+
setTimeout(() => {
55+
const activeItem = findEntryItem(state.params.entry_uuid);
56+
if (activeItem) activeItem.scrollIntoView({ behavior: 'smooth', block: 'center' });
57+
}, 500);
5058

5159
const uuids = allEntries().map(entry => entry.uuid);
5260

5361
const entryIsLoaded = uuids.includes(state.params.entry_uuid);
5462
const viewingLastMobile = viewport.lte('md') ? uuids[uuids.length - 1] === state.params.entry_uuid : false;
5563

5664
// If the entry is not listed or if we're viewing the last item on mobile, fetch more entries
57-
if (!entryIsLoaded || viewingLastMobile) {
65+
if ((!entryIsLoaded && fetches() < MAX_FETCHES) || viewingLastMobile) {
5866
query.fetchNextPage();
67+
setFetches(val => val + 1);
68+
setLastCursor(getNextCursor());
5969
return;
6070
}
6171

62-
const activeItem = findEntryItem(state.params.entry_uuid);
63-
if (activeItem) activeItem.focus();
64-
6572
setInit(true);
6673
});
6774

0 commit comments

Comments
 (0)