import { useMemo, useState } from "react"; import { type ColumnDef, type OnChangeFn, type SortingState, type Updater, getCoreRowModel, getSortedRowModel, useReactTable, } from "@tanstack/react-table"; import { DataTable, type DataTableEmptyState, } from "@/components/tables/DataTable"; import { dateCell } from "@/components/tables/cell-formatters"; import type { TaskCustomFieldDefinitionRead } from "@/api/generated/model"; type CustomFieldsTableProps = { fields: TaskCustomFieldDefinitionRead[]; isLoading?: boolean; sorting?: SortingState; onSortingChange?: OnChangeFn; stickyHeader?: boolean; editHref?: (field: TaskCustomFieldDefinitionRead) => string; onDelete?: (field: TaskCustomFieldDefinitionRead) => void; emptyState?: Omit & { icon?: DataTableEmptyState["icon"]; }; }; const DEFAULT_EMPTY_ICON = ( ); const formatDefaultValue = (value: unknown): string => { if (value === null || value === undefined) return ""; if (typeof value === "string") return value; try { return JSON.stringify(value); } catch { return String(value); } }; export function CustomFieldsTable({ fields, isLoading = false, sorting, onSortingChange, stickyHeader = false, editHref, onDelete, emptyState, }: CustomFieldsTableProps) { const [internalSorting, setInternalSorting] = useState([ { id: "field_key", desc: false }, ]); const resolvedSorting = sorting ?? internalSorting; const handleSortingChange: OnChangeFn = onSortingChange ?? ((updater: Updater) => { setInternalSorting(updater); }); const columns = useMemo[]>( () => [ { accessorKey: "field_key", header: "Field", cell: ({ row }) => (

{row.original.label || row.original.field_key}

key: {row.original.field_key}

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

), }, { accessorKey: "required", header: "Required", cell: ({ row }) => ( {row.original.required === true ? "Required" : "Optional"} ), }, { accessorKey: "field_type", header: "Type", cell: ({ row }) => ( {row.original.field_type} ), }, { accessorKey: "ui_visibility", header: "UI visible", cell: ({ row }) => ( {row.original.ui_visibility} ), }, { accessorKey: "default_value", header: "Default value", enableSorting: false, cell: ({ row }) => (

{formatDefaultValue(row.original.default_value) || "—"}

), }, { accessorKey: "updated_at", header: "Updated", cell: ({ row }) => dateCell(row.original.updated_at), }, ], [], ); // eslint-disable-next-line react-hooks/incompatible-library const table = useReactTable({ data: fields, columns, state: { sorting: resolvedSorting, }, onSortingChange: handleSortingChange, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), }); return ( ); }