Skip to content

Fully autonomous AI Agents system capable of performing complex penetration testing tasks

fix(frontend): kill X-clear race that resurrected the filter query

Sergey Kozyrenko4a83b29c3d2f
diff --git a/frontend/src/components/ui/data-table.test.tsx b/frontend/src/components/ui/data-table.test.tsx
index d634a35f..8dd2c6ce 100644
--- a/frontend/src/components/ui/data-table.test.tsx
+++ b/frontend/src/components/ui/data-table.test.tsx
@@ -3,6 +3,7 @@ import type { ReactNode } from 'react';
import { act, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import { useState } from 'react';
import { MemoryRouter } from 'react-router-dom';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -135,6 +136,64 @@ describe('DataTable — controlled filter projection', () => {
expect(onFilterChange).toHaveBeenCalledWith('');
});
+
+ it('does not resurrect the previous query after the X clears past a settled debounce', async () => {
+ // Regression for the X-clear race: parents typically funnel
+ // `onFilterChange` through an inline arrow (`(v) => setUrl(v)`), which
+ // is a fresh reference per render. The emit effect inside the filter
+ // input used to list that handler in its deps; combined with
+ // `useDebouncedValue` (a `useState` that lags behind `localValue` by
+ // a `setTimeout`), the very next parent render after `handleClear`
+ // saw `debouncedValue='alpha'` while `lastEmitted=''` and re-emitted
+ // `'alpha'` — the user saw the URL/value snap back briefly.
+ const emitted: string[] = [];
+ const user = userEvent.setup();
+
+ const Host = () => {
+ const [value, setValue] = useState('');
+
+ return (
+ <DataTable<Row>
+ columns={COLUMNS}
+ data={ROWS}
+ filterColumn="name"
+ filterPlaceholder="Filter..."
+ filterValue={value}
+ // Intentionally a fresh inline arrow per render —
+ // mirrors how pages wire this up.
+ onFilterChange={(next) => {
+ emitted.push(next);
+ setValue(next);
+ }}
+ />
+ );
+ };
+
+ render(<Host />, { wrapper: Wrapper });
+
+ const input = screen.getByPlaceholderText('Filter...');
+ await user.type(input, 'alpha');
+
+ // Wait until the debounce has flushed and the parent has committed
+ // 'alpha' upstream. From here the bug needs `debouncedValue` to be
+ // stuck at the old value across the clear+next-render boundary.
+ await waitFor(() => expect(emitted.at(-1)).toBe('alpha'));
+
+ const inputGroup = input.closest('[data-slot="input-group"]');
+ const clearButton = within(inputGroup as HTMLElement).getByRole('button');
+
+ await user.click(clearButton);
+
+ // Give the debounce timer and any follow-up effects plenty of time
+ // to misfire. With the fix in place no further 'alpha' emission
+ // should appear; only the trailing '' clear is expected.
+ await new Promise((resolve) => setTimeout(resolve, 400));
+
+ const after = emitted.slice(emitted.indexOf('alpha') + 1);
+ expect(after).not.toContain('alpha');
+ expect(after.at(-1)).toBe('');
+ expect((input as HTMLInputElement).value).toBe('');
+ });
});
describe('DataTable — uncontrolled filter is still routed through the same input', () => {
diff --git a/frontend/src/components/ui/data-table.tsx b/frontend/src/components/ui/data-table.tsx
index 0e83a5b2..d8256306 100644
--- a/frontend/src/components/ui/data-table.tsx
+++ b/frontend/src/components/ui/data-table.tsx
@@ -953,6 +953,17 @@ function DataTableFilter({ onQueryChange, placeholder, query }: DataTableFilterP
const [localValue, setLocalValue] = useState(query);
const debouncedValue = useDebouncedValue(localValue, FILTER_DEBOUNCE_MS);
const lastEmittedReference = useRef(query);
+ // Stash the handler in a ref so the emit effect can depend on
+ // `debouncedValue` alone. Parents typically pass an inline arrow
+ // (`onQueryChange={(v) => table.setGlobalFilter(v)}`), which is a fresh
+ // reference each render. If the effect listed it in deps, the X-button
+ // clear race resurrected the previous query: handleClear sets
+ // `lastEmitted=''` synchronously, but `debouncedValue` is `useState`
+ // inside `useDebouncedValue` and stays at the old value until its
+ // own setTimeout fires — so the very next parent render (with a new
+ // inline `onQueryChange`) triggered the effect, saw
+ // `debouncedValue='jwt' !== lastEmitted=''`, and re-emitted `'jwt'`.
+ const onQueryChangeReference = useLatestRef(onQueryChange);
// Generated per-instance so pages with multiple DataTables (e.g.
// /settings/prompts) don't end up with duplicate `id` attributes — that
// breaks `getElementById`, a11y semantics, and any test selector that
@@ -969,15 +980,15 @@ function DataTableFilter({ onQueryChange, placeholder, query }: DataTableFilterP
useEffect(() => {
if (debouncedValue !== lastEmittedReference.current) {
lastEmittedReference.current = debouncedValue;
- onQueryChange(debouncedValue);
+ onQueryChangeReference.current(debouncedValue);
}
- }, [debouncedValue, onQueryChange]);
+ }, [debouncedValue, onQueryChangeReference]);
const handleClear = useCallback(() => {
setLocalValue('');
lastEmittedReference.current = '';
- onQueryChange('');
- }, [onQueryChange]);
+ onQueryChangeReference.current('');
+ }, [onQueryChangeReference]);
return (
<InputGroup className="max-w-sm">
diff --git a/frontend/src/components/ui/input-search.tsx b/frontend/src/components/ui/input-search.tsx
index 6d36118c..0c5e56c2 100644
--- a/frontend/src/components/ui/input-search.tsx
+++ b/frontend/src/components/ui/input-search.tsx
@@ -6,6 +6,7 @@ import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from '
import { Kbd, KbdGroup } from '@/components/ui/kbd';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { useDebouncedValue } from '@/hooks/use-debounced-value';
+import { useLatestRef } from '@/hooks/use-latest-ref';
import { cn } from '@/lib/utils';
import { isMac } from '@/lib/utils/platform';
@@ -80,6 +81,13 @@ export function InputSearch({
// round-trip arrived" from "the parent reset me from elsewhere" without
// a race.
const lastEmittedReference = useRef(searchQuery);
+ // Mirror onto a ref so the emit effect can depend on `debouncedLocalValue`
+ // alone. Parents commonly pass an inline arrow; listing it in deps would
+ // re-run the emit effect on every parent render with a stale
+ // `debouncedLocalValue` (still holding the pre-clear value), letting a
+ // synchronous Esc / programmatic clear race the debounce timer and
+ // resurrect the previous query. See same fix in `DataTableFilter`.
+ const onSearchChangeReference = useLatestRef(onSearchChange);
// External → local sync. The parent owns the source of truth (URL,
// upstream state, etc.); when *they* change it (Esc clear, programmatic
@@ -99,9 +107,9 @@ export function InputSearch({
useEffect(() => {
if (debouncedLocalValue !== lastEmittedReference.current) {
lastEmittedReference.current = debouncedLocalValue;
- onSearchChange(debouncedLocalValue);
+ onSearchChangeReference.current(debouncedLocalValue);
}
- }, [debouncedLocalValue, onSearchChange]);
+ }, [debouncedLocalValue, onSearchChangeReference]);
const expand = useCallback(() => setIsExpanded(true), []);
@@ -211,7 +219,7 @@ export function InputSearch({
if (localValue.length > 0) {
setLocalValue('');
lastEmittedReference.current = '';
- onSearchChange('');
+ onSearchChangeReference.current('');
return;
}
@@ -219,7 +227,7 @@ export function InputSearch({
inputRef.current?.blur();
setIsExpanded(false);
},
- [localValue, onSearchChange],
+ [localValue, onSearchChangeReference],
);
return (