fosrl.pangolin/src/app/[orgId]/settings/access/users/UsersDataTable.tsx

152 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-11-02 16:12:20 -04:00
"use client";
import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
getPaginationRowModel,
SortingState,
getSortedRowModel,
ColumnFiltersState,
getFilteredRowModel,
} from "@tanstack/react-table";
import {
Table,
TableBody,
TableCell,
2025-01-04 20:22:01 -05:00
TableContainer,
2024-11-02 16:12:20 -04:00
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Button } from "@app/components/ui/button";
import { useState } from "react";
import { Input } from "@app/components/ui/input";
2025-01-01 21:41:31 -05:00
import { DataTablePagination } from "@app/components/DataTablePagination";
2024-11-25 23:07:21 -05:00
import { Plus, Search } from "lucide-react";
2024-11-02 16:12:20 -04:00
interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[];
inviteUser?: () => void;
}
export function UsersDataTable<TData, TValue>({
inviteUser,
columns,
data,
}: DataTableProps<TData, TValue>) {
const [sorting, setSorting] = useState<SortingState>([]);
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
onSortingChange: setSorting,
getSortedRowModel: getSortedRowModel(),
onColumnFiltersChange: setColumnFilters,
getFilteredRowModel: getFilteredRowModel(),
2025-02-05 21:53:30 -05:00
initialState: {
2024-11-02 16:12:20 -04:00
sorting,
columnFilters,
2024-11-09 23:59:19 -05:00
pagination: {
2025-02-05 21:53:30 -05:00
pageSize: 20,
2024-11-09 23:59:19 -05:00
pageIndex: 0,
},
2024-11-02 16:12:20 -04:00
},
});
return (
<div>
<div className="flex items-center justify-between pb-4">
2024-11-25 23:07:21 -05:00
<div className="flex items-center max-w-sm mr-2 w-full relative">
<Input
placeholder="Search users"
value={
(table
.getColumn("email")
?.getFilterValue() as string) ?? ""
}
onChange={(event) =>
table
.getColumn("email")
?.setFilterValue(event.target.value)
}
className="w-full pl-8"
/>
<Search className="h-4 w-4 absolute left-2 top-1/2 transform -translate-y-1/2" />
2024-11-25 23:07:21 -05:00
</div>
2024-11-02 16:12:20 -04:00
<Button
onClick={() => {
if (inviteUser) {
inviteUser();
}
}}
>
<Plus className="mr-2 h-4 w-4" /> Invite User
</Button>
</div>
2025-01-04 20:22:01 -05:00
<TableContainer>
2024-11-02 16:12:20 -04:00
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef
.header,
2024-11-25 23:07:21 -05:00
header.getContext(),
2024-11-02 16:12:20 -04:00
)}
</TableHead>
);
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={
row.getIsSelected() && "selected"
}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
2024-11-25 23:07:21 -05:00
cell.getContext(),
2024-11-02 16:12:20 -04:00
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className="h-24 text-center"
>
No Users. Invite one to share access to
resources.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
2025-01-04 20:22:01 -05:00
</TableContainer>
2024-11-02 16:12:20 -04:00
<div className="mt-4">
<DataTablePagination table={table} />
</div>
</div>
);
}