courrier multi account restore compose

This commit is contained in:
alma 2025-04-29 11:37:29 +02:00
parent 2fada01eba
commit 43c4bbd0b1

View File

@ -610,10 +610,10 @@ export default function CourrierPage() {
// Update handleMailboxChange to ensure consistent folder naming and prevent race conditions
const handleMailboxChange = (folder: string, accountId?: string) => {
// Enhanced logging to trace the flow
logEmailOp('MAILBOX-CHANGE-V3', `Starting mailbox change to folder=${folder}, accountId=${accountId || 'undefined'}`);
logEmailOp('MAILBOX-CHANGE-V4', `Starting mailbox change to folder=${folder}, accountId=${accountId || 'undefined'}`);
if (!accountId || accountId === 'loading-account') {
logEmailOp('MAILBOX-CHANGE-V3', 'No valid accountId provided, using default flow', { folder });
logEmailOp('MAILBOX-CHANGE-V4', 'No valid accountId provided, using default flow', { folder });
changeFolder(folder);
return;
}
@ -621,12 +621,12 @@ export default function CourrierPage() {
// Set loading state immediately
setLoading(true);
// Clear emails during transition
// Clear emails during transition to avoid UI flicker/confusion
setEmails([]);
const account = accounts.find(a => a.id.toString() === accountId.toString());
if (!account) {
logEmailOp('MAILBOX-CHANGE-V3', `ERROR: Account not found: ${accountId}`);
logEmailOp('MAILBOX-CHANGE-V4', `ERROR: Account not found: ${accountId}`);
toast({
title: "Account not found",
description: `The account ${accountId} could not be found.`,
@ -636,7 +636,7 @@ export default function CourrierPage() {
return;
}
logEmailOp('MAILBOX-CHANGE-V3', `Found account: ${account.email}`, {
logEmailOp('MAILBOX-CHANGE-V4', `Found account: ${account.email}`, {
folderCount: account.folders?.length || 0,
folder: folder
});
@ -650,12 +650,12 @@ export default function CourrierPage() {
folderAccountId = parts[0];
baseFolder = parts[1];
logEmailOp('MAILBOX-CHANGE-V3', `Parsed folder: accountId=${folderAccountId}, baseFolder=${baseFolder}`);
logEmailOp('MAILBOX-CHANGE-V4', `Parsed folder: accountId=${folderAccountId}, baseFolder=${baseFolder}`);
// 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-V3', `WARNING: Folder prefix mismatch - FIXING`, {
logEmailOp('MAILBOX-CHANGE-V4', `WARNING: Folder prefix mismatch - FIXING`, {
folderAccount: folderAccountId,
requestedAccount: accountId,
originalFolder: folder,
@ -668,28 +668,30 @@ export default function CourrierPage() {
// ALWAYS create a consistent folder name with REQUESTED account prefix
const prefixedFolder = `${accountId}:${baseFolder}`;
logEmailOp('MAILBOX-CHANGE-V3', `Normalized folder name: ${prefixedFolder}`);
logEmailOp('MAILBOX-CHANGE-V4', `Normalized folder name: ${prefixedFolder}`);
// Check if we're already on this folder to avoid unnecessary refreshes
if (currentFolder === prefixedFolder) {
logEmailOp('MAILBOX-CHANGE-V3', `Already on folder ${prefixedFolder}, skipping change`);
logEmailOp('MAILBOX-CHANGE-V4', `Already on folder ${prefixedFolder}, skipping change`);
setLoading(false);
return;
}
// 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
setSelectedAccount(account);
setSelectedFolders(prev => {
const updated = {
...prev,
[accountId]: prefixedFolder
};
logEmailOp('MAILBOX-CHANGE-V3', `Updated selected folders map`, updated);
logEmailOp('MAILBOX-CHANGE-V4', `Updated selected folders map`, updated);
return updated;
});
// EXTRA SAFETY: Log exactly what we're passing to changeFolder
logEmailOp('MAILBOX-CHANGE-V3', `CALLING changeFolder with EXACT PARAMS:`, {
logEmailOp('MAILBOX-CHANGE-V4', `CALLING changeFolder with EXACT PARAMS:`, {
folder: prefixedFolder,
accountId: accountId,
folderType: typeof prefixedFolder,
@ -699,10 +701,10 @@ export default function CourrierPage() {
// Now use the changeFolder function from the hook with our properly formatted folder name
changeFolder(prefixedFolder, accountId)
.then(() => {
logEmailOp('MAILBOX-CHANGE-V3', `Successfully changed to folder ${prefixedFolder}`);
logEmailOp('MAILBOX-CHANGE-V4', `Successfully changed to folder ${prefixedFolder}`);
})
.catch(error => {
logEmailOp('MAILBOX-CHANGE-V3', `ERROR: Failed to change to folder ${prefixedFolder}`, { error });
logEmailOp('MAILBOX-CHANGE-V4', `ERROR: Failed to change to folder ${prefixedFolder}`, { error });
toast({
title: "Error changing folders",
description: `Failed to change to folder ${baseFolder}. ${error instanceof Error ? error.message : String(error)}`,
@ -802,6 +804,12 @@ export default function CourrierPage() {
folderCount: account.folders?.length || 0
});
// Skip if this is already the selected account
if (selectedAccount?.id === account.id) {
logEmailOp('ACCOUNT-SELECT', `Account ${account.id} already selected, skipping`);
return;
}
// First set loading state to provide visual feedback
setLoading(true);
@ -830,14 +838,13 @@ export default function CourrierPage() {
// 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} for account ${account.id}`);
// Use handleMailboxChange which properly passes the account ID with the folder
handleMailboxChange(inboxFolder, account.id);
setLoading(false);
}, 100);
// CRITICAL FIX: Use the immediate mode of handleMailboxChange - no timeout needed
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.toString());
// We'll let handleMailboxChange manage the loading state, don't reset it here
};
const handleAddAccount = async (accountData: AccountData) => {