import { useEffect } from 'react' import { flexRender, type Table as ITable } from '@tanstack/react-table' import { ChevronDown, ChevronRight } from 'lucide-react' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table.tsx' import { cn } from '@/lib/utils.ts' import { Loading } from './loading.tsx' interface Props { table: ITable } export function BaseTable({ table }: Props) { const pageIndex = table.getState().pagination.pageIndex const pageSize = table.getState().pagination.pageSize const rowCount = table.options.rowCount const currentPageRowsLength = table.getCoreRowModel().rows.length const isFetching = table.options.meta?.isFetching useEffect(() => { if (pageIndex > 0 && currentPageRowsLength === 0 && rowCount) { const lastPageIndex = Math.max(0, Math.ceil(rowCount / pageSize) - 1) if (lastPageIndex !== pageIndex) { table.setPageIndex(lastPageIndex) table.setPagination((prev) => ({ ...prev, pageIndex: lastPageIndex })) } } }, [pageIndex, currentPageRowsLength, rowCount, pageSize, table]) const isTree = !!table.options.getExpandedRowModel return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())} ))} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell, cellIndex) => ( {cellIndex === 0 && isTree ? (
{row.getCanExpand() ? ( ) : ( )} {flexRender(cell.column.columnDef.cell, cell.getContext())}
) : ( flexRender(cell.column.columnDef.cell, cell.getContext()) )}
))}
)) ) : ( No results. )}
) }