357 lines
10 KiB
TypeScript
357 lines
10 KiB
TypeScript
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
TableEmptyState,
|
|
} from "~/components/ui/table";
|
|
import { TableErrorNotice } from "~/components/ui/error/ErrorContentPrimitives";
|
|
import type { DatasetMetadata } from "~/types/tensorzero";
|
|
import { Link, useFetcher, useLocation } from "react-router";
|
|
import { TableItemTime } from "~/components/ui/TableItems";
|
|
import { toDatasetUrl } from "~/utils/urls";
|
|
import { Button } from "~/components/ui/button";
|
|
import {
|
|
AlertCircle,
|
|
Trash,
|
|
ChevronUp,
|
|
ChevronDown,
|
|
Search,
|
|
} from "lucide-react";
|
|
import { Suspense, useMemo, useState } from "react";
|
|
import { Await, useAsyncError } from "react-router";
|
|
import { Skeleton } from "~/components/ui/skeleton";
|
|
import {
|
|
useReactTable,
|
|
getCoreRowModel,
|
|
getSortedRowModel,
|
|
getFilteredRowModel,
|
|
getPaginationRowModel,
|
|
createColumnHelper,
|
|
flexRender,
|
|
type SortingState,
|
|
type PaginationState,
|
|
} from "@tanstack/react-table";
|
|
import PageButtons from "~/components/utils/PageButtons";
|
|
import { ReadOnlyGuard } from "~/components/utils/read-only-guard";
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "~/components/ui/alert-dialog";
|
|
|
|
const columnHelper = createColumnHelper<DatasetMetadata>();
|
|
|
|
function DatasetTableHeaders() {
|
|
return (
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead>Dataset Name</TableHead>
|
|
<TableHead>Datapoint Count</TableHead>
|
|
<TableHead>Last Updated</TableHead>
|
|
<TableHead />
|
|
</TableRow>
|
|
</TableHeader>
|
|
);
|
|
}
|
|
|
|
// Skeleton table for loading state
|
|
function SkeletonTable() {
|
|
return (
|
|
<>
|
|
<Table>
|
|
<DatasetTableHeaders />
|
|
<TableBody>
|
|
{Array.from({ length: 15 }).map((_, i) => (
|
|
<TableRow key={i}>
|
|
<TableCell>
|
|
<Skeleton className="h-4 w-32" />
|
|
</TableCell>
|
|
<TableCell>
|
|
<Skeleton className="h-4 w-16" />
|
|
</TableCell>
|
|
<TableCell>
|
|
<Skeleton className="h-4 w-28" />
|
|
</TableCell>
|
|
<TableCell />
|
|
</TableRow>
|
|
))}
|
|
</TableBody>
|
|
</Table>
|
|
<PageButtons disabled />
|
|
</>
|
|
);
|
|
}
|
|
|
|
// Error state for failed data load
|
|
function TableErrorState() {
|
|
const error = useAsyncError();
|
|
const message =
|
|
error instanceof Error ? error.message : "Failed to load datasets";
|
|
|
|
return (
|
|
<>
|
|
<Table>
|
|
<DatasetTableHeaders />
|
|
<TableBody>
|
|
<TableRow>
|
|
<TableCell colSpan={4}>
|
|
<TableErrorNotice
|
|
icon={AlertCircle}
|
|
title="Error loading data"
|
|
description={message}
|
|
/>
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableBody>
|
|
</Table>
|
|
<PageButtons disabled />
|
|
</>
|
|
);
|
|
}
|
|
|
|
// Table content that renders resolved data
|
|
function DatasetTableContent({
|
|
datasets,
|
|
globalFilter,
|
|
onDeleteClick,
|
|
}: {
|
|
datasets: DatasetMetadata[];
|
|
globalFilter: string;
|
|
onDeleteClick: (datasetName: string) => void;
|
|
}) {
|
|
const [sorting, setSorting] = useState<SortingState>([]);
|
|
const [pagination, setPagination] = useState<PaginationState>({
|
|
pageIndex: 0,
|
|
pageSize: 15,
|
|
});
|
|
|
|
const columns = useMemo(
|
|
() => [
|
|
columnHelper.accessor("dataset_name", {
|
|
header: "Dataset Name",
|
|
cell: (info) => (
|
|
<Link
|
|
to={toDatasetUrl(info.getValue())}
|
|
className="block no-underline"
|
|
>
|
|
<code className="block overflow-hidden rounded font-mono text-ellipsis whitespace-nowrap transition-colors duration-300 hover:text-gray-500">
|
|
{info.getValue()}
|
|
</code>
|
|
</Link>
|
|
),
|
|
}),
|
|
columnHelper.accessor("datapoint_count", {
|
|
header: "Datapoint Count",
|
|
cell: (info) => info.getValue(),
|
|
}),
|
|
columnHelper.accessor("last_updated", {
|
|
header: "Last Updated",
|
|
cell: (info) => <TableItemTime timestamp={info.getValue()} />,
|
|
}),
|
|
columnHelper.display({
|
|
id: "actions",
|
|
header: "",
|
|
cell: (info) => (
|
|
<div className="text-right">
|
|
<ReadOnlyGuard
|
|
asChild
|
|
onClick={() => onDeleteClick(info.row.original.dataset_name)}
|
|
>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
className="opacity-60 transition-opacity hover:opacity-100"
|
|
>
|
|
<Trash />
|
|
</Button>
|
|
</ReadOnlyGuard>
|
|
</div>
|
|
),
|
|
enableSorting: false,
|
|
}),
|
|
],
|
|
[onDeleteClick],
|
|
);
|
|
|
|
const table = useReactTable({
|
|
data: datasets,
|
|
columns,
|
|
state: {
|
|
sorting,
|
|
globalFilter,
|
|
pagination,
|
|
},
|
|
onSortingChange: setSorting,
|
|
onPaginationChange: setPagination,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getSortedRowModel: getSortedRowModel(),
|
|
getFilteredRowModel: getFilteredRowModel(),
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
globalFilterFn: "includesString",
|
|
});
|
|
|
|
return (
|
|
<>
|
|
<Table>
|
|
<TableHeader>
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
<TableRow key={headerGroup.id}>
|
|
{headerGroup.headers.map((header) => (
|
|
<TableHead
|
|
key={header.id}
|
|
className={
|
|
header.column.getCanSort()
|
|
? "cursor-pointer select-none"
|
|
: ""
|
|
}
|
|
onClick={header.column.getToggleSortingHandler()}
|
|
>
|
|
<div className="flex items-center gap-1">
|
|
{header.isPlaceholder
|
|
? null
|
|
: flexRender(
|
|
header.column.columnDef.header,
|
|
header.getContext(),
|
|
)}
|
|
{header.column.getCanSort() && (
|
|
<div className="flex flex-col">
|
|
{header.column.getIsSorted() === "asc" ? (
|
|
<ChevronUp className="h-3 w-3" />
|
|
) : header.column.getIsSorted() === "desc" ? (
|
|
<ChevronDown className="h-3 w-3" />
|
|
) : (
|
|
<div className="flex flex-col">
|
|
<ChevronUp className="h-2 w-2 opacity-40" />
|
|
<ChevronDown className="h-2 w-2 opacity-40" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
</TableHead>
|
|
))}
|
|
</TableRow>
|
|
))}
|
|
</TableHeader>
|
|
<TableBody>
|
|
{table.getRowModel().rows.length === 0 ? (
|
|
<TableEmptyState message="No datasets found" />
|
|
) : (
|
|
table.getRowModel().rows.map((row) => (
|
|
<TableRow key={row.id} id={row.original.dataset_name}>
|
|
{row.getVisibleCells().map((cell, index) => (
|
|
<TableCell
|
|
key={cell.id}
|
|
className={index === 0 ? "max-w-[200px]" : ""}
|
|
>
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
))
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
|
|
<PageButtons
|
|
onPreviousPage={() => table.previousPage()}
|
|
onNextPage={() => table.nextPage()}
|
|
disablePrevious={!table.getCanPreviousPage()}
|
|
disableNext={!table.getCanNextPage()}
|
|
/>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default function DatasetTable({
|
|
data,
|
|
}: {
|
|
data: Promise<DatasetMetadata[]>;
|
|
}) {
|
|
const fetcher = useFetcher();
|
|
const location = useLocation();
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
|
const [datasetToDelete, setDatasetToDelete] = useState<string | null>(null);
|
|
const [globalFilter, setGlobalFilter] = useState("");
|
|
|
|
return (
|
|
<div>
|
|
<div className="mb-4">
|
|
<div className="relative">
|
|
<Search className="text-muted-foreground absolute top-1/2 left-3 h-4 w-4 -translate-y-1/2" />
|
|
<input
|
|
type="text"
|
|
placeholder="Search datasets..."
|
|
value={globalFilter}
|
|
onChange={(e) => setGlobalFilter(e.target.value)}
|
|
className="border-input bg-background focus:ring-ring w-full rounded-md border py-2 pr-4 pl-10 text-sm focus:border-transparent focus:ring-2 focus:outline-none"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<Suspense key={location.key} fallback={<SkeletonTable />}>
|
|
<Await resolve={data} errorElement={<TableErrorState />}>
|
|
{(datasets) => (
|
|
<DatasetTableContent
|
|
datasets={datasets}
|
|
globalFilter={globalFilter}
|
|
onDeleteClick={(name) => {
|
|
setDatasetToDelete(name);
|
|
setDeleteDialogOpen(true);
|
|
}}
|
|
/>
|
|
)}
|
|
</Await>
|
|
</Suspense>
|
|
|
|
<AlertDialog open={deleteDialogOpen} onOpenChange={setDeleteDialogOpen}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>
|
|
Are you sure you want to delete the dataset{" "}
|
|
<span className="font-mono text-lg font-bold text-red-500">
|
|
{datasetToDelete}
|
|
</span>
|
|
?
|
|
</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
The datapoints will be marked as stale in the database (soft
|
|
deletion). This action cannot be undone.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter className="flex justify-between gap-2">
|
|
<Button
|
|
variant="outline"
|
|
onClick={() => setDeleteDialogOpen(false)}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<div className="flex-1" />
|
|
<Button
|
|
variant="destructive"
|
|
onClick={() => {
|
|
if (datasetToDelete) {
|
|
fetcher.submit(
|
|
{ action: "delete", datasetName: datasetToDelete },
|
|
{ method: "post" },
|
|
);
|
|
}
|
|
setDeleteDialogOpen(false);
|
|
setDatasetToDelete(null);
|
|
}}
|
|
>
|
|
<Trash className="inline h-4 w-4" />
|
|
Delete
|
|
</Button>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</div>
|
|
);
|
|
}
|