Files
SupplierSystem/src/hooks/useClientTableSort.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

129 lines
3.6 KiB
TypeScript

import { startTransition, useCallback, useMemo, useRef, useState } from 'react';
export type ClientSortDir = 'asc' | 'desc';
type SortState<K extends string> = {
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<T, K extends string>(
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<K extends string = string>(
initialKey: K | null = null,
initialDir: ClientSortDir = 'asc'
) {
const [state, setState] = useState<SortState<K>>({
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(
<T,>(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<T, K extends string>(
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]
);
}