'use client'; import { ColumnDef, ColumnFiltersState, SortingState, VisibilityState, flexRender, getCoreRowModel, getFilteredRowModel, getSortedRowModel, useReactTable, } from '@tanstack/react-table'; import { ArrowUpDown } from 'lucide-react'; import * as React from 'react'; import { RenameDialog } from '@/components/rename-dialog'; import SvgIcon from '@/components/svg-icon'; import { TableEmpty, TableSkeleton } from '@/components/table-skeleton'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Tooltip, TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip'; import { useFetchFileList } from '@/hooks/file-manager-hooks'; import { IFile } from '@/interfaces/database/file-manager'; import { cn } from '@/lib/utils'; import { formatFileSize } from '@/utils/common-util'; import { formatDate } from '@/utils/date'; import { getExtension } from '@/utils/document-util'; import { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { ActionCell } from './action-cell'; import { useHandleConnectToKnowledge, useNavigateToOtherFolder, useRenameCurrentFile, } from './hooks'; import { LinkToDatasetDialog } from './link-to-dataset-dialog'; export function FilesTable() { const [sorting, setSorting] = React.useState([]); const [columnFilters, setColumnFilters] = React.useState( [], ); const [columnVisibility, setColumnVisibility] = React.useState({}); const [rowSelection, setRowSelection] = React.useState({}); const { t } = useTranslation('translation', { keyPrefix: 'fileManager', }); const navigateToOtherFolder = useNavigateToOtherFolder(); const { connectToKnowledgeVisible, hideConnectToKnowledgeModal, showConnectToKnowledgeModal, initialConnectedIds, onConnectToKnowledgeOk, connectToKnowledgeLoading, } = useHandleConnectToKnowledge(); const { fileRenameVisible, showFileRenameModal, hideFileRenameModal, onFileRenameOk, initialFileName, fileRenameLoading, } = useRenameCurrentFile(); const { pagination, data, loading, setPagination } = useFetchFileList(); const columns: ColumnDef[] = [ { id: 'select', header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} aria-label="Select all" /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label="Select row" /> ), enableSorting: false, enableHiding: false, }, { accessorKey: 'name', header: ({ column }) => { return ( ); }, meta: { cellClassName: 'max-w-[20vw]' }, cell: ({ row }) => { const name: string = row.getValue('name'); const type = row.original.type; const id = row.original.id; const isFolder = type === 'folder'; const handleNameClick = () => { if (isFolder) { navigateToOtherFolder(id); } }; return (
{name}

{name}

); }, }, { accessorKey: 'create_time', header: ({ column }) => { return ( ); }, cell: ({ row }) => (
{formatDate(row.getValue('create_time'))}
), }, { accessorKey: 'size', header: ({ column }) => { return ( ); }, cell: ({ row }) => (
{formatFileSize(row.getValue('size'))}
), }, { accessorKey: 'kbs_info', header: t('knowledgeBase'), cell: ({ row }) => { const value = row.getValue('kbs_info'); return Array.isArray(value) ? (
{value?.slice(0, 2).map((x) => ( {x.kb_name} ))} {value.length > 2 && ( )}
) : ( '' ); }, }, { id: 'actions', header: t('action'), enableHiding: false, cell: ({ row }) => { return ( ); }, }, ]; const currentPagination = useMemo(() => { return { pageIndex: (pagination.current || 1) - 1, pageSize: pagination.pageSize || 10, }; }, [pagination]); const table = useReactTable({ data: data?.files || [], 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: data?.total ?? 0, debugTable: true, }); return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {loading ? ( ) : table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ))} )) ) : ( )}
{table.getFilteredSelectedRowModel().rows.length} of {data?.total}{' '} row(s) selected.
{connectToKnowledgeVisible && ( )} {fileRenameVisible && ( )}
); }