From 1a380f973c5662aed4979f86f6fbf673d7515642 Mon Sep 17 00:00:00 2001 From: alma Date: Tue, 29 Apr 2025 10:48:54 +0200 Subject: [PATCH] courrier multi account restore compose --- app/courrier/page.tsx | 94 ++++++++++--------------------------------- 1 file changed, 21 insertions(+), 73 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 4d3f09e3..3dccc8a0 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -642,7 +642,7 @@ export default function CourrierPage() { // Extract the base folder name if it already has an account prefix let baseFolder = folder; - let folderAccountId = accountId; + let folderAccountId = null; if (folder.includes(':')) { const parts = folder.split(':'); @@ -651,71 +651,46 @@ export default function CourrierPage() { logEmailOp('MAILBOX-CHANGE', `Parsed folder: accountId=${folderAccountId}, baseFolder=${baseFolder}`); - // If the prefixed account doesn't match our account, log a warning + // CRITICAL FIX: If the folder has an account prefix that doesn't match the requested account, + // log a warning and ALWAYS use the requested accountId if (folderAccountId !== accountId.toString()) { - logEmailOp('MAILBOX-CHANGE', `WARNING: Folder prefix mismatch`, { + logEmailOp('MAILBOX-CHANGE', `WARNING: Folder prefix mismatch - FIXING`, { folderAccount: folderAccountId, - targetAccount: accountId, - originalFolder: folder + requestedAccount: accountId, + originalFolder: folder, + baseFolder: baseFolder }); + // Force the correct account ID to be used + folderAccountId = accountId.toString(); } } - // Always create a consistent folder name with current account prefix + // ALWAYS create a consistent folder name with REQUESTED account prefix const prefixedFolder = `${accountId}:${baseFolder}`; logEmailOp('MAILBOX-CHANGE', `Normalized folder name: ${prefixedFolder}`); - // Find exact folder match in account's folders - const exactMatch = account.folders?.find(f => - f === prefixedFolder || - f === baseFolder || - (f.includes(':') && f.split(':')[1] === baseFolder) - ); - - if (exactMatch) { - logEmailOp('MAILBOX-CHANGE', `Found exact folder match: ${exactMatch}`); - } else { - logEmailOp('MAILBOX-CHANGE', `No exact folder match found for ${baseFolder}`); - } - - // If no match and not INBOX, show error - if (!exactMatch && !baseFolder.toLowerCase().includes('inbox')) { - logEmailOp('MAILBOX-CHANGE', `ERROR: Folder not found: ${baseFolder}`); - toast({ - title: "Folder not found", - description: `The folder "${baseFolder}" does not exist for this account.`, - variant: "destructive", - }); - setLoading(false); - return; - } - - // Use the exact match if found, otherwise use our constructed prefixedFolder - const folderToUse = exactMatch || prefixedFolder; - logEmailOp('MAILBOX-CHANGE', `Proceeding with folder: ${folderToUse}`); - // 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, - [accountId]: folderToUse + [accountId]: prefixedFolder }; logEmailOp('MAILBOX-CHANGE', `Updated selected folders map`, updated); return updated; }); - // Use the correct changeFolder method to handle the folder change properly - // This ensures that the account ID is properly passed along - changeFolder(folderToUse, accountId) + // Call changeFolder with the prefixed folder name and explicit accountId + // This ensures that both the folder name and accountId parameter are consistent + changeFolder(prefixedFolder, accountId) .then(() => { - logEmailOp('MAILBOX-CHANGE', `Successfully changed to folder ${folderToUse}`); + logEmailOp('MAILBOX-CHANGE', `Successfully changed to folder ${prefixedFolder}`); }) .catch(error => { - logEmailOp('MAILBOX-CHANGE', `ERROR: Failed to change to folder ${folderToUse}`, { error }); + logEmailOp('MAILBOX-CHANGE', `ERROR: Failed to change to folder ${prefixedFolder}`, { error }); toast({ title: "Error changing folders", - description: `Failed to change to folder ${baseFolder}. ${error?.message || ''}`, + description: `Failed to change to folder ${baseFolder}. ${error instanceof Error ? error.message : String(error)}`, variant: "destructive", }); }) @@ -822,37 +797,10 @@ export default function CourrierPage() { setSelectedAccount(account); logEmailOp('ACCOUNT-SELECT', `Selected account set to: ${account.email}`); - // 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 => { - 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 - if (!inboxFolder) { - inboxFolder = account.folders.length > 0 - ? account.folders[0] - : `${account.id}:INBOX`; - logEmailOp('ACCOUNT-SELECT', `No INBOX found, using folder: ${inboxFolder}`); - } else { - logEmailOp('ACCOUNT-SELECT', `Found INBOX folder: ${inboxFolder}`); - } - - // Ensure the folder has the account prefix - if (!inboxFolder.includes(':')) { - inboxFolder = `${account.id}:${inboxFolder}`; - logEmailOp('ACCOUNT-SELECT', `Added account prefix to folder: ${inboxFolder}`); - } + // SIMPLIFIED APPROACH: Instead of trying to find an existing INBOX folder, + // just create a properly formatted one with the current account ID + const inboxFolder = `${account.id}:INBOX`; + logEmailOp('ACCOUNT-SELECT', `Using standardized INBOX folder: ${inboxFolder}`); // Update the selected folders map with the proper account ID and folder setSelectedFolders(prev => {