import { useState, useMemo, useCallback } from "react"; import { Table, TableHeader, TableColumn, TableBody, TableRow, TableCell, Button, DropdownTrigger, Dropdown, DropdownMenu, DropdownItem, SortDescriptor, Link, } from "@nextui-org/react"; import { MoreVertical } from "lucide-react"; import { RunType } from "@/types/run"; import dayjs from "dayjs"; const headerColumns = [ { name: "ID", uid: "id", sortable: true }, { name: "Name", uid: "name", sortable: true }, { name: "Description", uid: "description", sortable: true }, { name: "Last update", uid: "updatedAt", sortable: true }, { name: "Actions", uid: "actions" }, ]; type Props = { projectId: string; runs: RunType[]; onDeleteRun: (runId: number) => void; }; export default function RunsTable({ projectId, runs, onDeleteRun }: Props) { const [sortDescriptor, setSortDescriptor] = useState({ column: "id", direction: "ascending", }); const sortedItems = useMemo(() => { return [...runs].sort((a: RunType, b: RunType) => { const first = a[sortDescriptor.column as keyof RunType] as number; const second = b[sortDescriptor.column as keyof RunType] as number; const cmp = first < second ? -1 : first > second ? 1 : 0; return sortDescriptor.direction === "descending" ? -cmp : cmp; }); }, [sortDescriptor, runs]); const truncateText = (text: string, maxLength: number) => { return text.length > maxLength ? text.slice(0, maxLength) + "..." : text; }; const renderCell = useCallback((run: RunType, columnKey: Key) => { const cellValue = run[columnKey as keyof RunType]; switch (columnKey) { case "id": return {cellValue}; case "name": return ( {cellValue} ); case "detail": const maxLength = 20; const truncatedValue = truncateText(cellValue, maxLength); return (
{truncatedValue}
); case "updatedAt": return {dayjs(cellValue).format("YYYY/MM/DD HH:mm")}; case "actions": return ( onDeleteRun(run.id)} > Delete run ); default: return cellValue; } }, []); const classNames = useMemo( () => ({ wrapper: ["max-w-3xl"], th: ["bg-transparent", "text-default-500", "border-b", "border-divider"], td: [ // changing the rows border radius // first "group-data-[first=true]:first:before:rounded-none", "group-data-[first=true]:last:before:rounded-none", // middle "group-data-[middle=true]:before:rounded-none", // last "group-data-[last=true]:first:before:rounded-none", "group-data-[last=true]:last:before:rounded-none", ], }), [] ); return ( <> {(column) => ( {column.name} )} {(item) => ( {(columnKey) => ( {renderCell(item, columnKey)} )} )}
); }