Skip to content

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

refactor(frontend): inline cycleColumnSort into data-table.tsx

Sergey Kozyrenko33dba9dbf4ab
diff --git a/frontend/src/components/shared/README.md b/frontend/src/components/shared/README.md
index 233530eb..7cd42537 100644
--- a/frontend/src/components/shared/README.md
+++ b/frontend/src/components/shared/README.md
@@ -62,7 +62,6 @@ that every list reuses.
| ------------------------- | ------------------------------------------------------------------------------------ |
| `table-filter.ts` | `createTextMatcher` — case + diacritic-insensitive substring matcher. |
| `table-state.ts` | Unified `table_4_<path>` JSON slot. Carries filter + sorting + columnVis + pageSize. |
-| `table-sort.ts` | `cycleColumnSort` — pure none/asc/desc cycle for TanStack columns. |
| `view-options-storage.ts` | `viewOptions_4_<path>` for FileManager-style screens (folders-first, etc.). |
| `storage-keys.ts` | Single source of truth for storage-key conventions and `getTopLevelPath`. |
| `url-params.ts` | `URL_PARAMS` constants + `mergeHrefWithSearchParams` (preserves hash on merge). |
@@ -145,7 +144,7 @@ and deletes them.
## Testing notes
- `vitest run` covers the pure utilities (`table-filter`, `table-state`,
- `view-options-storage`, `url-params`, `table-sort`), the hook behaviours
+ `view-options-storage`, `url-params`), the hook behaviours
(`use-pagination`, `use-table-query-filter`, `use-inline-edit`,
`use-page-storage-keys`, `use-detail-navigation`), and the components
(`detail-navigation/`, `data-table`).
diff --git a/frontend/src/components/ui/data-table.test.tsx b/frontend/src/components/ui/data-table.test.tsx
index e37f41b4..43e6abc1 100644
--- a/frontend/src/components/ui/data-table.test.tsx
+++ b/frontend/src/components/ui/data-table.test.tsx
@@ -1,4 +1,4 @@
-import type { ColumnDef } from '@tanstack/react-table';
+import type { Column, ColumnDef } from '@tanstack/react-table';
import type { ReactNode } from 'react';
import { fireEvent, render, screen, within } from '@testing-library/react';
@@ -8,7 +8,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { TooltipProvider } from '@/components/ui/tooltip';
-import { DataTable } from './data-table';
+import { cycleColumnSort, DataTable } from './data-table';
interface Row {
id: string;
@@ -294,3 +294,51 @@ describe('DataTable — empty results', () => {
expect(input).toHaveAttribute('id', 'data-table-filter-name');
});
});
+
+interface FakeColumn {
+ clearSorting: ReturnType<typeof vi.fn>;
+ getIsSorted: ReturnType<typeof vi.fn>;
+ toggleSorting: ReturnType<typeof vi.fn>;
+}
+
+// Stub the subset of `Column` `cycleColumnSort` actually touches. The cast
+// through `unknown as Column<...>` keeps the test free of @tanstack/react-table
+// internals while preserving the typed call signature.
+const makeColumn = (sorted: 'asc' | 'desc' | false): FakeColumn => ({
+ clearSorting: vi.fn(),
+ getIsSorted: vi.fn().mockReturnValue(sorted),
+ toggleSorting: vi.fn(),
+});
+
+describe('cycleColumnSort', () => {
+ it('starts the cycle on a column with no sort by setting ascending', () => {
+ const column = makeColumn(false);
+ cycleColumnSort(column as unknown as Column<unknown>);
+
+ expect(column.toggleSorting).toHaveBeenCalledWith(false);
+ expect(column.clearSorting).not.toHaveBeenCalled();
+ });
+
+ it('advances ascending to descending', () => {
+ const column = makeColumn('asc');
+ cycleColumnSort(column as unknown as Column<unknown>);
+
+ expect(column.toggleSorting).toHaveBeenCalledWith(true);
+ expect(column.clearSorting).not.toHaveBeenCalled();
+ });
+
+ it('clears the sort after descending — the third tap removes the sort entirely', () => {
+ const column = makeColumn('desc');
+ cycleColumnSort(column as unknown as Column<unknown>);
+
+ expect(column.clearSorting).toHaveBeenCalledTimes(1);
+ expect(column.toggleSorting).not.toHaveBeenCalled();
+ });
+
+ it('reads `getIsSorted` exactly once per call — no double-evaluation between branches', () => {
+ const column = makeColumn('asc');
+ cycleColumnSort(column as unknown as Column<unknown>);
+
+ expect(column.getIsSorted).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/frontend/src/components/ui/data-table.tsx b/frontend/src/components/ui/data-table.tsx
index 2cb3217c..ec0ceaf2 100644
--- a/frontend/src/components/ui/data-table.tsx
+++ b/frontend/src/components/ui/data-table.tsx
@@ -44,7 +44,6 @@ 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 { cycleColumnSort } from '@/lib/table-sort';
import { migrateLegacyTableState, updateTableState } from '@/lib/table-state';
import { cn } from '@/lib/utils';
@@ -143,6 +142,31 @@ interface DataTableColumnHeaderProps<TData, TValue> {
title: ReactNode;
}
+/**
+ * Cycle a TanStack column through `none → asc → desc → none`. Pure with
+ * respect to React (no hooks called) so a header `onClick` can invoke it
+ * directly without `useCallback`/`useMemo` ceremony. Exported alongside
+ * {@link DataTableColumnHeader} so a custom header can drive the same
+ * sort cycle without duplicating the if/else.
+ */
+export function cycleColumnSort<TData, TValue = unknown>(column: Column<TData, TValue>): void {
+ const sorted = column.getIsSorted();
+
+ if (sorted === 'asc') {
+ column.toggleSorting(true);
+
+ return;
+ }
+
+ if (sorted === 'desc') {
+ column.clearSorting();
+
+ return;
+ }
+
+ column.toggleSorting(false);
+}
+
function DataTable<TData, TValue = unknown>({
columns,
columnVisibility: externalColumnVisibility,
diff --git a/frontend/src/lib/table-sort.test.ts b/frontend/src/lib/table-sort.test.ts
deleted file mode 100644
index 6d41edc8..00000000
--- a/frontend/src/lib/table-sort.test.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-import type { Column } from '@tanstack/react-table';
-
-import { describe, expect, it, vi } from 'vitest';
-
-import { cycleColumnSort } from './table-sort';
-
-interface FakeColumn {
- clearSorting: ReturnType<typeof vi.fn>;
- getIsSorted: ReturnType<typeof vi.fn>;
- toggleSorting: ReturnType<typeof vi.fn>;
-}
-
-// Build a stub that satisfies the subset of `Column` we actually call. Using
-// `unknown as Column<...>` keeps the test free of @tanstack/react-table
-// internals while still letting us pass the stub to the typed function.
-const makeColumn = (sorted: 'asc' | 'desc' | false): FakeColumn => ({
- clearSorting: vi.fn(),
- getIsSorted: vi.fn().mockReturnValue(sorted),
- toggleSorting: vi.fn(),
-});
-
-describe('cycleColumnSort', () => {
- it('starts the cycle on a column with no sort by setting ascending', () => {
- const column = makeColumn(false);
- cycleColumnSort(column as unknown as Column<unknown>);
-
- expect(column.toggleSorting).toHaveBeenCalledWith(false);
- expect(column.clearSorting).not.toHaveBeenCalled();
- });
-
- it('advances ascending to descending', () => {
- const column = makeColumn('asc');
- cycleColumnSort(column as unknown as Column<unknown>);
-
- expect(column.toggleSorting).toHaveBeenCalledWith(true);
- expect(column.clearSorting).not.toHaveBeenCalled();
- });
-
- it('clears the sort after descending — the third tap removes the sort entirely', () => {
- const column = makeColumn('desc');
- cycleColumnSort(column as unknown as Column<unknown>);
-
- expect(column.clearSorting).toHaveBeenCalledTimes(1);
- expect(column.toggleSorting).not.toHaveBeenCalled();
- });
-
- it('reads `getIsSorted` exactly once per call — no double-evaluation between branches', () => {
- const column = makeColumn('asc');
- cycleColumnSort(column as unknown as Column<unknown>);
-
- expect(column.getIsSorted).toHaveBeenCalledTimes(1);
- });
-});
diff --git a/frontend/src/lib/table-sort.ts b/frontend/src/lib/table-sort.ts
deleted file mode 100644
index b7ee4b43..00000000
--- a/frontend/src/lib/table-sort.ts
+++ /dev/null
@@ -1,31 +0,0 @@
-import type { Column } from '@tanstack/react-table';
-
-/**
- * Cycle a TanStack Table column through the three-state sort order
- * `none → asc → desc → none`. Mirrors the header behaviour of `DataTable`
- * and the FileManager sortable headers.
- *
- * Pure with respect to React (does not call any hook), so it can be invoked
- * directly from a header `onClick` without wrapping in `useCallback`/`useMemo`.
- *
- * Generic over the row type so `column` keeps its full TanStack typing at the
- * call site — no need to inline a structural subset like
- * `{ getIsSorted; toggleSorting; clearSorting }` in every page.
- */
-export const cycleColumnSort = <TData, TValue = unknown>(column: Column<TData, TValue>): void => {
- const sorted = column.getIsSorted();
-
- if (sorted === 'asc') {
- column.toggleSorting(true);
-
- return;
- }
-
- if (sorted === 'desc') {
- column.clearSorting();
-
- return;
- }
-
- column.toggleSorting(false);
-};