From 96fc0fe33e1ee3dac61abbab3e672308b6344e14 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 30 Apr 2025 14:27:58 +0200 Subject: [PATCH] courrier multi account restore compose --- hooks/use-email-state.ts | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/hooks/use-email-state.ts b/hooks/use-email-state.ts index 1720312d..f85e71fc 100644 --- a/hooks/use-email-state.ts +++ b/hooks/use-email-state.ts @@ -338,6 +338,64 @@ export const useEmailState = () => { payload: { emailId, isRead, accountId: effectiveAccountId } }); + // If email is being marked as read, update the unread count for this folder + if (isRead && email && !email.flags.seen) { + // Get current count for this folder + const currentCount = state.unreadCountMap[effectiveAccountId]?.[normalizedFolder] || 0; + if (currentCount > 0) { + // Decrement the unread count (minimum 0) + dispatch({ + type: 'UPDATE_UNREAD_COUNT', + payload: { + accountId: effectiveAccountId, + folder: normalizedFolder, + count: Math.max(0, currentCount - 1) + } + }); + + // Also update the prefixed version if needed + const prefixedFolder = `${effectiveAccountId}:${normalizedFolder}`; + const prefixedCount = state.unreadCountMap[effectiveAccountId]?.[prefixedFolder] || 0; + if (prefixedCount > 0) { + dispatch({ + type: 'UPDATE_UNREAD_COUNT', + payload: { + accountId: effectiveAccountId, + folder: prefixedFolder, + count: Math.max(0, prefixedCount - 1) + } + }); + } + } + } + // If email is being marked as unread, increment the count + else if (!isRead && email && email.flags.seen) { + // Get current count for this folder + const currentCount = state.unreadCountMap[effectiveAccountId]?.[normalizedFolder] || 0; + + // Increment the unread count + dispatch({ + type: 'UPDATE_UNREAD_COUNT', + payload: { + accountId: effectiveAccountId, + folder: normalizedFolder, + count: currentCount + 1 + } + }); + + // Also update the prefixed version + const prefixedFolder = `${effectiveAccountId}:${normalizedFolder}`; + const prefixedCount = state.unreadCountMap[effectiveAccountId]?.[prefixedFolder] || 0; + dispatch({ + type: 'UPDATE_UNREAD_COUNT', + payload: { + accountId: effectiveAccountId, + folder: prefixedFolder, + count: prefixedCount + 1 + } + }); + } + // Make API call to update on server const response = await fetch(`/api/courrier/${emailId}/mark-read`, { method: 'POST',