'use client' import type { ReactNode } from 'react' import { ArrowDown, ArrowUp, ChevronLeft, ChevronRight, ChevronsUpDown, Inbox } from 'lucide-react' import { Checkbox } from './checkbox' import { cx } from './cx' export type Column = { key: string; header: string; sortKey?: string; cell: (row: T) => ReactNode } export type Sort = { key: string; dir: 'asc' | 'desc' } export type TableView = { key: string; label: string; count?: number } export interface TableCardProps { columns: Column[] rows: T[] rowKey: (row: T) => string onRowOpen?: (row: T) => void rowActions?: (row: T) => ReactNode views?: { items: TableView[]; active: string; onChange: (key: string) => void } toolbar?: ReactNode sort?: Sort onSortChange?: (sortKey: string) => void selection?: { selected: Set; onChange: (next: Set) => void } bulkActions?: ReactNode footer?: { from: number; to: number; total: number; page: number; pageCount: number; onPage: (page: number) => void } empty?: { title: string; action?: ReactNode } mobileRow?: (row: T) => ReactNode } const TH = 'px-3 py-[9px] text-xs font-[550] text-muted text-left bg-surface-2 border-b border-rule-2 whitespace-nowrap' const TD = 'px-3 py-3 text-[13px] border-b border-rule' export function TableCard({ columns, rows, rowKey, onRowOpen, rowActions, views, toolbar, sort, onSortChange, selection, bulkActions, footer, empty, mobileRow, }: TableCardProps) { const selectedOnPage = selection ? rows.filter(r => selection.selected.has(rowKey(r))).length : 0 const allSelected = rows.length > 0 && selectedOnPage === rows.length const setAll = (next: boolean) => { if (!selection) return const s = new Set(selection.selected) rows.forEach(r => (next ? s.add(rowKey(r)) : s.delete(rowKey(r)))) selection.onChange(s) } const setOne = (id: string, next: boolean) => { if (!selection) return const s = new Set(selection.selected) if (next) s.add(id) else s.delete(id) selection.onChange(s) } const open = (row: T) => onRowOpen?.(row) const isEmpty = rows.length === 0 && empty return (
{views && (
{views.items.map(v => ( ))}
)} {toolbar &&
{toolbar}
} {isEmpty ? (

{empty.title}

{empty.action}
) : ( <>
{selection && ( )} {columns.map(c => { const sorted = sort && c.sortKey && sort.key === c.sortKey ? sort.dir : null return ( ) })} {rowActions && {rows.map(row => { const id = rowKey(row) const selected = Boolean(selection?.selected.has(id)) return ( open(row)} onKeyDown={e => { if (e.key === 'Enter') open(row) }} className={cx( 'group', onRowOpen && 'cursor-pointer', selected ? 'bg-accent-tint' : 'bg-surface hover:bg-surface-2', )} > {selection && ( )} {columns.map(c => ( ))} {rowActions && ( )} ) })}
0 && !allSelected} onChange={setAll} label="Select all rows" /> {c.sortKey && onSortChange ? ( ) : ( c.header )} }
e.stopPropagation()}> setOne(id, next)} label="Select row" /> {c.cell(row)} e.stopPropagation()}>
{rowActions(row)}
{mobileRow && (
    {rows.map(row => (
  • open(row)} className={cx('px-4 py-3 border-b border-rule', onRowOpen && 'cursor-pointer')} > {mobileRow(row)}
  • ))}
)} )} {footer && (
{`Showing ${footer.from}–${footer.to} of ${footer.total}`}
)} {selection && bulkActions && selection.selected.size > 0 && (
{`${selection.selected.size} selected`} {bulkActions}
)}
) }