diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 07769033..c186663a 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -177,18 +177,47 @@ export default function CourrierPage() { } }, [accounts, selectedAccount, showFolders, currentFolder]); - // Calculate unread count (this would be replaced with actual data in production) + // Calculate unread count for each account useEffect(() => { - // Example: counting unread emails in the inbox - const unreadInInbox = (emails || []).filter(email => { + // Create a map to store unread counts per account + const accountUnreadCounts = new Map(); + + // Initialize counts for all accounts + accounts.forEach(account => { + if (account.id !== 'all-accounts' && account.id !== 'loading-account') { + accountUnreadCounts.set(account.id, 0); + } + }); + + // Count unread emails for each account + (emails || []).forEach(email => { // Access the 'read' property safely, handling both old and new email formats const emailWithFlags = email as unknown as EmailWithFlags; - return (!emailWithFlags.read && emailWithFlags.read !== undefined) || - (emailWithFlags.flags && !emailWithFlags.flags.seen) || - false; - }).filter(email => currentFolder === 'INBOX').length; - setUnreadCount(unreadInInbox); - }, [emails, currentFolder]); + const isUnread = (!emailWithFlags.read && emailWithFlags.read !== undefined) || + (emailWithFlags.flags && !emailWithFlags.flags.seen) || + false; + + if (isUnread && email.folder === 'INBOX') { + // If email has an accountId, increment that account's count + if (email.accountId) { + const currentCount = accountUnreadCounts.get(email.accountId) || 0; + accountUnreadCounts.set(email.accountId, currentCount + 1); + } + } + }); + + // Update the unread count for the selected account + if (selectedAccount && selectedAccount.id !== 'all-accounts') { + setUnreadCount(accountUnreadCounts.get(selectedAccount.id) || 0); + } else { + // For 'all-accounts', sum up all unread counts + let totalUnread = 0; + accountUnreadCounts.forEach(count => { + totalUnread += count; + }); + setUnreadCount(totalUnread); + } + }, [emails, selectedAccount, accounts]); // Ensure accounts section is never empty useEffect(() => { diff --git a/hooks/use-courrier.ts b/hooks/use-courrier.ts index 4bdbcd71..093b5810 100644 --- a/hooks/use-courrier.ts +++ b/hooks/use-courrier.ts @@ -25,6 +25,7 @@ export interface Email { folder: string; hasAttachments: boolean; contentFetched?: boolean; + accountId?: string; } export interface EmailListResult {