|
| 1 | +import { |
| 2 | + Table, |
| 3 | + TableBody, |
| 4 | + TableCell, |
| 5 | + TableHead, |
| 6 | + TableHeader, |
| 7 | + TableRow, |
| 8 | +} from "@/components/ui/table"; |
| 9 | +import { |
| 10 | + flexRender, |
| 11 | + Row, |
| 12 | + ColumnDef, |
| 13 | + useReactTable, |
| 14 | + getCoreRowModel, |
| 15 | +} from "@tanstack/react-table"; |
| 16 | +import { DataTableColumns } from "./data-table-columns"; |
| 17 | +import { useState } from "react"; |
| 18 | +import {PasswordItem} from "@prisma/client"; |
| 19 | + |
| 20 | +type DataTableTableProps<TData> = { |
| 21 | + decryptedData: TData[]; |
| 22 | + loading: boolean; |
| 23 | + setCurrentId: (id: string) => void; |
| 24 | + setOpen: React.Dispatch<React.SetStateAction<boolean>>; |
| 25 | +}; |
| 26 | + |
| 27 | +export function DataTableTable<TData>({ |
| 28 | + decryptedData, |
| 29 | + loading, |
| 30 | + setCurrentId, |
| 31 | + setOpen, |
| 32 | +}: DataTableTableProps<PasswordItem>) { |
| 33 | + const [visibility, setVisibility] = useState<Record<string, boolean>>({}); |
| 34 | + |
| 35 | + const toggleVisibility = (id: string) => { |
| 36 | + setVisibility((prev) => ({ ...prev, [id]: !prev[id] })); |
| 37 | + setCurrentId(id); |
| 38 | + }; |
| 39 | + |
| 40 | + const columns: ColumnDef<PasswordItem>[] = DataTableColumns({ |
| 41 | + toggleVisibility, |
| 42 | + visibility, |
| 43 | + setOpen, |
| 44 | + setCurrentId, |
| 45 | + }); |
| 46 | + |
| 47 | + const table = useReactTable({ |
| 48 | + data: decryptedData, |
| 49 | + columns, |
| 50 | + getCoreRowModel: getCoreRowModel(), |
| 51 | + }); |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="rounded-md border table-fixed w-full"> |
| 55 | + <Table className="table-fixed w-full"> |
| 56 | + <TableHeader> |
| 57 | + {table.getHeaderGroups().map((headerGroup) => ( |
| 58 | + <TableRow key={headerGroup.id}> |
| 59 | + {headerGroup.headers.map((header) => ( |
| 60 | + <TableHead key={header.id}> |
| 61 | + {header.isPlaceholder |
| 62 | + ? null |
| 63 | + : flexRender( |
| 64 | + header.column.columnDef.header, |
| 65 | + header.getContext() |
| 66 | + )} |
| 67 | + </TableHead> |
| 68 | + ))} |
| 69 | + </TableRow> |
| 70 | + ))} |
| 71 | + </TableHeader> |
| 72 | + <TableBody> |
| 73 | + {table.getRowModel().rows.length ? ( |
| 74 | + table.getRowModel().rows.map((row: Row<PasswordItem>) => ( |
| 75 | + <TableRow key={row.id}> |
| 76 | + {row.getVisibleCells().map((cell) => ( |
| 77 | + <TableCell key={cell.id}> |
| 78 | + {flexRender(cell.column.columnDef.cell, cell.getContext())} |
| 79 | + </TableCell> |
| 80 | + ))} |
| 81 | + </TableRow> |
| 82 | + )) |
| 83 | + ) : ( |
| 84 | + <TableRow> |
| 85 | + <TableCell colSpan={columns.length} className="h-24 text-center"> |
| 86 | + No results. |
| 87 | + </TableCell> |
| 88 | + </TableRow> |
| 89 | + )} |
| 90 | + </TableBody> |
| 91 | + </Table> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
0 commit comments