Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): drop a search hit once the user leaves its chat

Yudhi Armyndharisf09261a99cf0
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 34beb5727..d6a2bcd90 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -2007,6 +2007,22 @@ test("a search hit whose chat the other session's list lacks does not open it la
}
});
+// The hit's thread can take seconds to load, or fail; until it renders the hit is still pending.
+test("leaving a search hit's chat before its thread loads does not reopen it", async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ searchHits = [{ ...THIRD_SESSION_HIT, sessionId: SESSION.id, chatId: CHAT.id, waMessageId: DB_MESSAGE.waMessageId! }];
+ firstPageGate = new Promise<void>(() => {});
+ const { container } = renderChats();
+ await screen.findByText('Carol');
+
+ await clickSearchHit(container);
+ await waitFor(() => assert.ok(container.querySelector('.room-header'), "the hit's chat did not open"));
+ await within(container.querySelector('.room-header') as HTMLElement).findByText('Alice');
+ fireEvent.click(within(container.querySelector('.chats-sidebar') as HTMLElement).getByText('Carol'));
+ await flush();
+ await within(container.querySelector('.room-header') as HTMLElement).findByText('Carol');
+});
+
test('a second search hit in the chat already open scrolls to it at once', async () => {
const { screen, fireEvent, within, waitFor } = rtl;
searchHits = [{ ...THIRD_SESSION_HIT, sessionId: SESSION.id, chatId: CHAT.id, waMessageId: DB_MESSAGE.waMessageId! }];
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index d2f418db4..45af4e1f7 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -805,8 +805,11 @@ export function Chats() {
// A cross-session hit switches session, which asynchronously reloads the chats list — so the
// 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<{ sessionId: string; chatId: string; waMessageId: string } | null>(null);
+ // once the messages have rendered. `opened` marks a hit whose chat has been opened, so the user
+ // leaving that chat before its thread renders drops the hit instead of being sent back into it.
+ const pendingHitRef = useRef<{ sessionId: string; chatId: string; waMessageId: string; opened?: boolean } | null>(
+ null,
+ );
// Bumped with every hit, so a hit in the chat already open still re-runs the scroll effect, where
// every other state it sets is unchanged and React skips the render.
const [hitSeq, setHitSeq] = useState(0);
@@ -831,7 +834,8 @@ export function Chats() {
}
setSessions(ready);
}
- pendingHitRef.current = { sessionId: hit.sessionId, chatId: hit.chatId, waMessageId: hit.waMessageId };
+ const pending = { sessionId: hit.sessionId, chatId: hit.chatId, waMessageId: hit.waMessageId };
+ pendingHitRef.current = pending;
setHitSeq(n => n + 1);
if (hit.sessionId !== selectedSessionId) {
// Switching session triggers loadChats; the effect below selects the chat once the list lands.
@@ -844,16 +848,12 @@ export function Chats() {
// hit's message-highlight is intentionally dropped here since that pane has no per-message scroll target.
switchTab('channels');
pendingHitRef.current = null;
- } else if (chat.kind === 'status') {
- setActiveTab('status');
- setActiveChat(chat);
- setActiveChannel(null);
- setActiveStatusContactId(null);
} else {
- setActiveTab('chats');
+ setActiveTab(chat.kind === 'status' ? 'status' : 'chats');
setActiveChat(chat);
setActiveChannel(null);
setActiveStatusContactId(null);
+ pendingHitRef.current = { ...pending, opened: true };
}
} else {
pendingHitRef.current = null;
@@ -871,7 +871,7 @@ export function Chats() {
useEffect(() => {
const pending = pendingHitRef.current;
if (!pending) return;
- if (pending.sessionId !== selectedSessionId) {
+ if (pending.sessionId !== selectedSessionId || (pending.opened && activeChat?.id !== pending.chatId)) {
pendingHitRef.current = null;
return;
}
@@ -882,16 +882,12 @@ export function Chats() {
} 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');
+ setActiveTab(chat.kind === 'status' ? 'status' : 'chats');
setActiveChat(chat);
setActiveChannel(null);
setActiveStatusContactId(null);
+ pendingHitRef.current = { ...pending, opened: true };
}
}, [chats, loadingChats, activeChat, selectedSessionId, switchTab]);