diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 2fd47fd3..6d4786bf 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -263,24 +263,52 @@ export default function CourrierPage() { updatedAccounts.push(accountWithFolders); }); } else if (data.email) { - // Fallback to single account if allAccounts is not available - console.log(`[DEBUG] Fallback to single account: ${data.email}`); - - // Force include some hardcoded folders if none are present - const fallbackFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; - - // Prioritize mailboxes from IMAP if available - const folderList = (data.mailboxes && data.mailboxes.length > 0) ? - data.mailboxes : fallbackFolders; - - // Create account with mailboxes as folders (using type assertion to avoid TypeScript error) - updatedAccounts.push({ - id: 1, - name: data.email, - email: data.email, - color: 'bg-blue-500', - folders: folderList - } as Account); + // Check if we have accounts from the API + if (data.allAccounts && Array.isArray(data.allAccounts) && data.allAccounts.length === 1) { + const singleAccount = data.allAccounts[0]; + console.log('[DEBUG] Single account detected:', + { id: singleAccount.id, email: singleAccount.email, folders: singleAccount.folders?.length }); + + // Force include some hardcoded folders if none are present + const fallbackFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; + + // Fix: Check for account-specific folders first, then global mailboxes + const accountFolders = (singleAccount.folders && singleAccount.folders.length > 0) ? + singleAccount.folders : + (data.mailboxes && data.mailboxes.length > 0) ? + data.mailboxes : + fallbackFolders; + + console.log(`[DEBUG] Using folders for single account ${singleAccount.email}:`, accountFolders); + + updatedAccounts.push({ + id: singleAccount.id || 1, + name: singleAccount.display_name || singleAccount.email, + email: singleAccount.email, + color: singleAccount.color || 'bg-blue-500', + folders: accountFolders + } as Account); + } else { + // Fallback to single account if allAccounts is not available + console.log(`[DEBUG] Fallback to single account: ${data.email}`); + + // Force include some hardcoded folders if none are present + const fallbackFolders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; + + // Prioritize mailboxes from IMAP if available + const folderList = (data.mailboxes && data.mailboxes.length > 0) ? + data.mailboxes : fallbackFolders; + + console.log(`[DEBUG] Using folders for fallback account ${data.email}:`, folderList); + + updatedAccounts.push({ + id: 1, + name: data.displayName || data.email, + email: data.email, + color: 'bg-blue-500', + folders: folderList + } as Account); + } } console.log('Setting accounts:', updatedAccounts);