From 06861e1898cf0e1a3d84d9804778e29edc15f864 Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Sun, 5 Jan 2025 15:38:03 +0200 Subject: [PATCH] Auto load more pages when running apply mode --- .../web/app/(app)/automation/ProcessRules.tsx | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/apps/web/app/(app)/automation/ProcessRules.tsx b/apps/web/app/(app)/automation/ProcessRules.tsx index 69518939..192e106e 100644 --- a/apps/web/app/(app)/automation/ProcessRules.tsx +++ b/apps/web/app/(app)/automation/ProcessRules.tsx @@ -48,7 +48,7 @@ export function ProcessRulesContent({ testMode }: { testMode: boolean }) { parseAsBoolean.withDefault(false), ); - const { data, isLoading, isValidating, error, setSize } = + const { data, isLoading, isValidating, error, setSize, mutate } = useSWRInfinite((_index, previousPageData) => { const pageToken = previousPageData?.nextPageToken; if (previousPageData && !pageToken) return null; @@ -60,9 +60,7 @@ export function ProcessRulesContent({ testMode }: { testMode: boolean }) { return `/api/google/messages${paramsString ? `?${paramsString}` : ""}`; }); - const onLoadMore = () => { - setSize((size) => size + 1); - }; + const onLoadMore = () => setSize((size) => size + 1); const messages = useMemo( () => data?.flatMap((page) => page.messages) || [], @@ -113,10 +111,27 @@ export function ProcessRulesContent({ testMode }: { testMode: boolean }) { const handleRunAll = async () => { handleStart(); - for (const message of messages) { - if (!isRunningAllRef.current) break; - if (results[message.id]) continue; - await onRun(message); + const PAGE_LIMIT = testMode ? 1 : 10; + + for (let page = 0; page < PAGE_LIMIT; page++) { + // Get current data, only fetch if we don't have this page yet + let currentData = data; + if (!currentData?.[page]) { + await setSize((size) => size + 1); + currentData = await mutate(); + } + + const currentBatch = currentData?.[page]?.messages || []; + + for (const message of currentBatch) { + if (!isRunningAllRef.current) break; + if (results[message.id]) continue; + await onRun(message); + } + + // Check if we got new data in the last request + const lastPage = currentData?.[page]; + if (!lastPage?.nextPageToken || !isRunningAllRef.current) break; } handleStop();