Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): show a chat the sidebar does not list yet
Yudhi Armyndharisb75dc4f736d4
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 0c06f4c50..8e70210b5 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -998,6 +998,48 @@ test('a chat list refetch lands while a newer one is out, and an older answer neassert.ok(!screen.queryByText('carol v2'), 'an older answer replaced the newer list');});+test('every message for a chat the sidebar does not list refetches the list, and the chat appears', async () => {+ const { screen, waitFor } = rtl;+ renderChats();+ await screen.findByText('Alice');+ resetFetchCalls();++ const DAVE = '[email protected]';+ const socket = lastSocket();+ assert.ok(socket, 'expected the page to have opened a socket');+ const receive = (id: string): void =>+ socket.receive('message', {+ type: 'event',+ timestamp: new Date(1_700_003_000_000).toISOString(),+ payload: {+ event: 'message.received',+ sessionId: SESSION.id,+ data: {+ id,+ chatId: DAVE,+ from: DAVE,+ to: 'me',+ body: 'hi',+ type: 'text',+ fromMe: false,+ timestamp: 1_700_003_000,+ },+ },+ });+ // Each arrival renders before the next, the shape of live traffic.+ const chatsPath = `/api/sessions/${SESSION.id}/chats`;+ for (const [index, id] of ['wamid.dave.1', 'wamid.dave.2', 'wamid.dave.3'].entries()) {+ receive(id);+ await waitFor(() => assert.equal(countFetchCalls('GET', chatsPath), index + 1, 'an arrival did not refetch'));+ await flush();+ }++ chatsResponder = () =>+ Promise.resolve(jsonResponse([{ ...CHAT_2, id: DAVE, name: 'Dave', lastMessage: 'hi' }, CHAT_2, CHAT]));+ receive('wamid.dave.4');+ await screen.findByText('Dave');+});+test('changing the UI language keeps the selected session and the open chat', async () => {const { screen, fireEvent, within, waitFor, act } = rtl;const { default: i18n } = await import('../i18n/index.ts');diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex bb0f4dae3..4f30db393 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -386,6 +386,10 @@ export function Chats() {);// 3. WebSocket integration for real-time messages+ const chatsRef = useRef(chats);+ useEffect(() => {+ chatsRef.current = chats;+ });const handleIncomingMessage = useCallback((event: { sessionId: string; message: Record<string, unknown> }) => {if (event.sessionId !== selectedSessionId) return;@@ -422,20 +426,18 @@ export function Chats() {if (!newMsg.fromMe) onMessageAppended('incoming');}- // Update sidebar chat list. The refetch is REPORTED by the reducer and fired below, never from- // inside the updater: React double-invokes updaters under StrictMode, so a side effect in there- // ran twice for every message arriving in a chat the sidebar does not have.- let needsSidebarRefetch = false;- setChats(prevChats => {- const result = applyIncomingToChatList(prevChats, newMsg, {- // Only a chat this key marks read is exempt from the unread count (see markChatRead).- activeChatId: canWrite ? activeChat?.id : undefined,- // A location message's body is the (multi-KB) base64 map thumbnail; show a label instead.- locationLabel: `π ${t('chats.media.location')}`,- });- needsSidebarRefetch = result.needsSidebarRefetch;- return result.chats;- });+ // Update sidebar chat list. Whether the chat is missing is decided against the list on screen,+ // never inside the updater: React double-invokes updaters under StrictMode, and it may defer+ // one to the next render, so a flag set in there was still false when read here and a chat the+ // sidebar does not list never appeared.+ const listOptions = {+ // Only a chat this key marks read is exempt from the unread count (see markChatRead).+ activeChatId: canWrite ? activeChat?.id : undefined,+ // A location message's body is the (multi-KB) base64 map thumbnail; show a label instead.+ locationLabel: `π ${t('chats.media.location')}`,+ };+ const { needsSidebarRefetch } = applyIncomingToChatList(chatsRef.current, newMsg, listOptions);+ setChats(prevChats => applyIncomingToChatList(prevChats, newMsg, listOptions).chats);if (needsSidebarRefetch) {void loadChats(selectedSessionId, { background: true });}