import { useMemo, useState } from "react"; import Link from "next/link"; import { type ColumnDef, type OnChangeFn, type SortingState, type Updater, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import type { SkillPackRead } from "@/api/generated/model"; import { DataTable, type DataTableEmptyState } from "@/components/tables/DataTable"; import { dateCell } from "@/components/tables/cell-formatters"; import { Button } from "@/components/ui/button"; import { truncateText as truncate } from "@/lib/formatters"; type SkillPacksTableProps = { packs: SkillPackRead[]; isLoading?: boolean; sorting?: SortingState; onSortingChange?: OnChangeFn; stickyHeader?: boolean; canSync?: boolean; syncingPackIds?: Set; onSync?: (pack: SkillPackRead) => void; onDelete?: (pack: SkillPackRead) => void; getEditHref?: (pack: SkillPackRead) => string; emptyState?: Omit & { icon?: DataTableEmptyState["icon"]; }; }; const DEFAULT_EMPTY_ICON = ( ); export function SkillPacksTable({ packs, isLoading = false, sorting, onSortingChange, stickyHeader = false, canSync = false, syncingPackIds, onSync, onDelete, getEditHref, emptyState, }: SkillPacksTableProps) { const [internalSorting, setInternalSorting] = useState([ { id: "name", desc: false }, ]); const resolvedSorting = sorting ?? internalSorting; const handleSortingChange: OnChangeFn = onSortingChange ?? ((updater: Updater) => { setInternalSorting(updater); }); const columns = useMemo[]>(() => { const baseColumns: ColumnDef[] = [ { accessorKey: "name", header: "Pack", cell: ({ row }) => (

{row.original.name}

{row.original.description || "No description provided."}

), }, { accessorKey: "source_url", header: "Pack URL", cell: ({ row }) => ( {truncate(row.original.source_url, 48)} ), }, { accessorKey: "skill_count", header: "Skills", cell: ({ row }) => ( {row.original.skill_count ?? 0} ), }, { accessorKey: "updated_at", header: "Updated", cell: ({ row }) => dateCell(row.original.updated_at), }, { id: "sync", header: "", enableSorting: false, cell: ({ row }) => { if (!onSync) return null; const isThisPackSyncing = Boolean(syncingPackIds?.has(row.original.id)); return (
); }, }, ]; return baseColumns; }, [canSync, onSync, syncingPackIds]); // eslint-disable-next-line react-hooks/incompatible-library const table = useReactTable({ data: packs, columns, state: { sorting: resolvedSorting, }, onSortingChange: handleSortingChange, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), }); return ( ); }