Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): sync the chat list ref before a frame can read it
Yudhi Armyndharis0465ce8b1bdb
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 063cd7cff..4d435c250 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -793,6 +793,52 @@ test('a chat whose newest message has no text does not claim to have no messagesassert.equal(row.querySelector('.no-message')?.textContent ?? null, null, 'the row reads "No messages yet"');});+test('a message arriving the moment the chat list commits updates the listed row without a refetch', async () => {+ const { screen, waitFor } = rtl;+ resetFetchCalls();+ // Deliver the frame from a MutationObserver callback, a microtask right after the commit that puts+ // the Alice row on screen and before React flushes that commit's passive effects. A list the socket+ // handler reads from a passive effect is still empty there, so the chat reads as unlisted and the+ // refetch it fires replaces the row this frame just updated.+ let delivered = false;+ const observer = new MutationObserver(() => {+ if (delivered || !screen.queryByText('Alice')) return;+ delivered = true;+ observer.disconnect();+ const socket = lastSocket();+ assert.ok(socket, 'expected the page to have opened a socket');+ socket.receive('message', {+ type: 'event',+ timestamp: new Date(1_700_002_000_000).toISOString(),+ payload: {+ event: 'message.received',+ sessionId: SESSION.id,+ data: {+ id: 'wamid.live.commit',+ chatId: CHAT.id,+ from: CHAT.id,+ to: 'me',+ body: 'right on the commit',+ type: 'text',+ fromMe: false,+ timestamp: 1_700_001_800,+ },+ },+ });+ });+ observer.observe(document.body, { childList: true, subtree: true, characterData: true });+ try {+ renderChats();+ await waitFor(() => assert.ok(screen.queryByLabelText('3 unread messages'), 'the arrival did not reach the row'));+ await flush();+ assert.ok(delivered, 'the frame was never delivered');+ assert.equal(countFetchCalls('GET', `/api/sessions/${SESSION.id}/chats`), 1, 'a listed chat refetched the list');+ assert.ok(screen.queryByLabelText('3 unread messages'), 'a refetch overwrote the live row');+ } finally {+ observer.disconnect();+ }+});+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 976864fd7..337158f47 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -396,8 +396,10 @@ export function Chats() {);// 3. WebSocket integration for real-time messages+ // Synced in a layout effect: a socket frame handled after the list commits but before passive+ // effects flush would otherwise see the previous list, miss the chat and refetch the whole list.const chatsRef = useRef(chats);- useEffect(() => {+ useLayoutEffect(() => {chatsRef.current = chats;});const handleIncomingMessage = useCallback(