"use client" import * as React from "react" import { ColumnDef } from "@tanstack/react-table" import { Checkbox } from "@/components/ui/checkbox" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Ellipsis, Users, Boxes } from "lucide-react" // import { toast } from "sonner" // If needed import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header" import { formatDate } from "@/lib/utils" // or your date util // Example: If you have a type for row actions import { type DataTableRowAction } from "@/types/table" import { ContactItem, PossibleItem, vendorInvestigationsColumnsConfig, VendorInvestigationsViewWithContacts } from "@/config/vendorInvestigationsColumnsConfig" // Props that define how we handle special columns (contacts, items, actions, etc.) interface GetVendorInvestigationsColumnsProps { setRowAction?: React.Dispatch< React.SetStateAction< DataTableRowAction | null > > openContactsModal?: (investigationId: number, contacts: ContactItem[]) => void openItemsDrawer?: (investigationId: number, items: PossibleItem[]) => void } // This function returns the array of columns for TanStack Table export function getColumns({ setRowAction, openContactsModal, openItemsDrawer, }: GetVendorInvestigationsColumnsProps): ColumnDef< VendorInvestigationsViewWithContacts >[] { // -------------------------------------------- // 1) Select (checkbox) column // -------------------------------------------- const selectColumn: ColumnDef = { id: "select", header: ({ table }) => ( table.toggleAllPageRowsSelected(!!value)} aria-label="Select all" className="translate-y-0.5" /> ), cell: ({ row }) => ( row.toggleSelected(!!value)} aria-label="Select row" className="translate-y-0.5" /> ), size: 40, enableSorting: false, enableHiding: false, } // -------------------------------------------- // 2) Actions column (optional) // -------------------------------------------- const actionsColumn: ColumnDef = { id: "actions", enableHiding: false, cell: ({ row }) => { const inv = row.original return ( ) }, size: 40, } // -------------------------------------------- // 3) Contacts column (badge count -> open modal) // -------------------------------------------- const contactsColumn: ColumnDef = { id: "contacts", header: "Contacts", cell: ({ row }) => { const { contacts, investigationId } = row.original const count = contacts?.length ?? 0 const handleClick = () => { openContactsModal?.(investigationId, contacts) } return ( ) }, enableSorting: false, size: 60, } // -------------------------------------------- // 4) Possible Items column (badge count -> open drawer) // -------------------------------------------- const possibleItemsColumn: ColumnDef = { id: "possibleItems", header: "Items", cell: ({ row }) => { const { possibleItems, investigationId } = row.original const count = possibleItems?.length ?? 0 const handleClick = () => { openItemsDrawer?.(investigationId, possibleItems) } return ( ) }, enableSorting: false, size: 60, } // -------------------------------------------- // 5) Build "grouped" columns from config // -------------------------------------------- const groupMap: Record[]> = {} vendorInvestigationsColumnsConfig.forEach((cfg) => { const groupName = cfg.group || "_noGroup" if (!groupMap[groupName]) { groupMap[groupName] = [] } const childCol: ColumnDef = { accessorKey: cfg.id, header: ({ column }) => ( ), meta: { excelHeader: cfg.excelHeader, group: cfg.group, type: cfg.type, }, cell: ({ row, cell }) => { const val = cell.getValue() // Example: Format date fields if ( cfg.id === "investigationCreatedAt" || cfg.id === "investigationUpdatedAt" || cfg.id === "scheduledStartAt" || cfg.id === "scheduledEndAt" || cfg.id === "completedAt" ) { const dateVal = val ? new Date(val as string) : null return dateVal ? formatDate(dateVal) : "" } // Example: You could show an icon for "investigationStatus" if (cfg.id === "investigationStatus") { return {val as string} } return val ?? "" }, } groupMap[groupName].push(childCol) }) // Turn the groupMap into nested columns const nestedColumns: ColumnDef[] = [] for (const [groupName, colDefs] of Object.entries(groupMap)) { if (groupName === "_noGroup") { nestedColumns.push(...colDefs) } else { nestedColumns.push({ id: groupName, header: groupName, columns: colDefs, }) } } // -------------------------------------------- // 6) Return final columns array // (You can reorder these as you wish.) // -------------------------------------------- return [ selectColumn, ...nestedColumns, contactsColumn, possibleItemsColumn, actionsColumn, ] }