// * Packages import { useState } from "react"; import { ColumnDef, SortingState, flexRender, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; // * Components shadcn import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; // * Other import { DefaultModel, Resource, Views } from "@/back/types/global"; import ModelActions from "@/back/Components/ModelActions"; import DataTableFilterButton from "@/back/Components/Table/DataTableFilterButton"; import PaginationWrapper from "@/back/Components/Pagination/PaginationWrapper"; import useTrans from "@/hooks/use-translations"; export default function DataTable({ columns, modelRoute, modelsPaginate, actions, }: { columns: ColumnDef[]; modelRoute: string; modelsPaginate: Resource; actions: Views | null; }) { const [sorting, setSorting] = useState([]); const data = modelsPaginate.data as TData[]; const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), onSortingChange: setSorting, getSortedRowModel: getSortedRowModel(), state: { sorting, }, }); const noResultsTrans = useTrans("uwil-backend::back.table_no_results"); return ( <>
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ); })} {actions && ( )} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ))} {actions && ( )} )) ) : ( {noResultsTrans} )}
); }