Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): drop a search hit whose chat never showed up
Yudhi Armyndharis951df8e19f37
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 24e9648f0..153cb8f85 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -1825,6 +1825,62 @@ test("a search hit in another session opens that session's chat, not the one the}});+// Search reads stored messages while the list comes from the engine, so a hit's chat can be missing.+test("a search hit whose chat the other session's list lacks does not open it later on its own", async () => {+ const { screen, fireEvent, within, act, waitFor } = rtl;+ twoSessions = true;+ const DAVE: Chat = { ...CHAT_2, id: '[email protected]', name: 'Dave', timestamp: 1_700_000_900 };+ searchHits = [{ ...THIRD_SESSION_HIT, sessionId: SESSION_2.id, chatId: DAVE.id }];+ try {+ const { container } = renderChats();+ await screen.findByText('Alice');+ await clickSearchHit(container);+ const select = container.querySelector('select.session-selector') as HTMLSelectElement;+ await waitFor(() => assert.equal(select.value, SESSION_2.id));+ await flush();++ fireEvent.click(await screen.findByText('Carol'));+ const header = await waitFor(() => {+ const found = container.querySelector('.room-header');+ assert.ok(found, 'Carol did not open');+ return found as HTMLElement;+ });+ await within(header).findByText('Carol');++ // Dave writes; the refetch his unlisted chat triggers now lists him.+ chatsResponder = () => Promise.resolve(jsonResponse([CHAT, CHAT_2, DAVE]));+ const socket = lastSocket();+ assert.ok(socket, 'expected the page to have opened a socket');+ act(() =>+ socket.receive('message', {+ type: 'event',+ timestamp: new Date(1_700_002_000_000).toISOString(),+ payload: {+ event: 'message.received',+ sessionId: SESSION_2.id,+ data: {+ id: 'wamid.live.dave',+ chatId: DAVE.id,+ from: DAVE.id,+ to: 'me',+ body: 'dave says hi',+ type: 'text',+ fromMe: false,+ timestamp: 1_700_001_900,+ },+ },+ }),+ );+ await screen.findByText('Dave');+ await flush();+ await flush();+ const room = container.querySelector('.room-header') as HTMLElement;+ assert.equal(within(room).queryByText('Dave') === null, true, 'the old search hit opened Dave');+ } finally {+ twoSessions = false;+ }+});+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 c6e8d8a06..41c92a516 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -782,7 +782,7 @@ export function Chats() {// target chat may not be available at click time. pendingHitRef carries the intent across that// async gap: the chat-select effect picks it up once the list lands, and the scroll effect runs// once the messages have rendered.- const pendingHitRef = useRef<{ chatId: string; waMessageId: string } | null>(null);+ const pendingHitRef = useRef<{ sessionId: string; chatId: string; waMessageId: string } | null>(null);const handleSearchHit = useCallback(async (hit: SearchHit) => {@@ -804,7 +804,7 @@ export function Chats() {}setSessions(ready);}- pendingHitRef.current = { chatId: hit.chatId, waMessageId: hit.waMessageId };+ pendingHitRef.current = { sessionId: hit.sessionId, chatId: hit.chatId, waMessageId: hit.waMessageId };if (hit.sessionId !== selectedSessionId) {// Switching session triggers loadChats; the effect below selects the chat once the list lands.setSelectedSessionId(hit.sessionId);@@ -837,28 +837,35 @@ export function Chats() {// After a session switch the chats list reloads — pick up the pending chat once it appears.// While the switch's list is loading, `chats` still holds the previous session's list, which can- // list the same id (a shared group or contact) as a Chat object from the other account.+ // list the same id (a shared group or contact) as a Chat object from the other account. A hit whose+ // chat the session's list lacks, or whose session the user left, is dropped: it would otherwise open+ // on its own once a later list holds that id.useEffect(() => {const pending = pendingHitRef.current;- if (!pending || loadingChats || activeChat?.id === pending.chatId) return;+ if (!pending) return;+ if (pending.sessionId !== selectedSessionId) {+ pendingHitRef.current = null;+ return;+ }+ if (loadingChats || listedSessionRef.current !== pending.sessionId || activeChat?.id === pending.chatId) return;const chat = chats.find(c => c.id === pending.chatId);- if (chat) {- if (chat.kind === 'channel') {- switchTab('channels');- pendingHitRef.current = null;- } else if (chat.kind === 'status') {- setActiveTab('status');- setActiveChat(chat);- setActiveChannel(null);- setActiveStatusContactId(null);- } else {- setActiveTab('chats');- setActiveChat(chat);- setActiveChannel(null);- setActiveStatusContactId(null);- }+ if (!chat) {+ pendingHitRef.current = null;+ } else if (chat.kind === 'channel') {+ switchTab('channels');+ pendingHitRef.current = null;+ } else if (chat.kind === 'status') {+ setActiveTab('status');+ setActiveChat(chat);+ setActiveChannel(null);+ setActiveStatusContactId(null);+ } else {+ setActiveTab('chats');+ setActiveChat(chat);+ setActiveChannel(null);+ setActiveStatusContactId(null);}- }, [chats, loadingChats, activeChat, switchTab]);+ }, [chats, loadingChats, activeChat, selectedSessionId, switchTab]);// Best-effort scroll to the hit message. Runs as a layout effect (after useChatScrollPosition's// own restore on the same commit) so it overrides the bottom/saved jump with no visible flash.