import { startTransition, useCallback, useMemo, useRef, useState } from 'react'; export type ClientSortDir = 'asc' | 'desc'; type SortState = { key: K | null; dir: ClientSortDir; }; function toSortableNumber(value: unknown): number | null { if (value == null || value === '') return null; if (typeof value === 'number') return Number.isFinite(value) ? value : null; if (typeof value === 'boolean') return value ? 1 : 0; const n = Number(value); return Number.isFinite(n) ? n : null; } /** Pure sort: precompute compare values once per row (O(n)), then sort. Stable on ties. */ export function applyClientSort( rows: T[], sortKey: K | null, sortDir: ClientSortDir, getValue: (row: T, key: K) => unknown ): T[] { if (!sortKey || !rows.length) return rows; const dir = sortDir === 'asc' ? 1 : -1; const decorated = rows.map((row, index) => ({ row, index, value: toSortableNumber(getValue(row, sortKey)), })); decorated.sort((a, b) => { const na = a.value; const nb = b.value; if (na == null && nb == null) return a.index - b.index; if (na == null) return 1; if (nb == null) return -1; if (na === nb) return a.index - b.index; return na < nb ? -dir : dir; }); return decorated.map((d) => d.row); } /** * Client-side sort for the currently loaded table rows (scheme A). * Click cycle on the same column: asc → desc → clear (original order). * Null / non-numeric values sort last when sorting is active. */ export function useClientTableSort( initialKey: K | null = null, initialDir: ClientSortDir = 'asc' ) { const [state, setState] = useState>({ key: initialKey, dir: initialDir, }); const cacheRef = useRef<{ rows: readonly unknown[]; key: K | null; dir: ClientSortDir; result: unknown[]; } | null>(null); const toggle = useCallback((key: K) => { startTransition(() => { setState((prev) => { if (prev.key !== key) return { key, dir: 'asc' }; if (prev.dir === 'asc') return { key, dir: 'desc' }; return { key: null, dir: 'asc' }; }); }); }, []); const sortRows = useCallback( (rows: T[], getValue: (row: T, key: K) => unknown): T[] => { if (!state.key || !rows.length) { cacheRef.current = null; return rows; } const cached = cacheRef.current; if ( cached && cached.rows === rows && cached.key === state.key && cached.dir === state.dir ) { return cached.result as T[]; } const result = applyClientSort(rows, state.key, state.dir, getValue); cacheRef.current = { rows: rows as readonly unknown[], key: state.key, dir: state.dir, result, }; return result; }, [state.dir, state.key] ); return { sortKey: state.key, sortDir: state.dir, toggle, sortRows, isSorted: (key: K) => state.key === key, }; } /** * Memoized sorted rows for a list + active sort state. * Prefer this at call sites with heavy pages; getValueDeps keeps getters fresh when needed. */ export function useClientSortedRows( rows: T[], sortKey: K | null, sortDir: ClientSortDir, getValue: (row: T, key: K) => unknown, getValueDeps: readonly unknown[] = [] ): T[] { return useMemo( () => applyClientSort(rows, sortKey, sortDir, getValue), // getValue is often an inline closure; callers pass deps for captured values. // eslint-disable-next-line react-hooks/exhaustive-deps [rows, sortKey, sortDir, ...getValueDeps] ); }