Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): scroll to a search hit in the chat already open

Yudhi Armyndharis6a11b625ad64
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 153cb8f85..6eb1488f7 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -1881,6 +1881,32 @@ test("a search hit whose chat the other session's list lacks does not open it la
}
});
+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! }];
+ const scrolled: Element[] = [];
+ const original = Element.prototype.scrollIntoView;
+ Element.prototype.scrollIntoView = function (this: Element) {
+ scrolled.push(this);
+ };
+ try {
+ const { container } = renderChats();
+ fireEvent.click(await screen.findByText('Alice'));
+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
+ const toHit = () => scrolled.filter(el => el.getAttribute('data-wa-message-id') === DB_MESSAGE.waMessageId);
+
+ await clickSearchHit(container);
+ await waitFor(() => assert.equal(toHit().length, 1, 'the first hit did not scroll'));
+ // The same hit again, after the user scrolled away: the chat is open, so nothing else changes.
+ fireEvent.click(screen.getByLabelText('Search messages…'));
+ fireEvent.click(await waitFor(() => container.querySelector('.global-search-hit') as HTMLElement));
+ await flush();
+ assert.equal(toHit().length, 2, 'the second hit did not scroll');
+ } finally {
+ Element.prototype.scrollIntoView = original;
+ }
+});
+
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 41c92a516..d7eb4d9b4 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -783,6 +783,9 @@ export function Chats() {
// 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);
+ // 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);
const handleSearchHit = useCallback(
async (hit: SearchHit) => {
@@ -805,6 +808,7 @@ export function Chats() {
setSessions(ready);
}
pendingHitRef.current = { sessionId: hit.sessionId, chatId: hit.chatId, waMessageId: hit.waMessageId };
+ setHitSeq(n => n + 1);
if (hit.sessionId !== selectedSessionId) {
// Switching session triggers loadChats; the effect below selects the chat once the list lands.
setSelectedSessionId(hit.sessionId);
@@ -885,7 +889,7 @@ export function Chats() {
}
}
pendingHitRef.current = null;
- }, [activeChat, loadingMessages, messages, messagesContainerRef]);
+ }, [activeChat, loadingMessages, messages, messagesContainerRef, hitSeq]);
// Helper formats
const formatChatTime = useCallback(