Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): drop a chat list that lands after a session switch
Yudhi Armyndharis6f03116804b6
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 9738d8a91..7170bb8fd 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -37,6 +37,10 @@ const SESSION: Session = {const SESSION_2: Session = { ...SESSION, id: 'session-2', name: 'Second', phone: '15559876543' };let twoSessions = false;+// Answers a session's chat list in place of the fixture, keyed by the session id in the URL (before+// the rewrite below folds session-2 onto session-1), so a test can land two lists in any order.+let chatsResponder: ((sessionId: string) => Promise<Response>) | null = null;+const CHAT: Chat = {id: '[email protected]',name: 'Alice',@@ -260,6 +264,7 @@ function installFetchStub(): void {return Promise.resolve(jsonResponse({ engineType: 'baileys' }));}if (method === 'GET' && path === `/api/sessions/${SESSION.id}/chats`) {+ if (chatsResponder) return chatsResponder(url.includes(`/sessions/${SESSION_2.id}/`) ? SESSION_2.id : SESSION.id);return Promise.resolve(jsonResponse([CHAT, CHAT_2]));}if (method === 'GET' && path.startsWith(`/api/sessions/${SESSION.id}/contacts/profile-pictures`)) {@@ -369,6 +374,7 @@ afterEach(() => {olderPageGate = null;sendGate = null;olderPageFails = false;+ chatsResponder = null;});function renderChats(): { container: HTMLElement } {@@ -815,6 +821,36 @@ test('a staged reply is dropped when another session is opened', async () => {}});+test('a chat list that answers after the user switched sessions does not replace the new one', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ twoSessions = true;+ let releaseFirst!: () => void;+ const firstGate = new Promise<void>(resolve => {+ releaseFirst = resolve;+ });+ chatsResponder = sessionId =>+ sessionId === SESSION.id ? firstGate.then(() => jsonResponse([CHAT])) : Promise.resolve(jsonResponse([CHAT_2]));+ try {+ const { container } = renderChats();+ await screen.findByText('Main (15551234567)');+ await waitFor(() => assert.ok(container.querySelector('.chats-list-loading'), 'the first list never started'));++ // Session 2's list lands first; session 1's slower answer arrives after it.+ fireEvent.change(container.querySelector('select.session-selector') as HTMLSelectElement, {+ target: { value: SESSION_2.id },+ });+ await screen.findByText('Carol');+ releaseFirst();+ await flush();+ await flush();++ assert.ok(!screen.queryByText('Alice'), "the previous session's chats replaced the selected session's list");+ assert.ok(screen.queryByText('Carol'), "the selected session's chats are gone");+ } finally {+ twoSessions = false;+ }+});+/*** The server-side inline-media budget replaces an over-budget payload with `{ omitted: true }`. This* thread requests the largest page size and caches it with staleTime Infinity, so without a fetch ofdiff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 631abd5d9..53250028f 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -294,20 +294,26 @@ export function Chats() {void loadSessions();}, [t, showErrorToast]);- // 2. Fetch chats when active session changes+ // 2. Fetch chats when active session changes. Only the newest call may write: a session switch+ // does not cancel the list still loading for the session left behind, and a list that lands+ // late would put that account's chats under the selected session.+ const chatsRequestRef = useRef(0);const loadChats = useCallback(async (sessionId: string) => {if (!sessionId) return;+ const request = ++chatsRequestRef.current;try {setLoadingChats(true);const data = await sessionApi.getChats(sessionId);+ if (request !== chatsRequestRef.current) return;const sorted = [...data].sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0));setChats(sorted);} catch (err) {+ if (request !== chatsRequestRef.current) return;showErrorToast(t('chats.errors.loadChats'), err instanceof Error ? err.message : undefined);setChats([]);} finally {- setLoadingChats(false);+ if (request === chatsRequestRef.current) setLoadingChats(false);}},[t, showErrorToast],