From ce20fb78d1d2be221c0bbecc103d410dbbd2682a Mon Sep 17 00:00:00 2001 From: javiersuweijie Date: Wed, 16 Oct 2024 17:08:13 +0800 Subject: [PATCH] feat: allow user to load more emails --- .../app/src/app/try/[[...slug]]/content.tsx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/app/src/app/try/[[...slug]]/content.tsx b/packages/app/src/app/try/[[...slug]]/content.tsx index 8c8b9fe..bd251bd 100644 --- a/packages/app/src/app/try/[[...slug]]/content.tsx +++ b/packages/app/src/app/try/[[...slug]]/content.tsx @@ -5,7 +5,7 @@ import { Entry } from "@prisma/client"; import { CheckedState } from "@radix-ui/react-checkbox"; import { useState, useEffect, FormEvent } from "react"; import { useGoogleAuth, fetchEmailList, fetchEmailsRaw, useZkEmailSDK } from "@zk-email/zk-email-sdk"; -import { Check, LoaderCircle, Trash, X } from 'lucide-react'; +import { Check, LoaderCircle, Trash, X, RefreshCw } from 'lucide-react'; import { Input } from "@/components/ui/input"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; import PostalMime from 'postal-mime'; @@ -60,6 +60,7 @@ export function PageContent(props: ContentProps) { const [externalInputs, setExternalInputs] = useState>({}); const [isRedeploying, setIsRedeploying] = useState(false); const [proofLogs, setProofLogs] = useState>({}); + const [emailPageToken, setEmailPageToken] = useState("0"); useEffect(() => { if (!inputWorkers[entry.slug]) { @@ -70,7 +71,14 @@ export function PageContent(props: ContentProps) { function filterEmails(query: string) { const fetchData = async () => { - const res = await fetchEmailList(googleAuthToken.access_token, { q: query }) + if (emailPageToken === undefined) { + return + } + const res = await fetchEmailList(googleAuthToken.access_token, { q: query, pageToken: emailPageToken, maxResults: 3}) + setEmailPageToken(res.nextPageToken) + if (!res.messages) { + return + } const messageIds = res.messages.map((message: any) => message.id) const emails = await fetchEmailsRaw(googleAuthToken.access_token, messageIds); @@ -78,7 +86,7 @@ export function PageContent(props: ContentProps) { for (const email of emails) { processedEmails.push(await mapEmail(email)); } - setMessages(processedEmails) + setMessages([...messages, ...processedEmails]) } if (googleAuthToken) { fetchData(); @@ -190,6 +198,7 @@ export function PageContent(props: ContentProps) { return

No emails found

} else { return ( + <> @@ -223,6 +232,10 @@ export function PageContent(props: ContentProps) { ))}
+ {emailPageToken !== undefined && (
filterEmails(entry.emailQuery)}> + Load more emails +
)} + ) } }