Skip to content

Commit

Permalink
Merge pull request #291 from elie222/auto-load-more
Browse files Browse the repository at this point in the history
Auto load more pages when running apply mode
  • Loading branch information
elie222 authored Jan 5, 2025
2 parents 57b0a54 + 06861e1 commit c89b484
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions apps/web/app/(app)/automation/ProcessRules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessagesResponse>((_index, previousPageData) => {
const pageToken = previousPageData?.nextPageToken;
if (previousPageData && !pageToken) return null;
Expand All @@ -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) || [],
Expand Down Expand Up @@ -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();
Expand Down

1 comment on commit c89b484

@vercel
Copy link

@vercel vercel bot commented on c89b484 Jan 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.