Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep the open chat read after a socket reconnect

Yudhi Armyndharis20f1d27a1acb
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index a815a2745..3acc8e2d6 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -864,6 +864,31 @@ test('a socket reconnect refetches the chat list so the sidebar shows what arriv
assert.equal(countFetchCalls('GET', chatsPath), 2);
});
+test('a socket reconnect keeps the open chat read instead of badging it with the gap count', async () => {
+ const { screen, fireEvent, within, act, waitFor } = rtl;
+ 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');
+ await waitFor(() => assert.ok(!screen.queryByLabelText('2 unread messages'), 'opening the chat kept its badge'));
+ // Past the mark-as-read quiet window, so the open's own call is out before counting.
+ await new Promise(resolve => setTimeout(resolve, 1_000));
+ resetFetchCalls();
+
+ // Alice wrote into the open chat while the socket was down; the snapshot still counts it unread.
+ chatsResponder = () =>
+ Promise.resolve(jsonResponse([{ ...CHAT, unreadCount: 4, lastMessage: 'sent during the gap' }, 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 screen.findByText('sent during the gap');
+ await new Promise(resolve => setTimeout(resolve, 1_000));
+ assert.ok(!screen.queryByLabelText('4 unread messages'), 'the open chat was badged with the gap count');
+ assert.ok(findFetchCall('POST', `/api/sessions/${SESSION.id}/chats/read`), 'the gap messages were not marked read');
+});
+
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.tsx
index 48ec38047..01afa59c0 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -594,6 +594,7 @@ export function Chats() {
// banner's retry refreshes too. The transition logic is unit-tested in utils/reconnectState.
const reconnectHadConnected = useRef(false);
const reconnectWasDisconnected = useRef(false);
+ const activeChatId = activeChat?.id;
useEffect(() => {
const decision = nextReconnectState({
isConnected,
@@ -610,9 +611,15 @@ export function Chats() {
queryClient.invalidateQueries({ queryKey: ['contact-statuses', selectedSessionId] });
// The sidebar list is local state, so previews, unread counts and chats started during the gap
// only show after a refetch. Background mode keeps the current list on screen meanwhile.
- void loadChats(selectedSessionId, { background: true });
+ // The open chat's gap messages are read on screen: mark them read and keep its row at zero,
+ // 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);
+ void loadChats(selectedSessionId, { background: true }).then(() => {
+ if (readChatId) setChats(prev => prev.map(c => (c.id === readChatId ? { ...c, unreadCount: 0 } : c)));
+ });
}
- }, [isConnected, connectionFailed, selectedSessionId, queryClient, loadChats]);
+ }, [isConnected, connectionFailed, selectedSessionId, queryClient, loadChats, activeChatId, canWrite, markChatRead]);
useEffect(() => {
if (selectedSessionId && isConnected) {