courrier multi account restore compose

This commit is contained in:
alma 2025-04-29 10:58:30 +02:00
parent 686ea6cb53
commit 8b1a160c21
2 changed files with 29 additions and 17 deletions

View File

@ -610,10 +610,10 @@ export default function CourrierPage() {
// Update handleMailboxChange to ensure consistent folder naming and prevent race conditions // Update handleMailboxChange to ensure consistent folder naming and prevent race conditions
const handleMailboxChange = (folder: string, accountId?: string) => { const handleMailboxChange = (folder: string, accountId?: string) => {
// Enhanced logging to trace the flow // Enhanced logging to trace the flow
logEmailOp('MAILBOX-CHANGE-V2', `Starting mailbox change to folder=${folder}, accountId=${accountId || 'undefined'}`); logEmailOp('MAILBOX-CHANGE-V3', `Starting mailbox change to folder=${folder}, accountId=${accountId || 'undefined'}`);
if (!accountId || accountId === 'loading-account') { if (!accountId || accountId === 'loading-account') {
logEmailOp('MAILBOX-CHANGE-V2', 'No valid accountId provided, using default flow', { folder }); logEmailOp('MAILBOX-CHANGE-V3', 'No valid accountId provided, using default flow', { folder });
changeFolder(folder); changeFolder(folder);
return; return;
} }
@ -626,7 +626,7 @@ export default function CourrierPage() {
const account = accounts.find(a => a.id.toString() === accountId.toString()); const account = accounts.find(a => a.id.toString() === accountId.toString());
if (!account) { if (!account) {
logEmailOp('MAILBOX-CHANGE-V2', `ERROR: Account not found: ${accountId}`); logEmailOp('MAILBOX-CHANGE-V3', `ERROR: Account not found: ${accountId}`);
toast({ toast({
title: "Account not found", title: "Account not found",
description: `The account ${accountId} could not be found.`, description: `The account ${accountId} could not be found.`,
@ -636,7 +636,7 @@ export default function CourrierPage() {
return; return;
} }
logEmailOp('MAILBOX-CHANGE-V2', `Found account: ${account.email}`, { logEmailOp('MAILBOX-CHANGE-V3', `Found account: ${account.email}`, {
folderCount: account.folders?.length || 0, folderCount: account.folders?.length || 0,
folder: folder folder: folder
}); });
@ -650,12 +650,12 @@ export default function CourrierPage() {
folderAccountId = parts[0]; folderAccountId = parts[0];
baseFolder = parts[1]; baseFolder = parts[1];
logEmailOp('MAILBOX-CHANGE-V2', `Parsed folder: accountId=${folderAccountId}, baseFolder=${baseFolder}`); logEmailOp('MAILBOX-CHANGE-V3', `Parsed folder: accountId=${folderAccountId}, baseFolder=${baseFolder}`);
// CRITICAL FIX: If the folder has an account prefix that doesn't match the requested account, // 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 // log a warning and ALWAYS use the requested accountId
if (folderAccountId !== accountId.toString()) { if (folderAccountId !== accountId.toString()) {
logEmailOp('MAILBOX-CHANGE-V2', `WARNING: Folder prefix mismatch - FIXING`, { logEmailOp('MAILBOX-CHANGE-V3', `WARNING: Folder prefix mismatch - FIXING`, {
folderAccount: folderAccountId, folderAccount: folderAccountId,
requestedAccount: accountId, requestedAccount: accountId,
originalFolder: folder, originalFolder: folder,
@ -668,11 +668,11 @@ export default function CourrierPage() {
// ALWAYS create a consistent folder name with REQUESTED account prefix // ALWAYS create a consistent folder name with REQUESTED account prefix
const prefixedFolder = `${accountId}:${baseFolder}`; const prefixedFolder = `${accountId}:${baseFolder}`;
logEmailOp('MAILBOX-CHANGE-V2', `Normalized folder name: ${prefixedFolder}`); logEmailOp('MAILBOX-CHANGE-V3', `Normalized folder name: ${prefixedFolder}`);
// Check if we're already on this folder to avoid unnecessary refreshes // Check if we're already on this folder to avoid unnecessary refreshes
if (currentFolder === prefixedFolder) { if (currentFolder === prefixedFolder) {
logEmailOp('MAILBOX-CHANGE-V2', `Already on folder ${prefixedFolder}, skipping change`); logEmailOp('MAILBOX-CHANGE-V3', `Already on folder ${prefixedFolder}, skipping change`);
setLoading(false); setLoading(false);
return; return;
} }
@ -684,17 +684,25 @@ export default function CourrierPage() {
...prev, ...prev,
[accountId]: prefixedFolder [accountId]: prefixedFolder
}; };
logEmailOp('MAILBOX-CHANGE-V2', `Updated selected folders map`, updated); logEmailOp('MAILBOX-CHANGE-V3', `Updated selected folders map`, updated);
return updated; return updated;
}); });
// EXTRA SAFETY: Log exactly what we're passing to changeFolder
logEmailOp('MAILBOX-CHANGE-V3', `CALLING changeFolder with EXACT PARAMS:`, {
folder: prefixedFolder,
accountId: accountId,
folderType: typeof prefixedFolder,
accountIdType: typeof accountId
});
// Now use the changeFolder function from the hook with our properly formatted folder name // Now use the changeFolder function from the hook with our properly formatted folder name
changeFolder(prefixedFolder, accountId) changeFolder(prefixedFolder, accountId)
.then(() => { .then(() => {
logEmailOp('MAILBOX-CHANGE-V2', `Successfully changed to folder ${prefixedFolder}`); logEmailOp('MAILBOX-CHANGE-V3', `Successfully changed to folder ${prefixedFolder}`);
}) })
.catch(error => { .catch(error => {
logEmailOp('MAILBOX-CHANGE-V2', `ERROR: Failed to change to folder ${prefixedFolder}`, { error }); logEmailOp('MAILBOX-CHANGE-V3', `ERROR: Failed to change to folder ${prefixedFolder}`, { error });
toast({ toast({
title: "Error changing folders", title: "Error changing folders",
description: `Failed to change to folder ${baseFolder}. ${error instanceof Error ? error.message : String(error)}`, description: `Failed to change to folder ${baseFolder}. ${error instanceof Error ? error.message : String(error)}`,

View File

@ -109,13 +109,17 @@ export const useCourrier = () => {
// Try to get cached emails first // Try to get cached emails first
const currentRequestPage = page; const currentRequestPage = page;
// CRITICAL FIX: Use the correct parameter order
// Signature is: (userId: string, folder: string, page: number, perPage: number, timeoutMs: number = 100, accountId?: string)
console.log(`Getting cached emails for ${currentFolder} with accountId ${accountId || 'default'}`);
const cachedEmails = await getCachedEmailsWithTimeout( const cachedEmails = await getCachedEmailsWithTimeout(
session.user.id, session.user.id, // userId: string
accountId || 'default', currentFolder, // folder: string
currentFolder, currentRequestPage, // page: number
currentRequestPage, perPage, // perPage: number
perPage, 100, // timeoutMs: number
accountId && accountId !== 'all-accounts' && accountId !== 'loading-account' ? accountId : undefined accountId // accountId?: string
); );
if (cachedEmails) { if (cachedEmails) {