From ed39772a28f82ee2e3d8c710a32ea2f598ec4b9a Mon Sep 17 00:00:00 2001 From: alma Date: Mon, 28 Apr 2025 14:55:29 +0200 Subject: [PATCH] courrier multi account restore compose --- app/courrier/page.tsx | 55 +++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 896b4603..0b47c34f 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -177,19 +177,23 @@ export default function CourrierPage() { } }, [accounts, selectedAccount, showFolders, currentFolder]); - // Calculate unread count for each account + // Calculate unread count for each account and folder useEffect(() => { - // Create a map to store unread counts per account - const accountUnreadCounts = new Map(); + // Create a map to store unread counts per account and folder + const accountFolderUnreadCounts = new Map>(); - // Initialize counts for all accounts + // Initialize counts for all accounts and folders accounts.forEach(account => { if (account.id !== 'all-accounts' && account.id !== 'loading-account') { - accountUnreadCounts.set(account.id, 0); + const folderCounts = new Map(); + account.folders.forEach(folder => { + folderCounts.set(folder, 0); + }); + accountFolderUnreadCounts.set(account.id, folderCounts); } }); - // Count unread emails for each account + // Count unread emails for each account and folder (emails || []).forEach(email => { // Access the 'read' property safely, handling both old and new email formats const emailWithFlags = email as unknown as EmailWithFlags; @@ -197,28 +201,43 @@ export default function CourrierPage() { (emailWithFlags.flags && !emailWithFlags.flags.seen) || false; - // Count unread emails regardless of current folder - if (isUnread) { - // 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); + // Count unread emails for the specific account and folder + if (isUnread && email.accountId && email.folder) { + const folderCounts = accountFolderUnreadCounts.get(email.accountId); + if (folderCounts) { + const currentCount = folderCounts.get(email.folder) || 0; + folderCounts.set(email.folder, currentCount + 1); } } }); - // Update the unread count for the selected account + // Update the unread count for the selected account and folder if (selectedAccount && selectedAccount.id !== 'all-accounts') { - setUnreadCount(accountUnreadCounts.get(selectedAccount.id) || 0); + const folderCounts = accountFolderUnreadCounts.get(selectedAccount.id); + if (folderCounts) { + setUnreadCount(folderCounts.get(currentFolder) || 0); + } else { + setUnreadCount(0); + } } else { - // For 'all-accounts', sum up all unread counts + // For 'all-accounts', sum up all unread counts for the current folder let totalUnread = 0; - accountUnreadCounts.forEach(count => { - totalUnread += count; + accountFolderUnreadCounts.forEach((folderCounts: Map) => { + totalUnread += folderCounts.get(currentFolder) || 0; }); setUnreadCount(totalUnread); } - }, [emails, selectedAccount, accounts]); + + // Log the counts for debugging + console.log('Unread counts per account and folder:', + Object.fromEntries( + Array.from(accountFolderUnreadCounts.entries()).map(([accountId, folderCounts]) => [ + accountId, + Object.fromEntries(folderCounts.entries()) + ]) + ) + ); + }, [emails, selectedAccount, currentFolder, accounts]); // Ensure accounts section is never empty useEffect(() => {