### What problem does this PR solve? Feat: Batch operations on documents in a dataset #3221 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
245 lines
7 KiB
TypeScript
245 lines
7 KiB
TypeScript
'use client';
|
|
|
|
import {
|
|
ColumnFiltersState,
|
|
SortingState,
|
|
VisibilityState,
|
|
flexRender,
|
|
getCoreRowModel,
|
|
getFilteredRowModel,
|
|
getPaginationRowModel,
|
|
getSortedRowModel,
|
|
useReactTable,
|
|
} from '@tanstack/react-table';
|
|
import * as React from 'react';
|
|
|
|
import { ChunkMethodDialog } from '@/components/chunk-method-dialog';
|
|
import { RenameDialog } from '@/components/rename-dialog';
|
|
import { Button } from '@/components/ui/button';
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
} from '@/components/ui/table';
|
|
import { UseRowSelectionType } from '@/hooks/logic-hooks/use-row-selection';
|
|
import { useFetchDocumentList } from '@/hooks/use-document-request';
|
|
import { getExtension } from '@/utils/document-util';
|
|
import { useMemo } from 'react';
|
|
import { SetMetaDialog } from './set-meta-dialog';
|
|
import { useChangeDocumentParser } from './use-change-document-parser';
|
|
import { useDatasetTableColumns } from './use-dataset-table-columns';
|
|
import { useRenameDocument } from './use-rename-document';
|
|
import { useSaveMeta } from './use-save-meta';
|
|
|
|
export type DatasetTableProps = Pick<
|
|
ReturnType<typeof useFetchDocumentList>,
|
|
'documents' | 'setPagination' | 'pagination'
|
|
> &
|
|
Pick<UseRowSelectionType, 'rowSelection' | 'setRowSelection'>;
|
|
|
|
export function DatasetTable({
|
|
documents,
|
|
pagination,
|
|
setPagination,
|
|
rowSelection,
|
|
setRowSelection,
|
|
}: DatasetTableProps) {
|
|
const [sorting, setSorting] = React.useState<SortingState>([]);
|
|
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
|
|
[],
|
|
);
|
|
const [columnVisibility, setColumnVisibility] =
|
|
React.useState<VisibilityState>({});
|
|
|
|
const {
|
|
changeParserLoading,
|
|
onChangeParserOk,
|
|
changeParserVisible,
|
|
hideChangeParserModal,
|
|
showChangeParserModal,
|
|
changeParserRecord,
|
|
} = useChangeDocumentParser();
|
|
|
|
const {
|
|
renameLoading,
|
|
onRenameOk,
|
|
renameVisible,
|
|
hideRenameModal,
|
|
showRenameModal,
|
|
initialName,
|
|
} = useRenameDocument();
|
|
|
|
const {
|
|
showSetMetaModal,
|
|
hideSetMetaModal,
|
|
setMetaVisible,
|
|
setMetaLoading,
|
|
onSetMetaModalOk,
|
|
metaRecord,
|
|
} = useSaveMeta();
|
|
|
|
const columns = useDatasetTableColumns({
|
|
showChangeParserModal,
|
|
showRenameModal,
|
|
showSetMetaModal,
|
|
});
|
|
|
|
const currentPagination = useMemo(() => {
|
|
return {
|
|
pageIndex: (pagination.current || 1) - 1,
|
|
pageSize: pagination.pageSize || 10,
|
|
};
|
|
}, [pagination]);
|
|
|
|
const table = useReactTable({
|
|
data: documents,
|
|
columns,
|
|
onSortingChange: setSorting,
|
|
onColumnFiltersChange: setColumnFilters,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
getSortedRowModel: getSortedRowModel(),
|
|
getFilteredRowModel: getFilteredRowModel(),
|
|
onColumnVisibilityChange: setColumnVisibility,
|
|
onRowSelectionChange: setRowSelection,
|
|
onPaginationChange: (updaterOrValue: any) => {
|
|
if (typeof updaterOrValue === 'function') {
|
|
const nextPagination = updaterOrValue(currentPagination);
|
|
setPagination({
|
|
page: nextPagination.pageIndex + 1,
|
|
pageSize: nextPagination.pageSize,
|
|
});
|
|
} else {
|
|
setPagination({
|
|
page: updaterOrValue.pageIndex,
|
|
pageSize: updaterOrValue.pageSize,
|
|
});
|
|
}
|
|
},
|
|
manualPagination: true, //we're doing manual "server-side" pagination
|
|
state: {
|
|
sorting,
|
|
columnFilters,
|
|
columnVisibility,
|
|
rowSelection,
|
|
pagination: currentPagination,
|
|
},
|
|
rowCount: pagination.total ?? 0,
|
|
});
|
|
|
|
return (
|
|
<div className="w-full">
|
|
<div className="rounded-md border">
|
|
<Table>
|
|
<TableHeader>
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
<TableRow key={headerGroup.id}>
|
|
{headerGroup.headers.map((header) => {
|
|
return (
|
|
<TableHead key={header.id}>
|
|
{header.isPlaceholder
|
|
? null
|
|
: flexRender(
|
|
header.column.columnDef.header,
|
|
header.getContext(),
|
|
)}
|
|
</TableHead>
|
|
);
|
|
})}
|
|
</TableRow>
|
|
))}
|
|
</TableHeader>
|
|
<TableBody>
|
|
{table.getRowModel().rows?.length ? (
|
|
table.getRowModel().rows.map((row) => (
|
|
<TableRow
|
|
key={row.id}
|
|
data-state={row.getIsSelected() && 'selected'}
|
|
>
|
|
{row.getVisibleCells().map((cell) => (
|
|
<TableCell
|
|
key={cell.id}
|
|
className={cell.column.columnDef.meta?.cellClassName}
|
|
>
|
|
{flexRender(
|
|
cell.column.columnDef.cell,
|
|
cell.getContext(),
|
|
)}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
))
|
|
) : (
|
|
<TableRow>
|
|
<TableCell
|
|
colSpan={columns.length}
|
|
className="h-24 text-center"
|
|
>
|
|
No results.
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</div>
|
|
<div className="flex items-center justify-end space-x-2 py-4">
|
|
<div className="flex-1 text-sm text-muted-foreground">
|
|
{table.getFilteredSelectedRowModel().rows.length} of{' '}
|
|
{pagination?.total} row(s) selected.
|
|
</div>
|
|
<div className="space-x-2">
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => table.previousPage()}
|
|
disabled={!table.getCanPreviousPage()}
|
|
>
|
|
Previous
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => table.nextPage()}
|
|
disabled={!table.getCanNextPage()}
|
|
>
|
|
Next
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
{changeParserVisible && (
|
|
<ChunkMethodDialog
|
|
documentId={changeParserRecord.id}
|
|
parserId={changeParserRecord.parser_id}
|
|
parserConfig={changeParserRecord.parser_config}
|
|
documentExtension={getExtension(changeParserRecord.name)}
|
|
onOk={onChangeParserOk}
|
|
visible={changeParserVisible}
|
|
hideModal={hideChangeParserModal}
|
|
loading={changeParserLoading}
|
|
></ChunkMethodDialog>
|
|
)}
|
|
|
|
{renameVisible && (
|
|
<RenameDialog
|
|
visible={renameVisible}
|
|
onOk={onRenameOk}
|
|
loading={renameLoading}
|
|
hideModal={hideRenameModal}
|
|
initialName={initialName}
|
|
></RenameDialog>
|
|
)}
|
|
|
|
{setMetaVisible && (
|
|
<SetMetaDialog
|
|
hideModal={hideSetMetaModal}
|
|
loading={setMetaLoading}
|
|
onOk={onSetMetaModalOk}
|
|
initialMetaData={metaRecord.meta_fields}
|
|
></SetMetaDialog>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|