Skip to content

Commit 28d6d72

Browse files
committed
data table refactor
1 parent 51922e3 commit 28d6d72

5 files changed

Lines changed: 253 additions & 223 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Eye } from "lucide-react";
2+
import { Button } from "../button";
3+
import { DropdownActions } from "./dropdown-actions";
4+
import { ColumnDef, Row } from "@tanstack/react-table";
5+
import { PasswordItem } from "@prisma/client";
6+
7+
type DataTableColumnsProps<TData extends PasswordItem> = {
8+
toggleVisibility: (id: string) => void;
9+
visibility: Record<string, boolean>;
10+
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
11+
setCurrentId: (id: string) => void;
12+
};
13+
14+
export function DataTableColumns<TData extends PasswordItem>({
15+
toggleVisibility,
16+
visibility,
17+
setOpen,
18+
setCurrentId,
19+
}: DataTableColumnsProps<TData>): ColumnDef<TData>[] {
20+
return [
21+
{
22+
accessorKey: "website",
23+
header: "Website",
24+
},
25+
{
26+
accessorKey: "username",
27+
header: "Username",
28+
},
29+
{
30+
accessorKey: "password",
31+
header: "Password",
32+
cell: ({ row }: { row: Row<TData> }) => {
33+
const entry = row.original;
34+
const isVisible = visibility[entry.id] || false;
35+
36+
return (
37+
<div className="flex items-center">
38+
<span>{isVisible ? entry.password : "••••••••"}</span>
39+
<Button
40+
variant="ghost"
41+
className="ml-2 h-8 w-8 p-0"
42+
onClick={() => toggleVisibility(entry.id)}
43+
>
44+
<Eye />
45+
</Button>
46+
</div>
47+
);
48+
},
49+
},
50+
{
51+
header: "Created At",
52+
accessorKey: "createdAt",
53+
cell: ({ getValue }) => {
54+
const date = new Date(getValue() as string);
55+
return date.toLocaleDateString();
56+
},
57+
},
58+
{
59+
id: "actions",
60+
cell: ({ row }: { row: Row<TData> }) => {
61+
return <DropdownActions entry={row.original} setOpen={setOpen} setCurrentId={setCurrentId} />;
62+
},
63+
},
64+
];
65+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)