'use client' import { ChevronUp, ChevronDown, ChevronsUpDown } from 'lucide-react' import type { Column, SortState, Density, TableVariant } from './types' import { DENSITY_TH, VARIANT_THEAD, VARIANT_THEAD_TH, VARIANT_HEAD_BORDER, VARIANT_HEAD_BG } from './constants' type HeaderProps> = { columns: Column[] sort?: SortState onSort: (sort: SortState) => void hasMenu: boolean hasSelect: boolean hasExpand: boolean allSelected: boolean someSelected: boolean onSelectAll: () => void variant: TableVariant density: Density } // Title case from any key format: last_active → "Last Active", cpuPct → "Cpu Pct", id → "ID" function formatLabel(key: string, label?: string): string { if (label) return label if (key.length <= 2) return key.toUpperCase() return key .replace(/([a-z0-9])([A-Z])/g, '$1 $2') // split camelCase .replaceAll('_', ' ') .replaceAll('-', ' ') .split(/\s+/) .filter(Boolean) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(' ') } export default function Header>({ columns, sort, onSort, hasMenu, hasSelect, hasExpand, allSelected, someSelected, onSelectAll, variant, density, }: HeaderProps) { function handleSort(key: string) { const sameCol = sort?.column === key onSort({ column: key, order: sameCol && sort?.order === 'asc' ? 'desc' : 'asc' }) } return ( {hasSelect && (
)} {columns.map((col) => { const sortable = col.sortable !== false const isActive = sort?.column === col.key const ariaSort = isActive ? sort!.order === 'asc' ? 'ascending' : 'descending' : 'none' const alignClass = col.align === 'right' ? 'justify-end' : col.align === 'center' ? 'justify-center' : '' return ( {sortable ? ( ) : ( {formatLabel(col.key, col.label)} )} ) })} {hasExpand && ( + )} {hasMenu && ( )} ) }