Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): open a search hit with the new session's chat

Yudhi Armyndharis3bc190528e9c
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 8fb02bb1f..ab682a17c 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -1359,6 +1359,42 @@ test('a search hit in a session that connected after the page loaded opens it',
await within(container.querySelector('.room-header') as HTMLElement).findByText('Alice');
});
+test("a search hit in another session opens that session's chat, not the one the previous list held", async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ twoSessions = true;
+ // A chat both accounts list, under a different name in each.
+ searchHits = [{ ...THIRD_SESSION_HIT, sessionId: SESSION_2.id }];
+ let releaseSecond!: () => void;
+ const secondGate = new Promise<void>(resolve => {
+ releaseSecond = resolve;
+ });
+ chatsResponder = sessionId =>
+ sessionId === SESSION.id
+ ? Promise.resolve(jsonResponse([CHAT, CHAT_2]))
+ : secondGate.then(() => jsonResponse([{ ...CHAT, name: 'Alice on two' }]));
+ try {
+ const { container } = renderChats();
+ // A room already open is what re-runs the hit's lookup while session 1's list is still held.
+ fireEvent.click(await screen.findByText('Carol'));
+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'Carol did not open'));
+
+ await clickSearchHit(container);
+ const select = container.querySelector('select.session-selector') as HTMLSelectElement;
+ await waitFor(() => assert.equal(select.value, SESSION_2.id));
+ await flush();
+ releaseSecond();
+
+ const header = await waitFor(() => {
+ const found = container.querySelector('.room-header');
+ assert.ok(found, "the hit's chat did not open");
+ return found as HTMLElement;
+ });
+ await within(header).findByText('Alice on two');
+ } 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.tsx
index 3fc561188..976864fd7 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -783,9 +783,11 @@ 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.
useEffect(() => {
const pending = pendingHitRef.current;
- if (!pending || activeChat?.id === pending.chatId) return;
+ if (!pending || loadingChats || activeChat?.id === pending.chatId) return;
const chat = chats.find(c => c.id === pending.chatId);
if (chat) {
if (chat.kind === 'channel') {
@@ -803,7 +805,7 @@ export function Chats() {
setActiveStatusContactId(null);
}
}
- }, [chats, activeChat, switchTab]);
+ }, [chats, loadingChats, activeChat, 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.