Fully autonomous AI Agents system capable of performing complex penetration testing tasks
Go ai-agentsai-security-toolanthropicautonomous-agentsgolanggptgraphqlmulti-agent-systemoffensive-securityopen-sourceopenaipenetration-testingpenetration-testing-toolsreactsecurity-automationsecurity-testingsecurity-toolsself-hosted
refactor(frontend): composite globalFilter for DataTable multi-column search
Sergey Kozyrenko847c873f012c
diff --git a/frontend/src/components/ui/data-table.test.tsx b/frontend/src/components/ui/data-table.test.tsxindex ac18cda6..0433146d 100644--- a/frontend/src/components/ui/data-table.test.tsx+++ b/frontend/src/components/ui/data-table.test.tsx@@ -377,6 +377,51 @@ describe('DataTable — multi-column search', () => {expect(stored.searchColumns).toEqual(['name']);});+ it('refilters immediately when the picker disables a column that an already-typed query matched (regression for d4c1b13)', async () => {+ const user = userEvent.setup();+ render(+ <DataTable<MultiRow>+ columns={MULTI_COLUMNS}+ data={MULTI_ROWS}+ filterPlaceholder="Filter..."+ />,+ { wrapper: Wrapper },+ );++ // "admin" appears only in `role` on Alpha. Confirm the multi-column+ // search surfaces her *before* we narrow the picker.+ await user.type(screen.getByPlaceholderText('Filter...'), 'admin');+ expect(screen.getByText('Alpha')).toBeInTheDocument();++ // Now uncheck `Role`. The previous closure-based implementation kept+ // Alpha visible here because `state.globalFilter` stayed equal and+ // TanStack short-circuited the filter pipeline. The composite-value+ // implementation produces a new `globalFilter` object reference, so+ // the pipeline re-runs and Alpha must disappear without retyping.+ await user.click(screen.getByRole('button', { name: /Search in/ }));+ await user.click(await screen.findByRole('menuitemcheckbox', { name: /role/i }));++ expect(screen.queryByText('Alpha')).not.toBeInTheDocument();+ });++ it('prunes stale ids from persisted searchColumns when the candidate set shrinks', () => {+ // Pre-seed storage as if a previous version of this page exposed a+ // `deleted` column the user had selected. Today's columns no longer+ // include it — the rebase effect must trim the persisted entry.+ localStorage.setItem('table_4_/flows', JSON.stringify({ searchColumns: ['name', 'role', 'deleted'] }));++ render(+ <DataTable<MultiRow>+ columns={MULTI_COLUMNS}+ data={MULTI_ROWS}+ />,+ { wrapper: Wrapper },+ );++ const stored = JSON.parse(localStorage.getItem('table_4_/flows') ?? '{}');+ expect(stored.searchColumns).toEqual(['name', 'role']);+ });+it('activates multi-column mode when filterColumn is an array (without meta.searchable)', async () => {const user = userEvent.setup();const PLAIN_COLUMNS: ColumnDef<MultiRow>[] = [diff --git a/frontend/src/components/ui/data-table.tsx b/frontend/src/components/ui/data-table.tsxindex c3e1beee..8ba44d4f 100644--- a/frontend/src/components/ui/data-table.tsx+++ b/frontend/src/components/ui/data-table.tsx@@ -2,6 +2,7 @@ import {type Column,type ColumnDef,type ExpandedState,+ type FilterFn,flexRender,getCoreRowModel,getExpandedRowModel,@@ -11,7 +12,6 @@ import {type Table as ReactTable,type Row,type SortingState,- type Updater,useReactTable,type VisibilityState,} from '@tanstack/react-table';@@ -44,9 +44,20 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@import { useEffectAfterMount } from '@/hooks/use-effect-after-mount';import { useLatestRef } from '@/hooks/use-latest-ref';import { usePageStorageKeys } from '@/hooks/use-page-storage-keys';+import { getColumnId } from '@/lib/column-utils';import { migrateLegacyTableState, updateTableState } from '@/lib/table-state';import { cn } from '@/lib/utils';+/**+ * Composite value stored in TanStack's `state.globalFilter`. Bundling `query`+ * and the active `columns` set together makes the predicate self-contained:+ * any change to either field produces a new object reference, which is what+ * TanStack watches to re-run the filter pipeline. This sidesteps the trap+ * where a closure-only narrowing (`getColumnCanGlobalFilter`) updates silently+ * and the rows stay stale.+ */+type DataTableGlobalFilter = { columns: string[]; query: string };+interface DataTableProps<TData, TValue = unknown> {columns: ColumnDef<TData, TValue>[];columnVisibility?: VisibilityState;@@ -97,14 +108,14 @@ interface DataTableFilterProps<TData> {}/**- * Search input bound to TanStack's `state.globalFilter`. Reads/writes through- * `table.setGlobalFilter` exclusively — `DataTable`'s `onGlobalFilterChange`- * funnels the write to the parent (`onFilterChange`) when the table is in- * controlled-filter mode, so this component stays uniform regardless of- * single- vs multi-column mode.+ * Search input bound to TanStack's `state.globalFilter`. Reads `.query` out of+ * the composite filter value and writes back a raw string — the normalising+ * `onGlobalFilterChange` handler upstream takes care of folding it back into+ * the composite shape (and forwarding the parent-visible string in+ * controlled mode).*/const DataTableFilter = <TData,>({ placeholder, table }: DataTableFilterProps<TData>) => {- const filterValue = (table.getState().globalFilter as string | undefined) ?? '';+ const filterValue = (table.getState().globalFilter as DataTableGlobalFilter | undefined)?.query ?? '';return (<InputGroup className="max-w-sm">@@ -232,22 +243,31 @@ function DataTable<TData, TValue = unknown>({return columns.filter((column) => column.meta?.searchable === true)- .map((column) => {- const withId = column as { id?: string };-- if (withId.id) {- return withId.id;- }-- const withAccessor = column as { accessorKey?: string };-- return typeof withAccessor.accessorKey === 'string' ? withAccessor.accessorKey : undefined;- })+ .map(getColumnId).filter((id): id is string => typeof id === 'string');}, [columns, filterColumn]);const isMultiMode = Array.isArray(filterColumn) || (filterColumn === undefined && searchCandidateIds.length > 0);+ // Rebase `searchColumns` whenever the set of candidates changes (column+ // reconfiguration, HMR, or a hydrated localStorage entry that references+ // ids the current page no longer exposes). The empty-array sentinel — our+ // "search everywhere" state — is preserved as-is; non-empty selections are+ // pruned to the still-valid intersection. Return `prev` unchanged when the+ // intersection equals the input so we don't trigger a spurious storage+ // write through the persistence effect below.+ useEffect(() => {+ setSearchColumns((prev) => {+ if (prev.length === 0) {+ return prev;+ }++ const filtered = prev.filter((id) => searchCandidateIds.includes(id));++ return filtered.length === prev.length ? prev : filtered;+ });+ }, [searchCandidateIds]);+// Track which tableKey we've already migrated + seeded from. When the// key rotates (route change inside a persistent layout, or an explicit// override) we re-run the migration for the new path and refresh local@@ -275,32 +295,59 @@ function DataTable<TData, TValue = unknown>({}));}, [initialPageSize, initialSorting, isColumnVisibilityControlled, pathname, tableKey]);- // Effective global filter: controlled-mode parents own the value via- // `useTableQueryFilter` (URL `?q=`), uncontrolled tables drive it from- // internal state. Either way TanStack reads from `state.globalFilter`- // exclusively.- const effectiveGlobalFilter = isFilterControlled ? (externalFilterValue ?? '') : internalGlobalFilter;+ // Compose the query and the active column set into a single TanStack+ // state value. New object identity on any change is exactly what TanStack+ // watches to re-run the filter pipeline — no imperative `setGlobalFilter`+ // pokes, no closure-only narrowing that updates silently. The `columns`+ // field follows the same "empty selection = search everywhere" sentinel+ // as `searchColumns`, resolved once here for the predicate's convenience.+ const effectiveQuery = isFilterControlled ? (externalFilterValue ?? '') : internalGlobalFilter;++ const effectiveGlobalFilter = useMemo<DataTableGlobalFilter>(+ () => ({+ columns: searchColumns.length > 0 ? searchColumns : searchCandidateIds,+ query: effectiveQuery,+ }),+ [effectiveQuery, searchCandidateIds, searchColumns],+ );- const externalFilterValueReference = useLatestRef(externalFilterValue);+ const effectiveGlobalFilterReference = useLatestRef(effectiveGlobalFilter);- // Funnel every TanStack global-filter change through the right sink:- // parent callback in controlled mode, internal state otherwise. The- // updater signature mirrors TanStack's own — we resolve the function- // form against the latest external value before calling `onFilterChange`,- // so the parent always sees the next string, not a function.+ // Funnel every TanStack global-filter change through the right sink. The+ // updater arrives in three shapes:+ // * a raw string from `DataTableFilter` (input.onChange → setGlobalFilter)+ // * a function `(prev: DataTableGlobalFilter) => DataTableGlobalFilter`+ // from any TanStack-internal mutation (e.g. `table.resetGlobalFilter`)+ // * a bare composite object from a programmatic write+ // We resolve it down to the `query` string at the boundary — controlled+ // parents and internal state both speak strings.const handleGlobalFilterChange = useCallback(- (updater: Updater<string>) => {- if (!isFilterControlled) {- setInternalGlobalFilter(updater);+ (updater: unknown) => {+ const resolveQuery = (value: unknown): string => {+ if (typeof value === 'string') {+ return value;+ }- return;- }+ if (value && typeof value === 'object' && 'query' in value) {+ return String((value as { query: unknown }).query ?? '');+ }++ return '';+ };- const previous = externalFilterValueReference.current ?? '';- const next = typeof updater === 'function' ? updater(previous) : updater;- onFilterChange?.(next);+ const nextRaw =+ typeof updater === 'function'+ ? (updater as (previous: DataTableGlobalFilter) => unknown)(effectiveGlobalFilterReference.current)+ : updater;+ const nextQuery = resolveQuery(nextRaw);++ if (isFilterControlled) {+ onFilterChange?.(nextQuery);+ } else {+ setInternalGlobalFilter(nextQuery);+ }},- [externalFilterValueReference, isFilterControlled, onFilterChange],+ [effectiveGlobalFilterReference, isFilterControlled, onFilterChange],);// Persist sorting + column visibility + page size into the unified@@ -394,29 +441,29 @@ function DataTable<TData, TValue = unknown>({[handlePaginationChange],);- // Active column set for the global filter: when the user has narrowed via- // the picker, honour the explicit list; otherwise fall back to all- // candidates ("empty selection = search everywhere"). Bake the predicate- // into per-column `enableGlobalFilter` so TanStack re-evaluates the row- // model whenever the set changes — a `getColumnCanGlobalFilter` closure- // alone would update silently because `state.globalFilter` stays equal.- const tanstackColumns = useMemo<ColumnDef<TData, TValue>[]>(() => {- const active = searchColumns.length > 0 ? searchColumns : searchCandidateIds;-- return columns.map((column) => {- const withId = column as { id?: string };- const withAccessor = column as { accessorKey?: string };- const columnId =- withId.id ?? (typeof withAccessor.accessorKey === 'string' ? withAccessor.accessorKey : undefined);- const enableGlobalFilter = typeof columnId === 'string' && active.includes(columnId);-- return { ...column, enableGlobalFilter };- });- }, [columns, searchCandidateIds, searchColumns]);+ // Case-insensitive substring predicate that consults the composite+ // filter for both "what to look for" and "where to look". Returning+ // `true` for the empty query keeps the default "no filter = all rows"+ // behaviour identical to TanStack's built-in `includesString`.+ const globalFilterFn = useCallback<FilterFn<TData>>((row, columnId, filter: DataTableGlobalFilter) => {+ if (!filter.query) {+ return true;+ }++ if (!filter.columns.includes(columnId)) {+ return false;+ }++ const value = row.getValue(columnId);++ return String(value ?? '')+ .toLowerCase()+ .includes(filter.query.toLowerCase());+ }, []);const table = useReactTable({autoResetPageIndex: false,- columns: tanstackColumns,+ columns,data,enableSortingRemoval: true,getCoreRowModel: getCoreRowModel(),@@ -424,7 +471,7 @@ function DataTable<TData, TValue = unknown>({getFilteredRowModel: getFilteredRowModel(),getPaginationRowModel: getPaginationRowModel(),getSortedRowModel: getSortedRowModel(),- globalFilterFn: 'includesString',+ globalFilterFn,onColumnVisibilityChange: handleColumnVisibilityChange,onExpandedChange: setExpanded,onGlobalFilterChange: handleGlobalFilterChange,diff --git a/frontend/src/lib/column-utils.ts b/frontend/src/lib/column-utils.tsnew file mode 100644index 00000000..1ec72dcd--- /dev/null+++ b/frontend/src/lib/column-utils.ts@@ -0,0 +1,20 @@+import type { ColumnDef } from '@tanstack/react-table';++/**+ * Resolve a TanStack `ColumnDef`'s id the way TanStack does internally:+ * explicit `id` wins, then `accessorKey` when it's a plain string. Display+ * columns and `accessorFn` columns without an explicit id resolve to+ * `undefined` — callers must filter those out before passing the id to+ * APIs that require one (e.g. `getColumn`, predicate matching).+ */+export const getColumnId = <TData, TValue>(column: ColumnDef<TData, TValue>): string | undefined => {+ const withId = column as { id?: string };++ if (withId.id) {+ return withId.id;+ }++ const withAccessor = column as { accessorKey?: string };++ return typeof withAccessor.accessorKey === 'string' ? withAccessor.accessorKey : undefined;+};