From 338f3bca78b7c3a9e250cb9845cba239fb21c7dc Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 14 Aug 2024 09:48:56 +0800 Subject: [PATCH] WIP: infinite scroll on the library with react-query --- packages/web/lib/hooks/useFetchMoreScroll.tsx | 16 +++++--- .../library_items/useLibraryItems.tsx | 39 +++++++++++-------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/packages/web/lib/hooks/useFetchMoreScroll.tsx b/packages/web/lib/hooks/useFetchMoreScroll.tsx index f454645e0..eb7a1759f 100644 --- a/packages/web/lib/hooks/useFetchMoreScroll.tsx +++ b/packages/web/lib/hooks/useFetchMoreScroll.tsx @@ -10,13 +10,17 @@ export const useFetchMore = (callback: () => void, delay = 500): void => { } const callbackInternal = (): void => { - const { - scrollTop, - scrollHeight, - clientHeight - } = window.document.documentElement; + const { scrollTop, scrollHeight, clientHeight } = + window.document.documentElement - if (scrollTop + clientHeight >= scrollHeight - (scrollHeight / 3)) { + if (scrollTop + clientHeight >= scrollHeight - scrollHeight / 3) { + console.log( + 'calling fetchMore: scrollTop + clientHeight >= scrollHeight - scrollHeight / 3', + scrollTop, + clientHeight, + scrollHeight, + scrollHeight / 3 + ) callback() } throttleTimeout.current = undefined diff --git a/packages/web/lib/networking/library_items/useLibraryItems.tsx b/packages/web/lib/networking/library_items/useLibraryItems.tsx index 27d2762db..b193cf827 100644 --- a/packages/web/lib/networking/library_items/useLibraryItems.tsx +++ b/packages/web/lib/networking/library_items/useLibraryItems.tsx @@ -537,7 +537,7 @@ export function useRefreshProcessingItems() { includeContent: false, })) as LibraryItemsData if (result.search.errorCodes?.length) { - throw new Error(result.search.errorCodes[0]) + return undefined } return result.search } @@ -546,33 +546,38 @@ export function useRefreshProcessingItems() { retry: 3, retryDelay: 10, onSuccess: async ( - data: LibraryItems, + data: LibraryItems | undefined, variables: { attempt: number itemIds: string[] } ) => { - let shouldRefetch = false - console.log('got processing items: ', data.edges) - for (const item of data.edges) { - if (item.node.state !== State.PROCESSING) { - overwriteItemPropertiesInCache( - queryClient, - item.node.id, - undefined, - item.node - ) - } else { - shouldRefetch = true + let shouldRefetch = data == undefined + if (data) { + for (const item of data.edges) { + if (item.node.state !== State.PROCESSING) { + overwriteItemPropertiesInCache( + queryClient, + item.node.id, + undefined, + item.node + ) + } else { + shouldRefetch = true + } } + } else { + shouldRefetch = true } if (shouldRefetch && variables.attempt < maxAttempts) { await delay(5000 * variables.attempt + 1) mutation.mutate({ attempt: variables.attempt + 1, - itemIds: data.edges - .filter((item) => item.node.state == State.PROCESSING) - .map((it) => it.node.id), + itemIds: data + ? data.edges + .filter((item) => item.node.state == State.PROCESSING) + .map((it) => it.node.id) + : variables.itemIds, }) } },