diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 431184a9..afedf3d2 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -77,30 +77,6 @@ interface EmailWithFlags { }; } -interface EmailMessage { - id: string; - from: { name: string; address: string }[]; - to: { name: string; address: string }[]; - subject: string; - date: Date; - flags: { - seen: boolean; - flagged: boolean; - answered: boolean; - draft: boolean; - deleted: boolean; - }; - size: number; - hasAttachments: boolean; - folder: string; - contentFetched: boolean; - accountId: string; - content: { - text: string; - html: string; - }; -} - export default function CourrierPage() { const router = useRouter(); const { data: session } = useSession(); @@ -157,30 +133,27 @@ export default function CourrierPage() { // Track expanded folders for each account const [expandedAccounts, setExpandedAccounts] = useState>({}); - // Track selected folder per account + // Add state to track selected folder per account const [selectedFolders, setSelectedFolders] = useState>({}); - // Track folder visibility per account - const [visibleFolders, setVisibleFolders] = useState>({}); - - // Update account folders when mailboxes change + // Update account folders when mailboxes change - update this to maintain account IDs useEffect(() => { + console.log('Mailboxes updated:', mailboxes); setAccounts(prev => { const updated = [...prev]; - const accountIndex = updated.findIndex(acc => acc.id === selectedAccount?.id); - if (accountIndex !== -1) { - updated[accountIndex] = { - ...updated[accountIndex], - folders: mailboxes - }; - setVisibleFolders(prev => ({ - ...prev, - [updated[accountIndex].id]: mailboxes - })); + if (updated.length > 1) { + // Only update folders, preserve other properties including ID + if (updated[1]) { + updated[1] = { + ...updated[1], + folders: mailboxes + }; + } + console.log('Updated accounts with new mailboxes:', updated); } return updated; }); - }, [mailboxes, selectedAccount]); + }, [mailboxes]); // Debug accounts state useEffect(() => { @@ -572,35 +545,58 @@ export default function CourrierPage() { setShowComposeModal(true); }; - // Update handleMailboxChange to properly handle per-account folders + // Update handleMailboxChange to track selected folders per account const handleMailboxChange = (folder: string, accountId?: string) => { + // Reset to page 1 when changing folders + setPage(1); + + // If we have a specific accountId, validate the folder exists for that account if (accountId && accountId !== 'all-accounts') { const account = accounts.find(a => a.id === accountId); - if (!account?.folders?.includes(folder)) { - folder = 'INBOX'; // Fallback to INBOX if folder doesn't exist + if (!account) { + console.error(`Account ${accountId} not found`); + return; } + + // Check if the folder exists for this account + if (!account.folders?.includes(folder)) { + console.error(`Folder ${folder} does not exist for account ${accountId}`); + // Fall back to INBOX if the folder doesn't exist + folder = 'INBOX'; + } + + // Update the selected folder for this account setSelectedFolders(prev => ({ ...prev, [accountId]: folder })); + + // Log the folder change with account ID + console.log(`Changing folder to ${folder} for account ${accountId}`); } + + // Change folder in the state, passing the clean folder name and accountId separately changeFolder(folder, accountId); + setCurrentView(folder); + + // Start prefetching additional pages for this folder + if (session?.user?.id && folder) { + // First two pages are most important - prefetch immediately + prefetchFolderEmails(session.user.id, folder, 3, 1, accountId).catch(err => { + console.error(`Error prefetching ${folder}:`, err); + }); + } }; // Update the folder button rendering to show selected state based on account const renderFolderButton = (folder: string, accountId: string) => { const isSelected = selectedFolders[accountId] === folder; - const account = accounts.find(a => a.id === accountId); - const isVisible = account?.folders?.includes(folder) || false; - - if (!isVisible) return null; - return (