Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep a reconnect's unread reset on its own session
Yudhi Armyndharis09ef2c41d8a6
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 3acc8e2d6..eae32217f 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -889,6 +889,46 @@ test('a socket reconnect keeps the open chat read instead of badging it with theassert.ok(findFetchCall('POST', `/api/sessions/${SESSION.id}/chats/read`), 'the gap messages were not marked read');});+test("a reconnect refetch that settles after a session switch leaves the new session's unread badge", async () => {+ const { screen, fireEvent, within, act, waitFor } = rtl;+ twoSessions = true;+ try {+ const { container } = renderChats();+ await screen.findByText('Main (15551234567)');+ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');++ // Session 1's reconnect refetch stays out; session 2 lists Alice too (a contact both accounts+ // share), unread there, and its list lands first.+ let releaseFirst!: () => void;+ const firstGate = new Promise<void>(resolve => {+ releaseFirst = resolve;+ });+ chatsResponder = sessionId =>+ sessionId === SESSION.id+ ? firstGate.then(() => jsonResponse([CHAT, CHAT_2]))+ : Promise.resolve(jsonResponse([{ ...CHAT, unreadCount: 3, lastMessage: 'alice on two' }, 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`) >= 2, true));++ fireEvent.change(container.querySelector('select.session-selector') as HTMLSelectElement, {+ target: { value: SESSION_2.id },+ });+ await screen.findByText('alice on two');+ assert.ok(screen.queryByLabelText('3 unread messages'), "session 2's unread count is not shown");++ releaseFirst();+ await flush();+ await flush();+ assert.ok(screen.queryByLabelText('3 unread messages'), "session 1's refetch cleared session 2's unread badge");+ } finally {+ twoSessions = false;+ }+});+test('a read-only key opening a chat sends no mark-as-read', async () => {const { screen, fireEvent, within, waitFor } = rtl;resetFetchCalls();diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 01afa59c0..78a66730d 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -615,8 +615,12 @@ export function Chats() {// since the snapshot still counts them (the same rule a live frame and opening a chat follow).const readChatId = canWrite ? activeChatId : undefined;if (readChatId) markChatRead(readChatId);+ // The zeroing waits for the refetch, by when another session's list may be on screen; like a send's+ // promote, it applies only while the list is still this session's.void loadChats(selectedSessionId, { background: true }).then(() => {- if (readChatId) setChats(prev => prev.map(c => (c.id === readChatId ? { ...c, unreadCount: 0 } : c)));+ if (readChatId && chatsSessionRef.current === selectedSessionId) {+ setChats(prev => prev.map(c => (c.id === readChatId ? { ...c, unreadCount: 0 } : c)));+ }});}}, [isConnected, connectionFailed, selectedSessionId, queryClient, loadChats, activeChatId, canWrite, markChatRead]);