ragflow/web/src/hooks/logic-hooks/use-row-selection.ts
balibabu 1657755b5d
Feat: Adjust the operation cell of the table on the file management page and dataset page #3221. (#7526)
### What problem does this PR solve?

Feat: Adjust the operation cell of the table on the file management page
and dataset page #3221.
### Type of change


- [x] New Feature (non-breaking change which adds functionality)
2025-05-08 15:25:26 +08:00

39 lines
1 KiB
TypeScript

import { RowSelectionState } from '@tanstack/react-table';
import { isEmpty } from 'lodash';
import { useCallback, useMemo, useState } from 'react';
export function useRowSelection() {
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
const clearRowSelection = useCallback(() => {
setRowSelection({});
}, []);
const selectedCount = useMemo(() => {
return Object.keys(rowSelection).length;
}, [rowSelection]);
return {
rowSelection,
setRowSelection,
rowSelectionIsEmpty: isEmpty(rowSelection),
clearRowSelection,
selectedCount,
};
}
export type UseRowSelectionType = ReturnType<typeof useRowSelection>;
export function useSelectedIds<T extends Array<{ id: string }>>(
rowSelection: RowSelectionState,
list: T,
) {
const selectedIds = useMemo(() => {
const indexes = Object.keys(rowSelection);
return list
.filter((x, idx) => indexes.some((y) => Number(y) === idx))
.map((x) => x.id);
}, [list, rowSelection]);
return { selectedIds };
}