"use client"; import { ColumnDef, Row } from "@tanstack/react-table"; import { ChevronDown, ChevronRight } from "lucide-react"; import { createContext, useContext, useState } from "react"; import { IssueDetail } from "@/components/issues/issue-detail"; import { Badge } from "@/components/shadcn/badge/badge"; import { Button } from "@/components/shadcn/button/button"; import { EntityInfo } from "@/components/shadcn/entities"; import { DataTable, DataTableColumnHeader } from "@/components/shadcn/table"; import { TableCell, TableRow } from "@/components/shadcn/table/table"; import { VERDICTS } from "@/lib/issues/verdicts"; import { MetaDataProps } from "@/types"; import { IssueResponse } from "@/types/issues"; type IssueRow = IssueResponse["data"][number]; interface IssueExpansion { openId: string | null; toggle: (id: string) => void; } const IssueExpansionContext = createContext(null); function IssueExpandCell({ row }: { row: Row }) { const expansion = useContext(IssueExpansionContext); const expanded = expansion?.openId === row.original.id; return ( ); } const columns: ColumnDef[] = [ { id: "expand", header: "", cell: IssueExpandCell }, { id: "issue", header: "Issue", cell: ({ row }) => (

{row.original.attributes.title}

{row.original.attributes.pattern}

), }, { id: "provider", header: "Provider", cell: ({ row }) => ( ), }, { id: "findings", accessorKey: "attributes.findings_count", header: ({ column }) => ( ), cell: ({ row }) => ( {row.original.attributes.findings_count} ), }, { id: "verdict", accessorKey: "attributes.verdict", header: ({ column }) => ( ), cell: ({ row }) => { const verdict = VERDICTS.find( (v) => v.id === row.original.attributes.verdict, ); return ( {verdict?.label ?? row.original.attributes.verdict} ); }, }, ]; export const IssuesTable = ({ issues, metadata, }: { issues: IssueRow[]; metadata?: MetaDataProps; }) => { // One open row at a time: each detail fetches its issue's resource ids. const [openId, setOpenId] = useState(null); const toggleRow = (id: string) => setOpenId((current) => (current === id ? null : id)); const renderAfterRow = (row: Row) => row.original.id === openId ? (
) : null; return (
toggleRow(row.original.id)} renderAfterRow={renderAfterRow} />
); };