Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow user to load more emails #42

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/app/src/app/try/[[...slug]]/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -60,6 +60,7 @@ export function PageContent(props: ContentProps) {
const [externalInputs, setExternalInputs] = useState<Record<string,string>>({});
const [isRedeploying, setIsRedeploying] = useState<boolean>(false);
const [proofLogs, setProofLogs] = useState<Record<string, string>>({});
const [emailPageToken, setEmailPageToken] = useState<string|undefined>("0");

useEffect(() => {
if (!inputWorkers[entry.slug]) {
Expand All @@ -70,15 +71,22 @@ 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);

const processedEmails: Email[] = [];
for (const email of emails) {
processedEmails.push(await mapEmail(email));
}
setMessages(processedEmails)
setMessages([...messages, ...processedEmails])
}
if (googleAuthToken) {
fetchData();
Expand Down Expand Up @@ -190,6 +198,7 @@ export function PageContent(props: ContentProps) {
return <p>No emails found</p>
} else {
return (
<>
<Table>
<TableHeader>
<TableRow>
Expand Down Expand Up @@ -223,6 +232,10 @@ export function PageContent(props: ContentProps) {
))}
</TableBody>
</Table>
{emailPageToken !== undefined && (<div className="flex flex-row justify-center pt-2 pb-4" onClick={() => filterEmails(entry.emailQuery)}>
<RefreshCw className="w-5 h-5 mr-2"/> Load more emails
</div>)}
</>
)
}
}
Expand Down
Loading