Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): do not read the address book at a session switch
Yudhi Armyndharis9df54c14197c
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.tsindex 2b4485219..6e54a3813 100644--- a/dashboard/src/pages/Chats.features.test.ts+++ b/dashboard/src/pages/Chats.features.test.ts@@ -2136,6 +2136,33 @@ test('the first menu to open reads the address book once, and its Save entry theassert.equal(contactReads().length, 1, 'a second menu read the address book again');});+test('switching session after a menu opened reads no address book for the new session', async () => {+ const { screen, fireEvent, waitFor } = rtl;+ listSecond = true;+ const { container } = renderPage();+ await screen.findByText('Alice');+ fireEvent.click(screen.getByRole('button', { name: 'Actions for Alice' }));+ await screen.findByRole('button', { name: 'Rename contact' });+ fireEvent.keyDown(document, { key: 'Escape' });+ const readsOn = (id: string) =>+ fetchCalls.filter(c => c.method === 'GET' && c.path.startsWith(`/api/sessions/${id}/contacts?`)).length;+ assert.equal(readsOn(SESSION.id), 1);++ switchSession(container, SECOND_SESSION.id);+ await waitFor(() =>+ assert.equal((container.querySelector('select.session-selector') as HTMLSelectElement).value, SECOND_SESSION.id),+ );+ await screen.findByText('Alice');+ await settle();+ await settle();+ assert.equal(readsOn(SECOND_SESSION.id), 0, 'the new session paid for an address book nobody asked for');++ // A menu on the new session asks for its own.+ fireEvent.click(screen.getByRole('button', { name: 'Actions for Alice' }));+ await screen.findByRole('button', { name: 'Rename contact' });+ assert.equal(readsOn(SECOND_SESSION.id), 1);+});+test("a group's menu has no use for the address book and does not read it", async () => {const { screen, fireEvent } = rtl;listGroup = true;diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex c109ec06a..acae361c8 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -226,8 +226,10 @@ export function Chats() {const [muteDialogChat, setMuteDialogChat] = useState<Chat | null>(null);const [contactDialogChat, setContactDialogChat] = useState<Chat | null>(null);const [dialogRunning, setDialogRunning] = useState<Chat | null>(null);- // Whether a menu has opened on this session, which is when its entries need the address book (see contactsQuery).- const [menuOpened, setMenuOpened] = useState<boolean>(false);+ // The session a menu has opened on, which is when its entries need that session's address book (see contactsQuery).+ // Keyed by session, so it needs no reset at a switch: a reset lands a render after the new session is selected,+ // when its address book has already been asked for.+ const [menuOpenedFor, setMenuOpenedFor] = useState<string>('');// Selected chat & message historyconst [activeChat, setActiveChat] = useState<Chat | null>(null);@@ -562,7 +564,6 @@ export function Chats() {setConfirmAction(null);setMuteDialogChat(null);setContactDialogChat(null);- setMenuOpened(false);setShowArchived(false);}return () => {@@ -1238,7 +1239,7 @@ export function Chats() {const contactsQuery = useQuery({queryKey: ['contacts', selectedSessionId],queryFn: () => contactApi.list(selectedSessionId),- enabled: Boolean(selectedSessionId) && (menuOpened || chatFilter === 'unknown'),+ enabled: Boolean(selectedSessionId) && (menuOpenedFor === selectedSessionId || chatFilter === 'unknown'),staleTime: 5 * 60 * 1000,// contactApi.list already retried a throttled page; another attempt would walk every page again.retry: false,@@ -1449,7 +1450,7 @@ export function Chats() {const rowActions: ChatRowActions = {// Only what a key that can write offers reads the address book (Save or Rename); a read-only menu has no such entry.- onMenuOpen: canWrite ? () => setMenuOpened(true) : undefined,+ onMenuOpen: canWrite ? () => setMenuOpenedFor(selectedSessionId) : undefined,onGroupInfo: setGroupDialogChat,onSaveContact: canWrite ? setContactDialogChat : undefined,canSaveContact: chat => addressbookIdOf(chat) !== null,