"use client"; import { type ColumnDef, flexRender, getCoreRowModel, type RowSelectionState, useReactTable, } from "@tanstack/react-table"; import { FolderCode, Plus } from "lucide-react"; import Link from "next/link"; import type { ComponentProps } from "react"; import { type MouseEventHandler, type ReactNode, useState } from "react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { cn } from "@/lib/utils"; import { Button } from "./ui/button"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, } from "./ui/context-menu"; import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from "./ui/empty"; interface DataTableProps { columns: ColumnDef[]; data: TData[]; } export function DataTable({ columns, data, className, openCreateDialog, emptyDescription, rowSelection, setRowSelection, home, context, onClick, style, }: DataTableProps & { className?: string; openCreateDialog?: () => void; emptyDescription?: string; rowSelection?: RowSelectionState; setRowSelection?: ( state: RowSelectionState | ((old: RowSelectionState) => RowSelectionState), ) => void; home?: string; context?: (row: TData) => { label: string; onClick?: MouseEventHandler; icon?: ReactNode; }[]; onClick?: (row: TData) => void; } & Pick, "style">) { const [internalRowSelection, setInternalRowSelection] = useState({}); const table = useReactTable({ data, columns, getCoreRowModel: getCoreRowModel(), onRowSelectionChange: setRowSelection || setInternalRowSelection, state: { rowSelection: rowSelection || internalRowSelection, }, }); return (
{table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( {header.isPlaceholder ? null : flexRender( header.column.columnDef.header, header.getContext(), )} ); })} ))} {table.getRowModel().rows?.length ? ( table.getRowModel().rows.map((row) => ( onClick?.(row.original)} className="group" > {row.getVisibleCells().map((cell) => ( {flexRender( cell.column.columnDef.cell, cell.getContext(), )} ))} {context?.(row.original).map((option) => ( {option.icon} {option.label} ))} )) ) : ( Nothing Yet {emptyDescription || "You haven't created any entries yet. Get started by creating your first entry."}
{openCreateDialog && ( )}
)}
); }