Skip to content

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

fix(frontend): preserve URL params across batched setSearchParams updates

Sergey Kozyrenko019ddcb1a738
diff --git a/frontend/src/hooks/use-pagination.ts b/frontend/src/hooks/use-pagination.ts
index f434473a..85402a22 100644
--- a/frontend/src/hooks/use-pagination.ts
+++ b/frontend/src/hooks/use-pagination.ts
@@ -1,4 +1,4 @@
-import { useCallback, useEffect, useMemo } from 'react';
+import { useCallback, useEffect, useMemo, useRef } from 'react';
import { useSearchParams } from 'react-router-dom';
import { URL_PARAMS } from '@/lib/url-params';
@@ -83,19 +83,37 @@ export const usePagination = ({ paramName = URL_PARAMS.PAGE }: UsePaginationOpti
);
}, [paramName, searchParams, setSearchParams]);
+ // See `use-table-query-filter.ts` for the full rationale — render-time
+ // mutation is safe because the ref is only read from event callbacks
+ // (`setPage`), never during render. We deliberately skip `useLatestRef`
+ // because its passive-`useEffect` sync lags by one commit, which is
+ // exactly the gap that lets a batched filter+page race lose state.
+ const searchParamsReference = useRef(searchParams);
+ // eslint-disable-next-line react-hooks/refs
+ searchParamsReference.current = searchParams;
+
const setPage = useCallback(
(newPageIndex: number) => {
- setSearchParams((previous) => {
- const next = new URLSearchParams(previous);
+ // Avoid the functional `(previous) => ...` form: react-router v6
+ // feeds the same pre-batch snapshot to every functional updater
+ // queued in a single tick, so a `setFilter` + `setPage` pair
+ // (e.g. debounced filter commit landing alongside a paging click)
+ // would collapse — the second write erases the first. Prefer
+ // `window.location.search` under `BrowserRouter`; fall back to
+ // the latest committed `searchParams` (via ref) under
+ // `MemoryRouter`, which doesn't sync `window.location`.
+ const fromWindow = typeof window !== 'undefined' ? window.location.search : '';
+ const next = fromWindow
+ ? new URLSearchParams(fromWindow)
+ : new URLSearchParams(searchParamsReference.current);
- if (newPageIndex <= 0) {
- next.delete(paramName);
- } else {
- next.set(paramName, String(newPageIndex + 1));
- }
+ if (newPageIndex <= 0) {
+ next.delete(paramName);
+ } else {
+ next.set(paramName, String(newPageIndex + 1));
+ }
- return next;
- });
+ setSearchParams(next);
},
[paramName, setSearchParams],
);
diff --git a/frontend/src/hooks/use-table-query-filter.ts b/frontend/src/hooks/use-table-query-filter.ts
index 01ff3d7c..e7896857 100644
--- a/frontend/src/hooks/use-table-query-filter.ts
+++ b/frontend/src/hooks/use-table-query-filter.ts
@@ -104,7 +104,7 @@ export const useTableQueryFilterReader = (
*/
export const useTableQueryFilter = (options: UseTableQueryFilterOptions = {}): UseTableQueryFilterResult => {
const { clearPageParamOnChange = true, paramName = URL_PARAMS.QUERY, storageKey: explicitStorageKey } = options;
- const [, setSearchParams] = useSearchParams();
+ const [searchParams, setSearchParams] = useSearchParams();
const { table: defaultStorageKey } = usePageStorageKeys();
const storageKey = explicitStorageKey ?? defaultStorageKey;
@@ -177,26 +177,48 @@ export const useTableQueryFilter = (options: UseTableQueryFilterOptions = {}): U
updateTableState(storageKey, { filter: filter.length > 0 ? filter : undefined });
}, [filter, storageKey]);
+ // Sync a ref to the latest committed `searchParams` on every render.
+ // Used as a fallback below when `window.location` isn't a faithful
+ // mirror of the router state — that happens under `MemoryRouter`,
+ // which is what our hook tests use. Updating the ref in render is safe
+ // because we never read it during render; we only read it inside event
+ // callbacks (`setFilter` below). `useEffect`-synced refs (see
+ // `useLatestRef`) lag one commit, which is fatal here: two updates
+ // fired in the same tick must both see the same authoritative latest
+ // value, not the prior-render snapshot.
+ const searchParamsReference = useRef(searchParams);
+ // eslint-disable-next-line react-hooks/refs
+ searchParamsReference.current = searchParams;
+
const setFilter = useCallback(
(value: string) => {
- setSearchParams(
- (previous) => {
- const next = new URLSearchParams(previous);
-
- if (value.length === 0) {
- next.delete(paramName);
- } else {
- next.set(paramName, value);
- }
-
- if (clearPageParamOnChange) {
- next.delete(URL_PARAMS.PAGE);
- }
-
- return next;
- },
- { replace: true },
- );
+ // Avoid the functional `(previous) => ...` form of
+ // `setSearchParams`. When two updates fire in the same React tick
+ // (e.g. a debounced filter commit landing alongside a paging
+ // button click), react-router v6 feeds **both** updaters the same
+ // pre-batch snapshot — the second write erases the first, so
+ // `?q=foo` plus `setPage(5)` collapses to `?page=6` and `q` is
+ // lost. `window.location.search` is the freshest source under
+ // `BrowserRouter` (production) and immune to that batching race;
+ // under `MemoryRouter` (tests) the in-memory history is *not*
+ // synced to `window.location`, so we fall back to the ref-stashed
+ // latest react-router snapshot.
+ const fromWindow = typeof window !== 'undefined' ? window.location.search : '';
+ const next = fromWindow
+ ? new URLSearchParams(fromWindow)
+ : new URLSearchParams(searchParamsReference.current);
+
+ if (value.length === 0) {
+ next.delete(paramName);
+ } else {
+ next.set(paramName, value);
+ }
+
+ if (clearPageParamOnChange) {
+ next.delete(URL_PARAMS.PAGE);
+ }
+
+ setSearchParams(next, { replace: true });
},
[clearPageParamOnChange, paramName, setSearchParams],
);