From 202a0e07d85b080eeb030f7f1ed7d9733fecf113 Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 20:05:02 +0200 Subject: [PATCH] courrier multi account restore compose --- hooks/use-courrier.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/hooks/use-courrier.ts b/hooks/use-courrier.ts index fa65d7f2..c17cec15 100644 --- a/hooks/use-courrier.ts +++ b/hooks/use-courrier.ts @@ -269,14 +269,17 @@ export const useCourrier = () => { }, [currentFolder, page, perPage, session?.user?.id, loadEmails]); // Fetch a single email's content - const fetchEmailContent = useCallback(async (emailId: string) => { + const fetchEmailContent = useCallback(async (emailId: string, accountId?: string, folderOverride?: string) => { try { - const response = await fetch(`/api/courrier/${emailId}?folder=${encodeURIComponent(currentFolder)}`); - + const folderToUse = folderOverride || currentFolder; + const query = new URLSearchParams({ + folder: folderToUse, + }); + if (accountId) query.set('accountId', accountId); + const response = await fetch(`/api/courrier/${emailId}?${query.toString()}`); if (!response.ok) { throw new Error(`Failed to fetch email content: ${response.status}`); } - const data = await response.json(); return data; } catch (error) { @@ -286,21 +289,17 @@ export const useCourrier = () => { }, [currentFolder]); // Select an email to view - const handleEmailSelect = useCallback(async (emailId: string) => { + const handleEmailSelect = useCallback(async (emailId: string, accountId?: string, folderOverride?: string) => { setIsLoading(true); - try { // Find the email in the current list - const email = emails.find(e => e.id === emailId); - + const email = emails.find(e => e.id === emailId && (!accountId || e.accountId === accountId) && (!folderOverride || e.folder === folderOverride)); if (!email) { throw new Error('Email not found'); } - // If content is not fetched, get the full content if (!email.contentFetched) { - const fullEmail = await fetchEmailContent(emailId); - + const fullEmail = await fetchEmailContent(emailId, accountId, folderOverride); // Merge the full content with the email const updatedEmail = { ...email, @@ -308,14 +307,12 @@ export const useCourrier = () => { attachments: fullEmail.attachments, contentFetched: true }; - // Update the email in the list setEmails(emails.map(e => e.id === emailId ? updatedEmail : e)); setSelectedEmail(updatedEmail); } else { setSelectedEmail(email); } - // Mark the email as read if it's not already if (!email.flags.seen) { markEmailAsRead(emailId, true);