Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): drop repeated hits when global search loads more

Yudhi Armyndharis7bcf39669254
diff --git a/dashboard/src/components/GlobalSearch.test.ts b/dashboard/src/components/GlobalSearch.test.ts
index 2daf953f0..343d0f4c6 100644
--- a/dashboard/src/components/GlobalSearch.test.ts
+++ b/dashboard/src/components/GlobalSearch.test.ts
@@ -28,8 +28,9 @@ function installFetchStub(): void {
resolve(
new Response(
JSON.stringify({
- hits: texts.map((text, i) => ({
- messageId: `${q}-${offset}-${i}`,
+ // A hit's id follows its text, so a page that repeats a text repeats the message.
+ hits: texts.map(text => ({
+ messageId: `${q}-${text}`,
sessionId: 'sess-1',
chatId: '[email protected]',
timestamp: 1_767_225_600,
@@ -175,6 +176,30 @@ test('a mouse click on "more" keeps the results open and appends the next page',
assert.equal(screen.getAllByRole('option').length, 40);
});
+test('"more" does not repeat a hit that a newly indexed message pushed onto the next page', async () => {
+ const { screen, fireEvent, act, waitFor } = rtl;
+ rtl.render(createElement(GlobalSearch, { onHit: () => undefined }));
+ const input = screen.getByRole('textbox');
+ input.focus();
+ await typeAndWaitForRequest(input, 'refund');
+ await act(async () => pending.get('refund')!(page(0), 40));
+ fireEvent.click(await screen.findByRole('button', { name: more40 }));
+ await waitFor(() => assert.deepEqual(offsets, ['0', '20']));
+ // A match indexed in between shifted every hit down by one, so offset 20 starts with the old 20th.
+ await act(async () => pending.get('refund')!(page(19), 41));
+ await waitFor(() => assert.ok(screen.queryByText('refund 38')));
+ assert.equal(screen.getAllByText('refund 19').length, 1, 'the shifted hit is listed twice');
+ assert.equal(screen.getAllByRole('option').length, 39);
+ // The next page starts after the 40 rows the server has sent, not after the 39 listed, and the last
+ // row it returns ends the list: the dropped repeat must not keep "more" up for a match never fetched.
+ fireEvent.click(screen.getByRole('button', { name: /39/ }));
+ await waitFor(() => assert.deepEqual(offsets, ['0', '20', '40']));
+ await act(async () => pending.get('refund')!(['refund 39'], 41));
+ await waitFor(() => assert.ok(screen.queryByText('refund 39')));
+ assert.equal(screen.getAllByRole('option').length, 40);
+ assert.equal(screen.queryByRole('button', { name: /41/ }), null, '"more" stayed up after the last page');
+});
+
test('a failed "more" keeps the results already shown and offers the button again', async () => {
const { screen, fireEvent, act, waitFor } = rtl;
rtl.render(createElement(GlobalSearch, { onHit: () => undefined }));
diff --git a/dashboard/src/components/GlobalSearch.tsx b/dashboard/src/components/GlobalSearch.tsx
index 943b13863..08ffde767 100644
--- a/dashboard/src/components/GlobalSearch.tsx
+++ b/dashboard/src/components/GlobalSearch.tsx
@@ -19,6 +19,9 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
const [q, setQ] = useState('');
const [hits, setHits] = useState<SearchHit[]>([]);
const [total, setTotal] = useState(0);
+ // Where the next page starts on the server: hits already listed are dropped from a page, so this can
+ // run ahead of hits.length, and "more" stays up only while the server has rows past it.
+ const [nextOffset, setNextOffset] = useState(0);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [scopeCurrent, setScopeCurrent] = useState(false);
@@ -45,6 +48,7 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
if (!params) {
setHits([]);
setTotal(0);
+ setNextOffset(0);
setError(null);
setLoading(false);
return;
@@ -54,8 +58,14 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
try {
const res = await searchApi.search(params);
if (id !== requestId.current) return;
- setHits(prev => (append ? [...prev, ...res.hits] : res.hits));
+ // A message indexed since the last page shifts the offset, so the next page can repeat a hit.
+ setHits(prev => {
+ if (!append) return res.hits;
+ const seen = new Set(prev.map(h => h.messageId));
+ return [...prev, ...res.hits.filter(h => !seen.has(h.messageId))];
+ });
setTotal(res.total);
+ setNextOffset(offset + res.hits.length);
} catch (e: unknown) {
if (id !== requestId.current) return;
const status = (e as { status?: number }).status;
@@ -66,6 +76,7 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
if (!append) {
setHits([]);
setTotal(0);
+ setNextOffset(0);
}
} finally {
if (id === requestId.current) setLoading(false);
@@ -81,6 +92,7 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
requestId.current += 1;
setHits([]);
setTotal(0);
+ setNextOffset(0);
setError(null);
setLoading(false);
return;
@@ -119,7 +131,7 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
// replaced by the loading row, which would otherwise leave focus on nothing.
const loadMore = () => {
inputRef.current?.focus();
- void run(q, hits.length, true);
+ void run(q, nextOffset, true);
};
return (
@@ -193,7 +205,7 @@ export function GlobalSearch({ onHit, currentSessionId }: GlobalSearchProps) {
</div>
</button>
))}
- {!loading && hits.length < total && (
+ {!loading && nextOffset < total && (
// preventDefault on mousedown keeps focus in the input, so a mouse click does not start the
// close timer; click still fires for the mouse and for Enter/Space.
<button className="global-search-more" onMouseDown={e => e.preventDefault()} onClick={loadMore}>