'use client' import { useEffect, useState } from 'react' import Link from 'next/link' import { usePathname, useRouter, useSearchParams } from 'next/navigation' import { ChevronRight, Eye, Search } from 'lucide-react' import { Button } from '@/components/ui/button' import { FilterChip } from '@/components/ui/filter-chip' import { StatusTag } from '@/components/ui/status-tag' import { TableCard, type Column } from '@/components/ui/table-card' import { PAGE_SIZE, nextSort, withParams, type ListParams, type SortKey } from '@/lib/incidents/list-params' import type { Incident, SiteOption, TruckOption } from '@/lib/incidents/types' const TYPE_LABELS: Record = { injury: 'Injury / Medical', near_miss: 'Near Miss', hazard: 'Hazard', asset_damage: 'Asset Damage', environmental: 'Environmental', security: 'Security', fire: 'Fire / Emergency', transport: 'Transport / Vehicle', } const STATUS_OPTIONS = [ { value: 'reported', label: 'Reported' }, { value: 'triaged', label: 'Triaged' }, { value: 'investigating', label: 'Investigating' }, { value: 'capa_pending', label: 'CAPA pending' }, { value: 'verification', label: 'Verification' }, { value: 'closed', label: 'Closed' }, ] const TYPE_OPTIONS = Object.entries(TYPE_LABELS).map(([value, label]) => ({ value, label })) interface Props { incidents: Incident[] basePath: string total: number counts: { all: number; open: number; closed: number } params: ListParams sites: SiteOption[] trucks: TruckOption[] } const fmtDate = (iso: string) => new Date(iso).toLocaleDateString('en-MY') export function IncidentTable({ incidents, basePath, total, counts, params, sites, trucks }: Props) { const router = useRouter() const pathname = usePathname() const searchParams = useSearchParams() const search = searchParams.toString() ? `?${searchParams.toString()}` : '' const [q, setQ] = useState(params.q) const update = (updates: Record) => router.replace(`${pathname}${withParams(search, updates)}`) useEffect(() => { if (q === params.q) return const timer = setTimeout(() => update({ q: q || null }), 300) return () => clearTimeout(timer) // eslint-disable-next-line react-hooks/exhaustive-deps }, [q]) const columns: Column[] = [ { key: 'reference', header: 'Reference', sortKey: 'reference_no', cell: r => {r.reference_no ?? '—'}, }, { key: 'status', header: 'Status', sortKey: 'status', cell: r => }, { key: 'type', header: 'Type', cell: r => TYPE_LABELS[r.incident_type] ?? r.incident_type }, { key: 'location', header: 'Location', cell: r => [r.zones?.name, r.sites?.name, r.trucks?.truck_no].filter(Boolean).join(' · ') || '—', }, { key: 'severity', header: 'Severity', cell: r => (r.severity ? `Sev ${r.severity}` : '—') }, { key: 'reported', header: 'Reported', sortKey: 'reported_at', cell: r => fmtDate(r.reported_at) }, ] const clearable = Boolean(params.q || params.status || params.type || params.siteId || params.truckId) const pageCount = Math.max(1, Math.ceil(total / PAGE_SIZE)) const from = incidents.length === 0 ? 0 : (params.page - 1) * PAGE_SIZE + 1 const to = incidents.length === 0 ? 0 : from + incidents.length - 1 const toolbar = ( <> update({ status: v || null })} /> update({ type: v || null })} /> {sites.length > 0 && ( ({ value: s.id, label: s.name }))} onChange={v => update({ site_id: v || null })} /> )} {trucks.length > 0 && ( ({ value: t.id, label: t.truck_no }))} onChange={v => update({ truck_id: v || null })} /> )} {clearable && ( )} ) return ( r.id} onRowOpen={r => router.push(`${basePath}/incidents/${r.id}`)} rowActions={r => ( )} views={{ items: [ { key: 'all', label: 'All', count: counts.all }, { key: 'open', label: 'Open', count: counts.open }, { key: 'closed', label: 'Closed', count: counts.closed }, ], active: params.view, onChange: key => update({ view: key === 'all' ? null : key }), }} toolbar={toolbar} sort={{ key: params.sort, dir: params.dir }} onSortChange={key => { const next = nextSort({ key: params.sort, dir: params.dir }, key as SortKey) update({ sort: next.key, dir: next.dir }) }} footer={total > 0 ? { from, to, total, page: params.page, pageCount, onPage: p => update({ page: String(p) }) } : undefined} empty={{ title: 'No incidents found', action: , }} mobileRow={r => (
{r.reference_no ?? '—'}

{[TYPE_LABELS[r.incident_type] ?? r.incident_type, r.zones?.name].filter(Boolean).join(' · ')}

{fmtDate(r.reported_at)}

)} /> ) }