Email for professionals and teams
[fix] read status inconsistent
Hieu Nguyen (Jack)0159439b2f2e
diff --git a/src/components/mark-read.tsx b/src/components/mark-read.tsxindex 244dfd7..3513915 100644--- a/src/components/mark-read.tsx+++ b/src/components/mark-read.tsx@@ -2,12 +2,15 @@import { useEffect } from "react";import { authFetch } from "@/lib/auth/client";+import { markMessagesReadInCaches } from "@/hooks/utils";export function MarkAsRead({ messageId }: { messageId: string }) {useEffect(() => {authFetch(`/api/messages/${messageId}/read`, { method: "POST" }).then((response) => {- if (response.ok) window.dispatchEvent(new Event("mailflare:messages-changed"));+ if (!response.ok) return;+ markMessagesReadInCaches([messageId], true);+ window.dispatchEvent(new Event("mailflare:messages-changed"));}).catch(() => {// silently faildiff --git a/src/components/message-actions/utils.ts b/src/components/message-actions/utils.tsindex df21e95..58dfd87 100644--- a/src/components/message-actions/utils.ts+++ b/src/components/message-actions/utils.ts@@ -1,5 +1,6 @@import type { BulkMessageAction } from "@/app/api/messages/bulk/types";import { authFetch } from "@/lib/auth/client";+import { markMessagesReadInCaches } from "@/hooks/utils";import { getEmailAddress, normalizeEmailAddress, splitEmailAddressList } from "@/lib/email/address";import { getLatestEmailContent } from "@/lib/email/reply-content-utils";import { formatUserDate } from "@/lib/time/utils";@@ -50,6 +51,7 @@ export async function runSingleMessageAction(throw new Error("Unable to update message");}+ if (action === "read" || action === "unread") markMessagesReadInCaches([messageId], action === "read");window.dispatchEvent(new Event("mailflare:messages-changed"));}diff --git a/src/components/messages/utils.ts b/src/components/messages/utils.tsindex f9c001a..71d216e 100644--- a/src/components/messages/utils.ts+++ b/src/components/messages/utils.ts@@ -1,5 +1,6 @@import type { Message } from "@/hooks/types";import { authFetch } from "@/lib/auth/client";+import { markMessagesReadInCaches } from "@/hooks/utils";import { getEmailDisplayName, splitEmailAddressList } from "@/lib/email/address";import { formatUserDate, getUserTimeZone, zonedDateFields } from "@/lib/time/utils";import type { MailboxOption } from "@/components/mailbox-provider";@@ -87,5 +88,6 @@ export async function runBulkMessageAction(messageIds: string[], action: string,});if (!response.ok) throw new Error("Unable to update selected messages");+ if (action === "read" || action === "unread") markMessagesReadInCaches(messageIds, action === "read");if (notify) window.dispatchEvent(new Event("mailflare:messages-changed"));}diff --git a/src/hooks/utils.ts b/src/hooks/utils.tsindex 62f8361..9bc18b5 100644--- a/src/hooks/utils.ts+++ b/src/hooks/utils.ts@@ -1,6 +1,7 @@import { authFetch } from "@/lib/auth/client";import { parseSearchQuery } from "@/lib/search/query-utils";import { getUserTimeZone } from "@/lib/time/utils";+import { setCachedMessageRead } from "@/lib/messages/detail-cache";import type { MessageFilterOptions, MessageFolder } from "./types";import type { MessageCounts, MessageListResponse } from "./types";@@ -83,6 +84,39 @@ export function clearMessageListCache() {messageListRequests.clear();}+/**+ * Mirror a read/unread change into the cached lists and details, so a list+ * remounted later (or a detail reopened) never shows the old state.+ */+export function markMessagesReadInCaches(messageIds: string[], read: boolean) {+ const ids = new Set(messageIds);+ for (const [key, response] of messageListCache) {+ if (!response.messages?.some((message) => ids.has(message.id))) continue;+ messageListCache.set(key, {+ ...response,+ messages: response.messages.map((message) => {+ if (!ids.has(message.id)) return message;+ const threadSize = message.threadMessageIds?.length;+ return {+ ...message,+ read,+ ...(threadSize !== undefined ? { threadUnread: read ? 0 : threadSize } : {}),+ };+ }),+ });+ }+ for (const id of ids) setCachedMessageRead(id, read);+}++// Caches must drop on every change, including ones made while no list is mounted+// (e.g. reading a message from the popup on a page without the inbox list).+if (typeof window !== "undefined") {+ window.addEventListener("mailflare:messages-changed", () => {+ clearMessageListCache();+ clearMessageCountsCache();+ });+}+export function clearMessageClientState() {messageCacheGeneration += 1;messageCountsGeneration += 1;@@ -127,7 +161,7 @@ export async function fetchMessageCounts(mailboxId?: string | null, force = falsexport async function fetchMessageList(params: URLSearchParams, force = false): Promise<MessageListResponse> {const key = params.toString();if (!force && messageListCache.has(key)) return messageListCache.get(key) ?? {};- if (messageListRequests.has(key)) return messageListRequests.get(key) ?? {};+ if (!force && messageListRequests.has(key)) return messageListRequests.get(key) ?? {};const requestGeneration = messageCacheGeneration;const request = authFetch(`/api/messages?${key}`)diff --git a/src/lib/messages/detail-cache.ts b/src/lib/messages/detail-cache.tsindex 9aacbb0..09a2b8c 100644--- a/src/lib/messages/detail-cache.ts+++ b/src/lib/messages/detail-cache.ts@@ -14,6 +14,11 @@ export function getCachedMessageDetail(messageId: string): CachedMessageDetail |return detailCache.get(messageId);}+export function setCachedMessageRead(messageId: string, read: boolean): void {+ const cached = detailCache.get(messageId);+ if (cached?.message) detailCache.set(messageId, { ...cached, message: { ...cached.message, read } });+}+export function primeMessageDetail(message: Message): void {if (message.textBody === undefined && message.htmlBody === undefined) return;detailCache.set(message.id, {