"use client"; import { ColumnDef, RowSelectionState } from "@tanstack/react-table"; import { ChevronRight } from "lucide-react"; import { Checkbox } from "@/components/shadcn"; import { DataTableColumnHeader, SeverityBadge, StatusFindingBadge, } from "@/components/shadcn/table"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/components/shadcn/tooltip"; import { cn } from "@/lib"; import { canDrillDownFindingGroup, getFilteredFindingGroupDelta, getFindingGroupImpactedCounts, isFindingGroupMuted, } from "@/lib/findings-groups"; import { FindingGroupRow } from "@/types"; import { getProviderDisplayName } from "@/types/providers"; import { DataTableRowActions } from "./data-table-row-actions"; import { canMuteFindingGroup } from "./finding-group-selection"; import { ImpactedResourcesCell } from "./impacted-resources-cell"; import { NotificationIndicator } from "./notification-indicator"; import { ProviderIconCell } from "./provider-icon-cell"; interface GetColumnFindingGroupsOptions { rowSelection: RowSelectionState; selectableRowCount: number; onDrillDown: (checkId: string, group: FindingGroupRow) => void; expandedCheckId?: string | null; /** True when the expanded group has individually selected resources */ hasResourceSelection?: boolean; /** Active URL filters — used to make the delta indicator status-aware */ filters?: Record; } const VISIBLE_DISABLED_CHECKBOX_CLASS = "disabled:opacity-100 disabled:bg-bg-input-primary/60 disabled:border-border-input-primary/70"; export function getColumnFindingGroups({ rowSelection, selectableRowCount, onDrillDown, expandedCheckId, hasResourceSelection = false, filters = {}, }: GetColumnFindingGroupsOptions): ColumnDef[] { const selectedCount = Object.values(rowSelection).filter(Boolean).length; const isAllSelected = selectedCount > 0 && selectedCount === selectableRowCount; const isSomeSelected = selectedCount > 0 && selectedCount < selectableRowCount; return [ // Combined column: notification + expand toggle + checkbox { id: "select", header: ({ table }) => { const headerChecked = isAllSelected ? true : isSomeSelected ? "indeterminate" : false; return (
table.toggleAllPageRowsSelected(checked === true) } onClick={(e) => e.stopPropagation()} aria-label="Select all" disabled={selectableRowCount === 0} />
); }, cell: ({ row }) => { const group = row.original; const allMuted = isFindingGroupMuted(group); const isExpanded = expandedCheckId === group.checkId; const deltaKey = getFilteredFindingGroupDelta(group, filters); const canExpand = canDrillDownFindingGroup(group); const canSelect = canMuteFindingGroup({ resourcesFail: group.resourcesFail, resourcesTotal: group.resourcesTotal, muted: group.muted, mutedCount: group.mutedCount, }); return (
{canExpand ? ( ) : (
)} { // When indeterminate (resources selected), clicking deselects the group if ( rowSelection[row.id] && isExpanded && hasResourceSelection ) { row.toggleSelected(false); } else { row.toggleSelected(checked === true); } }} onClick={(e) => e.stopPropagation()} aria-label="Select row" />
); }, enableSorting: false, enableHiding: false, }, // Status column — not sortable on finding-groups endpoint { accessorKey: "status", header: ({ column }) => ( ), cell: ({ row }) => { return ; }, enableSorting: false, }, // Finding title column { accessorKey: "finding", header: ({ column }) => ( ), cell: ({ row }) => { const group = row.original; const canExpand = canDrillDownFindingGroup(group); const provider = group.providers[0]; const providerName = provider ? getProviderDisplayName(provider) : undefined; return (
{provider && providerName ? (
{providerName}
) : null} {/* Single line always: min room to stay readable, ellipsis beyond the max, full title in the tooltip. */}
{canExpand ? ( ) : ( {group.checkTitle} )} {group.checkTitle}
); }, }, // Severity column { accessorKey: "severity", header: ({ column }) => ( ), cell: ({ row }) => , }, // Impacted Resources column { id: "impactedResources", header: ({ column }) => ( ), cell: ({ row }) => { const group = row.original; const counts = getFindingGroupImpactedCounts(group); return ( ); }, enableSorting: false, }, // Actions column { id: "actions", header: () =>
, cell: ({ row }) => , enableSorting: false, }, ]; }