'use client' import { useMemo, useState } from 'react' import { useRouter } from 'next/navigation' import { ChevronRight, Columns3, List, Search } from 'lucide-react' import { Avatar } from '@/components/ui/avatar' import { FilterChip } from '@/components/ui/filter-chip' import { Segmented } from '@/components/ui/segmented' import { PriorityTag, StatusTag } from '@/components/ui/status-tag' import { TableCard, type Column } from '@/components/ui/table-card' import { EMPTY_FILTERS, PAGE_SIZE, VIEWS, daysLate, distinct, filterCapas, nextSort, paginate, sortCapas, viewCounts, type CapaFilters, type CapaRow, type CapaSort, type CapaSortKey, type CapaViewKey, } from '@/lib/capa/list' import { CapaBoardView } from './capa-board-view' const PRIORITY_OPTIONS = [ { value: 'high', label: 'High' }, { value: 'med', label: 'Medium' }, { value: 'low', label: 'Low' }, ] const toOptions = (values: string[]) => values.map(v => ({ value: v, label: v })) function Owner({ name }: { name: string | null }) { if (!name) return — return ( {name} ) } function Due({ row, today }: { row: CapaRow; today: string }) { const late = row.status === 'overdue' ? daysLate(row.due_date, today) : 0 return ( {row.due_date} {late > 0 && {`${late} day${late === 1 ? '' : 's'} late`}} ) } export function CapaWorkspace({ capas, today }: { capas: CapaRow[]; today: string }) { const router = useRouter() const [mode, setMode] = useState<'list' | 'board'>('list') const [filters, setFilters] = useState(EMPTY_FILTERS) const [sort, setSort] = useState({ key: 'due_date', dir: 'asc' }) const [page, setPage] = useState(1) const patch = (next: Partial) => { setFilters(f => ({ ...f, ...next })) setPage(1) } const owners = useMemo(() => distinct(capas.map(c => c.owner_name)), [capas]) const departments = useMemo(() => distinct(capas.map(c => c.department)), [capas]) const counts = useMemo(() => viewCounts(capas, filters), [capas, filters]) const sorted = useMemo(() => sortCapas(filterCapas(capas, filters), sort), [capas, filters, sort]) const boardRows = useMemo( () => sortCapas(filterCapas(capas, { ...filters, view: 'all' }), { key: 'due_date', dir: 'asc' }), [capas, filters], ) const pageData = paginate(sorted, page, PAGE_SIZE) const clearable = Boolean(filters.q || filters.owner || filters.department || filters.priority) const columns: Column[] = [ { key: 'action', header: 'Action', cell: r => {r.description} }, { key: 'incident', header: 'Incident', cell: r => {r.incident_ref ?? '—'} }, { key: 'owner', header: 'Owner', cell: r => }, { key: 'department', header: 'Department', cell: r => r.department }, { key: 'priority', header: 'Priority', sortKey: 'priority', cell: r => }, { key: 'due', header: 'Due', sortKey: 'due_date', cell: r => }, { key: 'status', header: 'Status', sortKey: 'status', cell: r => }, ] const toolbar = ( <> patch({ owner: v })} /> patch({ department: v })} /> patch({ priority: v })} /> {clearable && ( )} ) return (

CAPA

setMode(v as 'list' | 'board')} options={[ { value: 'list', label: <>List }, { value: 'board', label: <>Board }, ]} />
{mode === 'list' ? ( r.id} onRowOpen={r => router.push(`/hse/capa/${r.id}`)} views={{ items: VIEWS.map(v => ({ key: v.key, label: v.label, count: counts[v.key] })), active: filters.view, onChange: key => patch({ view: key as CapaViewKey }), }} toolbar={toolbar} sort={sort} onSortChange={key => { setSort(nextSort(sort, key as CapaSortKey)) setPage(1) }} footer={ pageData.total > 0 ? { from: pageData.from, to: pageData.to, total: pageData.total, page: pageData.page, pageCount: pageData.pageCount, onPage: setPage } : undefined } empty={{ title: 'No CAPA actions match' }} mobileRow={r => (

{r.description}

{[r.incident_ref, r.owner_name, `Due ${r.due_date}`].filter(Boolean).join(' · ')}

)} /> ) : (
{toolbar}
)}
) }