Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): send no mark-as-read after leaving the Chats page

Yudhi Armyndharis0e0ec81b7a38
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index ba988f26d..24e9648f0 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -440,7 +440,7 @@ afterEach(async () => {
statuses = [];
});
-function renderChats(): { container: HTMLElement } {
+function renderChats(): ReturnType<RTL['render']> {
// The avatar and message hooks set their own gcTime over this 1s default; afterEach cancels before it
// clears so their timers cannot hold the test process open.
queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 1_000 } } });
@@ -1024,6 +1024,37 @@ test('a reconnect refetch that settles after the open chat was left keeps its un
);
});
+test('a reconnect refetch that settles after the page was left sends no mark-as-read', async () => {
+ const { screen, fireEvent, within, act, waitFor } = rtl;
+ const { container, unmount } = renderChats();
+ await screen.findByText('Main (15551234567)');
+ fireEvent.click(await screen.findByText('Alice'));
+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
+ await new Promise(resolve => setTimeout(resolve, 1_000));
+ resetFetchCalls();
+
+ let release!: () => void;
+ const gate = new Promise<void>(resolve => {
+ release = resolve;
+ });
+ chatsResponder = () =>
+ gate.then(() => jsonResponse([{ ...CHAT, unreadCount: 4, lastMessage: 'sent after leaving' }, CHAT_2]));
+ const socket = lastSocket();
+ assert.ok(socket, 'expected the page to have opened a socket');
+ act(() => socket.receive('disconnect', 'transport close'));
+ act(() => socket.receive('connect'));
+ await waitFor(() => assert.equal(countFetchCalls('GET', `/api/sessions/${SESSION.id}/chats`), 1));
+
+ // The operator opens another page before the refetch lands; the gap messages were never seen.
+ unmount();
+ release();
+ await new Promise(resolve => setTimeout(resolve, 1_000));
+ assert.ok(
+ !findFetchCall('POST', `/api/sessions/${SESSION.id}/chats/read`),
+ 'the chat was marked read after the page was left',
+ );
+});
+
test('a reconnect refetch overtaken by a newer list still marks the open chat read', async () => {
const { screen, fireEvent, within, act, waitFor } = rtl;
const { container } = renderChats();
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index fdc3f904f..c6e8d8a06 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -428,6 +428,14 @@ export function Chats() {
chatsRef.current = chats;
activeChatIdRef.current = activeChat?.id;
});
+ // A request that settles after the page is left no longer finds the chat open, so its follow-ups
+ // (the reconnect mark-as-read) do nothing.
+ useEffect(
+ () => () => {
+ activeChatIdRef.current = undefined;
+ },
+ [],
+ );
const handleIncomingMessage = useCallback(
(event: { sessionId: string; message: Record<string, unknown> }) => {
if (event.sessionId !== selectedSessionId) return;