Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): refetch cached threads on a session switch with no feed
Yudhi Armyndharisec0f91101028
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 894622e69..34beb5727 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -1437,7 +1437,7 @@ test('a staged reply is kept on reopening its chat and dropped when a different);});-test('a chat reopened after switching sessions and back fetches its thread again', async () => {+async function reopenAfterSessionSwitch(): Promise<void> {const { screen, fireEvent, within, waitFor } = rtl;twoSessions = true;chatsResponder = sessionId => Promise.resolve(jsonResponse(sessionId === SESSION.id ? [CHAT] : [CHAT_2]));@@ -1474,6 +1474,36 @@ test('a chat reopened after switching sessions and back fetches its thread againglobalThis.fetch = stub;twoSessions = false;}+}++test('a chat reopened after switching sessions and back fetches its thread again', reopenAfterSessionSwitch);++// A chat-scoped key is refused at the /events handshake, so its page never has a live feed.+test('a chat reopened after switching sessions and back fetches its thread again without a live feed', async () => {+ holdConnect();+ await reopenAfterSessionSwitch();+});++// A drop alone delivers nothing new; the reconnect is what refetches the open thread.+test('a socket drop does not refetch the open thread', async () => {+ const { screen, fireEvent, within, act } = rtl;+ resetFetchCalls();+ const { container } = renderChats();+ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');+ const threadFetches = () =>+ fetchCalls.filter(+ c =>+ c.method === 'GET' &&+ c.path.startsWith(`/api/sessions/${SESSION.id}/messages?chatId=${encodeURIComponent(CHAT.id)}&`),+ ).length;+ const before = threadFetches();++ const socket = lastSocket();+ assert.ok(socket, 'expected the page to have opened a socket');+ act(() => socket.receive('disconnect', 'transport close'));+ await act(() => new Promise(resolve => setTimeout(resolve, 50)));+ assert.equal(threadFetches(), before, 'the open thread was refetched on a socket drop');});test('a staged reply is dropped when another session is opened', async () => {diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex b798adeda..d2f418db4 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -675,6 +675,14 @@ export function Chats() {}}, [isConnected, connectionFailed, selectedSessionId, queryClient, loadChats, activeChatId, canWrite, markChatRead]);+ // The threads cache at staleTime: Infinity, and this session's events were not delivered while+ // another session was selected or the page was away. Mark them stale on a mount or a session switch+ // so the open one refetches and the others do when opened, with or without a live feed (a+ // chat-scoped key is refused one).+ useEffect(() => {+ if (selectedSessionId) void queryClient.invalidateQueries({ queryKey: ['messages', selectedSessionId] });+ }, [selectedSessionId, queryClient]);+useEffect(() => {if (selectedSessionId && isConnected) {subscribe(selectedSessionId, [@@ -686,9 +694,8 @@ export function Chats() {'message.edited','status.received',]);- // The threads cache at staleTime: Infinity, and events for this session were not delivered before- // this subscribe: after a mount, a session switch or a reconnect, a cached thread may miss some.- // Mark them stale so the open one refetches and the others do when opened.+ // Events sent while the socket was down were not delivered: after a reconnect, a cached thread+ // may miss some. Mark them stale the same way.void queryClient.invalidateQueries({ queryKey: ['messages', selectedSessionId] });return () => {unsubscribe(selectedSessionId);