Skip to content

Email for professionals and teams

update email row action

Hieu Nguyen (Jack)78fefdeb9b96
diff --git a/src/components/messages/message-folder-page.tsx b/src/components/messages/message-folder-page.tsx
index 2817d05..59127b2 100644
--- a/src/components/messages/message-folder-page.tsx
+++ b/src/components/messages/message-folder-page.tsx
@@ -17,7 +17,7 @@ import type { BulkMessageAction } from "@/app/api/messages/bulk/types";
import { setMessageDragData } from "@/lib/messages/drag-utils";
import { BulkMessageToolbar } from "./bulk-message-toolbar";
import { MessageListRowActions } from "./message-list-row-actions";
-import { toggleMessageStar } from "./message-list-row-actions-utils";
+import { dispatchMessageCountsDelta, toggleMessageStar } from "./message-list-row-actions-utils";
import { MessageNavigationProgress, useMessageNavigation } from "./message-navigation";
import type { MessageFolderPageProps, MessageListRowProps } from "./types";
import {
@@ -189,12 +189,17 @@ function MessageListRow({
message={rowMessage}
onAction={async (action) => {
const previousRead = read;
+ const unreadDelta = action === "read" ? -1 : action === "unread" ? 1 : 0;
if (action === "read") setRead(true);
if (action === "unread") setRead(false);
+ if (unreadDelta) dispatchMessageCountsDelta({ inboxUnreadDelta: unreadDelta });
try {
await onMessageAction(message.id, action);
} catch (error) {
- if (action === "read" || action === "unread") setRead(previousRead);
+ if (action === "read" || action === "unread") {
+ setRead(previousRead);
+ if (unreadDelta) dispatchMessageCountsDelta({ inboxUnreadDelta: -unreadDelta });
+ }
throw error;
}
}}
diff --git a/src/components/messages/message-list-row-actions-utils.ts b/src/components/messages/message-list-row-actions-utils.ts
index 43b6110..814efc7 100644
--- a/src/components/messages/message-list-row-actions-utils.ts
+++ b/src/components/messages/message-list-row-actions-utils.ts
@@ -1,4 +1,5 @@
import { authFetch } from "@/lib/auth/client";
+import type { MessageCountsDelta } from "@/hooks/types";
export type SnoozePreset = {
label: string;
@@ -52,3 +53,7 @@ export async function toggleMessageStar(messageId: string) {
window.dispatchEvent(new Event("mailflare:message-counts-changed"));
return result;
}
+
+export function dispatchMessageCountsDelta(detail: MessageCountsDelta) {
+ window.dispatchEvent(new CustomEvent<MessageCountsDelta>("mailflare:message-counts-delta", { detail }));
+}
diff --git a/src/hooks/types.d.ts b/src/hooks/types.d.ts
index 5677e0f..8c2c5e7 100644
--- a/src/hooks/types.d.ts
+++ b/src/hooks/types.d.ts
@@ -59,3 +59,7 @@ export type MessageCounts = {
customFolders: Record<string, FolderCount>;
mailboxes: MailboxCount[];
};
+
+export type MessageCountsDelta = {
+ inboxUnreadDelta?: number;
+};
diff --git a/src/hooks/use-message-counts.ts b/src/hooks/use-message-counts.ts
index ca9ace8..a84268e 100644
--- a/src/hooks/use-message-counts.ts
+++ b/src/hooks/use-message-counts.ts
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
-import type { MessageCounts } from "./types";
+import type { MessageCounts, MessageCountsDelta } from "./types";
import { clearMessageCountsCache, fetchMessageCounts } from "./utils";
const emptyCounts: MessageCounts = {
@@ -44,14 +44,30 @@ export function useMessageCounts(mailboxId?: string | null, enabled = true) {
clearMessageCountsCache();
void loadCounts(true);
}
+ function onMessageCountsDelta(event: Event) {
+ const detail = (event as CustomEvent<MessageCountsDelta>).detail;
+ if (!detail?.inboxUnreadDelta) return;
+ setCounts((current) => ({
+ ...current,
+ folders: {
+ ...current.folders,
+ inbox: {
+ ...current.folders.inbox,
+ unread: Math.max(0, current.folders.inbox.unread + detail.inboxUnreadDelta),
+ },
+ },
+ }));
+ }
window.addEventListener("mailflare:messages-changed", onMessagesChanged);
window.addEventListener("mailflare:message-counts-changed", onMessagesChanged);
+ window.addEventListener("mailflare:message-counts-delta", onMessageCountsDelta);
const refreshInterval = window.setInterval(() => void loadCounts(true), 15_000);
return () => {
cancelled = true;
window.removeEventListener("mailflare:messages-changed", onMessagesChanged);
window.removeEventListener("mailflare:message-counts-changed", onMessagesChanged);
+ window.removeEventListener("mailflare:message-counts-delta", onMessageCountsDelta);
window.clearInterval(refreshInterval);
};
}, [enabled, mailboxId]);