import { useState, type ReactNode } from 'react' import { ChevronUp, ChevronDown, ChevronsUpDown } from 'lucide-react' import { cn } from '@/lib/utils' export interface Column { key: string header: string render: (item: T) => ReactNode sortable?: boolean className?: string } interface DataTableProps { columns: Column[] data: T[] keyExtractor: (item: T) => string isLoading?: boolean skeletonRows?: number onSort?: (key: string, direction: 'asc' | 'desc') => void sortKey?: string sortDirection?: 'asc' | 'desc' emptyState?: ReactNode } export function DataTable({ columns, data, keyExtractor, isLoading = false, skeletonRows = 5, onSort, sortKey, sortDirection, emptyState, }: DataTableProps) { const [localSortKey, setLocalSortKey] = useState(null) const [localSortDir, setLocalSortDir] = useState<'asc' | 'desc'>('asc') const activeSortKey = sortKey ?? localSortKey const activeSortDir = sortDirection ?? localSortDir const handleSort = (key: string) => { const newDir = activeSortKey === key && activeSortDir === 'asc' ? 'desc' : 'asc' if (onSort) { onSort(key, newDir) } else { setLocalSortKey(key) setLocalSortDir(newDir) } } return (
{columns.map((col) => ( ))} {isLoading ? ( Array.from({ length: skeletonRows }).map((_, i) => ( {columns.map((col) => ( ))} )) ) : data.length === 0 ? ( ) : ( data.map((item) => ( {columns.map((col) => ( ))} )) )}
handleSort(col.key) : undefined} >
{col.header} {col.sortable && ( {activeSortKey === col.key ? ( activeSortDir === 'asc' ? ( ) : ( ) ) : ( )} )}
{emptyState || ( No data found )}
{col.render(item)}
) } export default DataTable