diff --git a/app/api/courrier/session/route.ts b/app/api/courrier/session/route.ts index cc6f403c..2b75d11d 100644 --- a/app/api/courrier/session/route.ts +++ b/app/api/courrier/session/route.ts @@ -178,7 +178,7 @@ export async function GET() { return NextResponse.json({ authenticated: true, hasEmailCredentials: true, - accounts: accountsWithFolders + allAccounts: accountsWithFolders }); } catch (error) { console.error('Error in session route:', error); diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 45a2b69f..bdc15f85 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -66,7 +66,7 @@ interface Account { name: string; email: string; color: string; - folders?: string[]; + folders: string[]; } interface EmailWithFlags { @@ -125,7 +125,7 @@ export default function CourrierPage() { // Email accounts for the sidebar const [accounts, setAccounts] = useState([ - { id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }, + { id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500', folders: [] }, { id: 'loading-account', name: 'Loading...', email: '', color: 'bg-blue-500', folders: [] } ]); const [selectedAccount, setSelectedAccount] = useState(null); @@ -196,7 +196,7 @@ export default function CourrierPage() { if (!accounts || accounts.length === 0) { console.warn('Accounts array is empty, restoring defaults'); setAccounts([ - { id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }, + { id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500', folders: mailboxes }, { id: 'loading-account', name: 'Loading...', email: '', color: 'bg-blue-500', folders: mailboxes } ]); } @@ -296,80 +296,27 @@ export default function CourrierPage() { setPrefetchStarted(Boolean(data.prefetchStarted)); // Create a copy of the current accounts to update - const updatedAccounts = [...accounts]; + const updatedAccounts = [{ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500', folders: [] }]; // Check if we have multiple accounts returned if (data.allAccounts && Array.isArray(data.allAccounts) && data.allAccounts.length > 0) { console.log('[DEBUG] Multiple accounts found:', data.allAccounts.length); - // First, validate the structure of each account - data.allAccounts.forEach((account: any, index: number) => { - console.log(`[DEBUG] Account ${index+1} structure check:`, { - id: account.id, - email: account.email, - display_name: account.display_name, - hasFolders: !!account.folders, - foldersIsArray: Array.isArray(account.folders), - foldersCount: Array.isArray(account.folders) ? account.folders.length : 0, - folders: account.folders || [] - }); - }); - - // Keep the All account at position 0 - if (updatedAccounts.length > 0) { - // Replace the loading account with the real one - data.allAccounts.forEach((account: any, index: number) => { - // Ensure folders are valid - const accountFolders = (account.folders && Array.isArray(account.folders)) - ? account.folders - : (data.mailboxes && Array.isArray(data.mailboxes)) - ? data.mailboxes - : ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; - - // If we're updating the first real account (index 0 in API, position 1 in our array) - if (index === 0 && updatedAccounts.length > 1) { - // Update the loading account in place to maintain references - updatedAccounts[1] = { - id: account.id, // Use the real account ID - name: account.display_name || account.email, - email: account.email, - color: account.color || 'bg-blue-500', - folders: accountFolders - }; - console.log(`[DEBUG] Updated loading account to real account: ${account.email} with ID ${account.id}`); - } else if (index > 0) { - // Add additional accounts as new entries - updatedAccounts.push({ - id: account.id || `account-${index}`, - name: account.display_name || account.email, - email: account.email, - color: account.color || 'bg-blue-500', - folders: accountFolders - }); - console.log(`[DEBUG] Added additional account: ${account.email} with ID ${account.id}`); - } - }); - } else { - // Fallback if accounts array is empty for some reason - updatedAccounts.push({ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }); + // Add all accounts from the API response + data.allAccounts.forEach((account: any) => { + const accountFolders = (account.folders && Array.isArray(account.folders)) + ? account.folders + : ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; - // Add all accounts from the API response - data.allAccounts.forEach((account: any) => { - const accountFolders = (account.folders && Array.isArray(account.folders)) - ? account.folders - : (data.mailboxes && Array.isArray(data.mailboxes)) - ? data.mailboxes - : ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk']; - - updatedAccounts.push({ - id: account.id, - name: account.display_name || account.email, - email: account.email, - color: account.color || 'bg-blue-500', - folders: accountFolders - }); + updatedAccounts.push({ + id: account.id, + name: account.display_name || account.email, + email: account.email, + color: account.color || 'bg-blue-500', + folders: accountFolders }); - } + console.log(`[DEBUG] Added account: ${account.email} with ID ${account.id}`); + }); } else { // Fallback to single account if allAccounts is not available console.log(`[DEBUG] Fallback to single account: ${data.email}`); @@ -381,37 +328,15 @@ export default function CourrierPage() { const folderList = (data.mailboxes && data.mailboxes.length > 0) ? data.mailboxes : fallbackFolders; - // Update the loading account if it exists - if (updatedAccounts.length > 1) { - updatedAccounts[1] = { - id: 'default-account', // Use consistent ID - name: data.displayName || data.email, - email: data.email, - color: 'bg-blue-500', - folders: folderList - }; - } else { - // Fallback if accounts array is empty - updatedAccounts.push({ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' }); - updatedAccounts.push({ - id: 'default-account', - name: data.displayName || data.email, - email: data.email, - color: 'bg-blue-500', - folders: folderList - }); - } + updatedAccounts.push({ + id: 'default-account', + name: data.displayName || data.email, + email: data.email, + color: 'bg-blue-500', + folders: folderList + }); } - console.log('Setting accounts:', updatedAccounts); - - // Debug each account's folders - updatedAccounts.forEach((acc: Account, idx: number) => { - console.log(`Account ${idx + 1}: ${acc.name} (${acc.email}) - Folders:`, - acc.folders ? acc.folders.length : 0, - acc.folders || []); - }); - // Update the accounts state setAccounts(updatedAccounts);