From 4e7bdb163144e39ec2125a5f64834d76769cf4f0 Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 29 Apr 2025 10:45:56 +0200 Subject: [PATCH] courrier multi account restore compose --- app/courrier/page.tsx | 74 +++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 5b012ff9..4d3f09e3 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -679,7 +679,7 @@ export default function CourrierPage() { } // If no match and not INBOX, show error - if (!exactMatch && !baseFolder.includes('INBOX') && baseFolder !== 'INBOX') { + if (!exactMatch && !baseFolder.toLowerCase().includes('inbox')) { logEmailOp('MAILBOX-CHANGE', `ERROR: Folder not found: ${baseFolder}`); toast({ title: "Folder not found", @@ -694,10 +694,8 @@ export default function CourrierPage() { const folderToUse = exactMatch || prefixedFolder; logEmailOp('MAILBOX-CHANGE', `Proceeding with folder: ${folderToUse}`); - // Update UI state - setSelectedAccount(account); - - // Update the selected folders map with the normalized folder name + // CRITICAL FIX: Lock in the selected folder BEFORE changing it in the system + // This prevents any race conditions where the folder gets reset to INBOX setSelectedFolders(prev => { const updated = { ...prev, @@ -707,15 +705,23 @@ export default function CourrierPage() { return updated; }); - // Use a deliberate delay before changing folder to ensure state is updated - logEmailOp('MAILBOX-CHANGE', `Starting folder change after delay...`); - - // Reduce timeout to improve responsiveness - setTimeout(() => { - logEmailOp('MAILBOX-CHANGE', `Loading emails for folder: ${folderToUse}, account: ${accountId}`); - changeFolder(folderToUse, accountId); - setLoading(false); - }, 150); // Reduced from 300ms to 150ms for better responsiveness + // Use the correct changeFolder method to handle the folder change properly + // This ensures that the account ID is properly passed along + changeFolder(folderToUse, accountId) + .then(() => { + logEmailOp('MAILBOX-CHANGE', `Successfully changed to folder ${folderToUse}`); + }) + .catch(error => { + logEmailOp('MAILBOX-CHANGE', `ERROR: Failed to change to folder ${folderToUse}`, { error }); + toast({ + title: "Error changing folders", + description: `Failed to change to folder ${baseFolder}. ${error?.message || ''}`, + variant: "destructive", + }); + }) + .finally(() => { + setLoading(false); + }); }; // Update the folder button rendering to show selected state based on account @@ -816,15 +822,20 @@ export default function CourrierPage() { setSelectedAccount(account); logEmailOp('ACCOUNT-SELECT', `Selected account set to: ${account.email}`); - // Initially hide folders during transition - setShowFolders(false); - // Find an INBOX folder in this account's folders // Look for exact INBOX or accountId:INBOX pattern const inboxPatterns = ['INBOX', ':INBOX', 'inbox', 'Inbox']; let inboxFolder = account.folders.find(f => - inboxPatterns.some(pattern => f.includes(pattern)) + inboxPatterns.some(pattern => { + const hasPattern = f.includes(pattern); + // For prefixed folders, make sure it's for the current account + if (f.includes(':')) { + const [folderId] = f.split(':'); + return hasPattern && folderId === account.id; + } + return hasPattern; + }) ); // If no inbox found, use the first folder or create a default inbox path @@ -843,32 +854,27 @@ export default function CourrierPage() { logEmailOp('ACCOUNT-SELECT', `Added account prefix to folder: ${inboxFolder}`); } - // We don't need to manually set currentFolder as it's managed by changeFolder - // Instead, just log that we'll update it through the proper function - logEmailOp('ACCOUNT-SELECT', `Will update current folder to: ${inboxFolder} through folder change`); - - // Create a streamlined account selection sequence - // Step 1: Show folders immediately for better feedback - setShowFolders(true); - logEmailOp('ACCOUNT-SELECT', `Folders shown for account: ${account.email}`); - - // Step 2: Update the selected folders map immediately + // Update the selected folders map with the proper account ID and folder setSelectedFolders(prev => { const updated = { ...prev, - [account.id.toString()]: inboxFolder + [account.id]: inboxFolder }; logEmailOp('ACCOUNT-SELECT', `Updated selected folders map`, updated); return updated; }); - // Step 3: Wait a small delay and then load emails - logEmailOp('ACCOUNT-SELECT', `Starting folder change after short delay`); + // First, show the folders for better UX + setShowFolders(true); + + // Use a slight delay to ensure state updates have propagated before changing folder setTimeout(() => { - logEmailOp('ACCOUNT-SELECT', `Triggering folder change to: ${inboxFolder}`); - handleMailboxChange(inboxFolder, account.id.toString()); + logEmailOp('ACCOUNT-SELECT', `Triggering folder change to: ${inboxFolder} for account ${account.id}`); + + // Use handleMailboxChange which properly passes the account ID with the folder + handleMailboxChange(inboxFolder, account.id); setLoading(false); - }, 100); // Reduced delay for better responsiveness + }, 100); }; const handleAddAccount = async (accountData: AccountData) => {