feat(design): migrate the Incidents list to TableCard with server-side views, sort, filters and paging
Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,138 +0,0 @@
|
||||
'use client'
|
||||
|
||||
import { useRouter, useSearchParams, usePathname } from 'next/navigation'
|
||||
import { useCallback, useState, useTransition, Suspense } from 'react'
|
||||
|
||||
const STATUS_OPTIONS = [
|
||||
{ value: '', label: 'All Statuses' },
|
||||
{ value: 'reported', label: 'Reported' },
|
||||
{ value: 'triaged', label: 'Triaged' },
|
||||
{ value: 'investigating', label: 'Investigating' },
|
||||
{ value: 'capa_pending', label: 'CAPA Pending' },
|
||||
{ value: 'verification', label: 'Pending Verification' },
|
||||
{ value: 'closed', label: 'Closed' },
|
||||
]
|
||||
|
||||
const TYPE_OPTIONS = [
|
||||
{ value: '', label: 'All Types' },
|
||||
{ value: 'injury', label: 'Injury' },
|
||||
{ value: 'near_miss', label: 'Near Miss' },
|
||||
{ value: 'hazard', label: 'Hazard' },
|
||||
{ value: 'asset_damage', label: 'Asset Damage' },
|
||||
{ value: 'environmental', label: 'Environmental' },
|
||||
{ value: 'security', label: 'Security' },
|
||||
{ value: 'fire', label: 'Fire' },
|
||||
{ value: 'transport', label: 'Transport' },
|
||||
]
|
||||
|
||||
export interface SiteOption { id: string; name: string }
|
||||
export interface TruckOption { id: string; truck_no: string }
|
||||
|
||||
interface FiltersProps {
|
||||
sites: SiteOption[]
|
||||
trucks: TruckOption[]
|
||||
}
|
||||
|
||||
function FiltersInner({ sites, trucks }: FiltersProps) {
|
||||
const router = useRouter()
|
||||
const pathname = usePathname()
|
||||
const searchParams = useSearchParams()
|
||||
const [, startTransition] = useTransition()
|
||||
const [search, setSearch] = useState(searchParams.get('q') ?? '')
|
||||
|
||||
const updateParam = useCallback((key: string, value: string) => {
|
||||
const params = new URLSearchParams(searchParams.toString())
|
||||
if (value) {
|
||||
params.set(key, value)
|
||||
} else {
|
||||
params.delete(key)
|
||||
}
|
||||
params.delete('page')
|
||||
startTransition(() => {
|
||||
router.replace(`${pathname}?${params.toString()}`)
|
||||
})
|
||||
}, [router, pathname, searchParams])
|
||||
|
||||
const handleSearch = useCallback((value: string) => {
|
||||
setSearch(value)
|
||||
const timer = setTimeout(() => updateParam('q', value), 300)
|
||||
return () => clearTimeout(timer)
|
||||
}, [updateParam])
|
||||
|
||||
const hasFilters = searchParams.get('q') || searchParams.get('status') ||
|
||||
searchParams.get('type') || searchParams.get('site_id') || searchParams.get('truck_id')
|
||||
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2 mb-4">
|
||||
<div className="relative flex-1 min-w-48">
|
||||
<svg
|
||||
className="absolute left-2.5 top-2 h-4 w-4 text-gray-400 pointer-events-none"
|
||||
fill="none" stroke="currentColor" viewBox="0 0 24 24"
|
||||
>
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2}
|
||||
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
|
||||
</svg>
|
||||
<input
|
||||
type="search"
|
||||
placeholder="Search reference or description..."
|
||||
value={search}
|
||||
onChange={e => handleSearch(e.target.value)}
|
||||
className="w-full pl-8 pr-3 py-1.5 text-sm border border-divider focus:outline-none focus:ring-2 focus:ring-accent"
|
||||
/>
|
||||
</div>
|
||||
<select
|
||||
value={searchParams.get('status') ?? ''}
|
||||
onChange={e => updateParam('status', e.target.value)}
|
||||
className="text-sm border border-divider px-2 py-1.5 focus:outline-none focus:ring-2 focus:ring-accent bg-surface"
|
||||
>
|
||||
{STATUS_OPTIONS.map(o => <option key={o.value} value={o.value}>{o.label}</option>)}
|
||||
</select>
|
||||
<select
|
||||
value={searchParams.get('type') ?? ''}
|
||||
onChange={e => updateParam('type', e.target.value)}
|
||||
className="text-sm border border-divider px-2 py-1.5 focus:outline-none focus:ring-2 focus:ring-accent bg-surface"
|
||||
>
|
||||
{TYPE_OPTIONS.map(o => <option key={o.value} value={o.value}>{o.label}</option>)}
|
||||
</select>
|
||||
{sites.length > 0 && (
|
||||
<select
|
||||
value={searchParams.get('site_id') ?? ''}
|
||||
onChange={e => updateParam('site_id', e.target.value)}
|
||||
className="text-sm border border-divider px-2 py-1.5 focus:outline-none focus:ring-2 focus:ring-accent bg-surface"
|
||||
>
|
||||
<option value="">All Sites</option>
|
||||
{sites.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
)}
|
||||
{trucks.length > 0 && (
|
||||
<select
|
||||
value={searchParams.get('truck_id') ?? ''}
|
||||
onChange={e => updateParam('truck_id', e.target.value)}
|
||||
className="text-sm border border-divider px-2 py-1.5 focus:outline-none focus:ring-2 focus:ring-accent bg-surface"
|
||||
>
|
||||
<option value="">All Trucks</option>
|
||||
{trucks.map(tk => <option key={tk.id} value={tk.id}>{tk.truck_no}</option>)}
|
||||
</select>
|
||||
)}
|
||||
{hasFilters && (
|
||||
<button
|
||||
onClick={() => {
|
||||
setSearch('')
|
||||
router.replace(pathname)
|
||||
}}
|
||||
className="text-sm text-gray-500 hover:text-gray-700 px-2 py-1.5 underline"
|
||||
>
|
||||
Clear filters
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function IncidentFilters({ sites, trucks }: FiltersProps) {
|
||||
return (
|
||||
<Suspense fallback={<div className="h-10 mb-4" />}>
|
||||
<FiltersInner sites={sites} trucks={trucks} />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { StatusTag } from '@/components/ui/status-tag'
|
||||
|
||||
const TYPE_LABELS: Record<string, string> = {
|
||||
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_ORDER = ['reported', 'triaged', 'investigating', 'capa_pending', 'verification', 'closed'] as const
|
||||
|
||||
export type Incident = {
|
||||
id: string
|
||||
reference_no: string | null
|
||||
incident_type: string
|
||||
status: string
|
||||
severity: number | null
|
||||
reported_at: string
|
||||
sites: { name: string } | null
|
||||
zones: { name: string } | null
|
||||
trucks: { truck_no: string } | null
|
||||
reporter: { name: string } | null
|
||||
}
|
||||
|
||||
interface Props {
|
||||
incidents: Incident[]
|
||||
basePath: string
|
||||
}
|
||||
|
||||
export function IncidentList({ incidents, basePath }: Props) {
|
||||
const [typeFilter, setTypeFilter] = useState('')
|
||||
const [statusFilter, setStatusFilter] = useState('')
|
||||
const [search, setSearch] = useState('')
|
||||
|
||||
const filtered = incidents.filter(inc => {
|
||||
if (typeFilter && inc.incident_type !== typeFilter) return false
|
||||
if (statusFilter && inc.status !== statusFilter) return false
|
||||
if (search) {
|
||||
const q = search.toLowerCase()
|
||||
return (
|
||||
inc.reference_no?.toLowerCase().includes(q) ||
|
||||
inc.incident_type.includes(q) ||
|
||||
(inc.sites?.name ?? '').toLowerCase().includes(q)
|
||||
)
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<input
|
||||
type="search"
|
||||
placeholder="Search ref, type, site…"
|
||||
className="border border-divider px-3 py-2 text-sm w-48 focus:outline-none focus:ring-2 focus:ring-accent"
|
||||
value={search}
|
||||
onChange={e => setSearch(e.target.value)}
|
||||
/>
|
||||
<select
|
||||
className="border border-divider px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-accent"
|
||||
value={typeFilter}
|
||||
onChange={e => setTypeFilter(e.target.value)}
|
||||
>
|
||||
<option value="">All types</option>
|
||||
{Object.entries(TYPE_LABELS).map(([v, l]) => (
|
||||
<option key={v} value={v}>{l}</option>
|
||||
))}
|
||||
</select>
|
||||
<select
|
||||
className="border border-divider px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-accent"
|
||||
value={statusFilter}
|
||||
onChange={e => setStatusFilter(e.target.value)}
|
||||
>
|
||||
<option value="">All statuses</option>
|
||||
{STATUS_ORDER.map(s => (
|
||||
<option key={s} value={s}>{s.replace(/_/g, ' ')}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{filtered.length === 0 ? (
|
||||
<div className="text-center py-12 text-gray-500 text-sm">No incidents found</div>
|
||||
) : (
|
||||
<div className="divide-y divide-divider bg-surface shadow-sm overflow-hidden">
|
||||
{filtered.map(inc => (
|
||||
<Link
|
||||
key={inc.id}
|
||||
href={`${basePath}/incidents/${inc.id}`}
|
||||
className="flex items-center justify-between px-4 py-3 hover:bg-gray-50 transition-colors"
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-2 mb-0.5">
|
||||
<span className="font-mono text-sm font-semibold text-gray-900">
|
||||
{inc.reference_no ?? '—'}
|
||||
</span>
|
||||
<StatusTag status={inc.status}>{inc.status.replace(/_/g, ' ').toUpperCase()}</StatusTag>
|
||||
</div>
|
||||
<div className="text-sm text-gray-600">
|
||||
{TYPE_LABELS[inc.incident_type] ?? inc.incident_type}
|
||||
{inc.zones?.name && ` · ${inc.zones.name}`}
|
||||
{inc.sites?.name && ` · ${inc.sites.name}`}
|
||||
{inc.trucks?.truck_no && (
|
||||
<span className="ml-1 text-xs text-gray-400">· {inc.trucks.truck_no}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="ml-4 text-right">
|
||||
<div className="text-xs text-gray-400">
|
||||
{new Date(inc.reported_at).toLocaleDateString('en-MY')}
|
||||
</div>
|
||||
{inc.severity && (
|
||||
<div className="text-xs text-gray-500 mt-0.5">Sev {inc.severity}</div>
|
||||
)}
|
||||
</div>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
'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<string, string> = {
|
||||
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<string, string | null>) =>
|
||||
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<Incident>[] = [
|
||||
{
|
||||
key: 'reference',
|
||||
header: 'Reference',
|
||||
sortKey: 'reference_no',
|
||||
cell: r => <span className="font-mono text-[12.5px] text-accent">{r.reference_no ?? '—'}</span>,
|
||||
},
|
||||
{ key: 'status', header: 'Status', sortKey: 'status', cell: r => <StatusTag status={r.status} /> },
|
||||
{ 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 = total === 0 ? 0 : (params.page - 1) * PAGE_SIZE + 1
|
||||
const to = Math.min(total, from + incidents.length - 1)
|
||||
|
||||
const toolbar = (
|
||||
<>
|
||||
<label className="flex items-center gap-2 h-8 px-2.5 rounded-md bg-surface shadow-input text-[13px] min-w-[260px]">
|
||||
<Search size={16} strokeWidth={2} className="text-muted" aria-hidden />
|
||||
<input
|
||||
type="search"
|
||||
aria-label="Search incidents"
|
||||
placeholder="Search reference or description"
|
||||
value={q}
|
||||
onChange={e => setQ(e.target.value)}
|
||||
className="flex-1 bg-transparent outline-none placeholder:text-faint"
|
||||
/>
|
||||
</label>
|
||||
<FilterChip label="Status" value={params.status} options={STATUS_OPTIONS} onChange={v => update({ status: v || null })} />
|
||||
<FilterChip label="Type" value={params.type} options={TYPE_OPTIONS} onChange={v => update({ type: v || null })} />
|
||||
{sites.length > 0 && (
|
||||
<FilterChip
|
||||
label="Site"
|
||||
value={params.siteId}
|
||||
options={sites.map(s => ({ value: s.id, label: s.name }))}
|
||||
onChange={v => update({ site_id: v || null })}
|
||||
/>
|
||||
)}
|
||||
{trucks.length > 0 && (
|
||||
<FilterChip
|
||||
label="Truck"
|
||||
value={params.truckId}
|
||||
options={trucks.map(t => ({ value: t.id, label: t.truck_no }))}
|
||||
onChange={v => update({ truck_id: v || null })}
|
||||
/>
|
||||
)}
|
||||
{clearable && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setQ('')
|
||||
router.replace(pathname)
|
||||
}}
|
||||
className="text-[12.5px] text-muted hover:text-ink cursor-pointer"
|
||||
>
|
||||
Clear filters
|
||||
</button>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
|
||||
return (
|
||||
<TableCard
|
||||
columns={columns}
|
||||
rows={incidents}
|
||||
rowKey={r => r.id}
|
||||
onRowOpen={r => router.push(`${basePath}/incidents/${r.id}`)}
|
||||
rowActions={r => (
|
||||
<Link
|
||||
href={`${basePath}/incidents/${r.id}`}
|
||||
aria-label="Open incident"
|
||||
className="inline-grid place-items-center w-7 h-7 rounded-md text-ink-3 hover:bg-rule"
|
||||
>
|
||||
<Eye size={16} strokeWidth={2} aria-hidden />
|
||||
</Link>
|
||||
)}
|
||||
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={{ from, to, total, page: params.page, pageCount, onPage: p => update({ page: String(p) }) }}
|
||||
empty={{
|
||||
title: 'No incidents found',
|
||||
action: <Button onClick={() => router.push('/report')}>Report incident</Button>,
|
||||
}}
|
||||
mobileRow={r => (
|
||||
<div className="flex items-start gap-3">
|
||||
<div className="min-w-0 flex-1 space-y-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-mono text-[12.5px] text-accent">{r.reference_no ?? '—'}</span>
|
||||
<StatusTag status={r.status} />
|
||||
</div>
|
||||
<p className="text-[13px] text-ink-3">
|
||||
{[TYPE_LABELS[r.incident_type] ?? r.incident_type, r.zones?.name].filter(Boolean).join(' · ')}
|
||||
</p>
|
||||
<p className="text-xs text-faint">{fmtDate(r.reported_at)}</p>
|
||||
</div>
|
||||
<ChevronRight size={16} strokeWidth={2} className="mt-1 text-faint" aria-hidden />
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import Link from 'next/link'
|
||||
|
||||
interface Props {
|
||||
page: number
|
||||
pageSize: number
|
||||
total: number
|
||||
href: string
|
||||
}
|
||||
|
||||
export function Pagination({ page, pageSize, total, href }: Props) {
|
||||
const totalPages = Math.max(1, Math.ceil(total / pageSize))
|
||||
if (totalPages <= 1) return null
|
||||
|
||||
const link = (p: number, label: string, disabled: boolean) =>
|
||||
disabled ? (
|
||||
<span className="px-3 py-1.5 text-sm text-gray-300">{label}</span>
|
||||
) : (
|
||||
<Link href={`${href}?page=${p}`} className="px-3 py-1.5 text-sm text-accent-700 hover:underline">
|
||||
{label}
|
||||
</Link>
|
||||
)
|
||||
|
||||
return (
|
||||
<nav className="flex items-center justify-between mt-4" aria-label="Pagination">
|
||||
{link(page - 1, '← Previous', page <= 1)}
|
||||
<span className="text-sm text-gray-500">
|
||||
Page {page} of {totalPages}
|
||||
</span>
|
||||
{link(page + 1, 'Next →', page >= totalPages)}
|
||||
</nav>
|
||||
)
|
||||
}
|
||||
+13
-13
@@ -19,18 +19,18 @@ type Props = ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||
mobile?: boolean
|
||||
}
|
||||
|
||||
export function Button({ variant = 'primary', block = false, mobile = false, className, type = 'button', ...rest }: Props) {
|
||||
return (
|
||||
<button
|
||||
type={type}
|
||||
className={cx(
|
||||
BASE,
|
||||
VARIANTS[variant],
|
||||
mobile && variant !== 'icon' && 'h-12 rounded-xl text-sm',
|
||||
block && 'w-full',
|
||||
className,
|
||||
)}
|
||||
{...rest}
|
||||
/>
|
||||
export function buttonClass(
|
||||
variant: Variant = 'primary',
|
||||
opts: { block?: boolean; mobile?: boolean } = {},
|
||||
): string {
|
||||
return cx(
|
||||
BASE,
|
||||
VARIANTS[variant],
|
||||
opts.mobile && variant !== 'icon' && 'h-12 rounded-xl text-sm',
|
||||
opts.block && 'w-full',
|
||||
)
|
||||
}
|
||||
|
||||
export function Button({ variant = 'primary', block = false, mobile = false, className, type = 'button', ...rest }: Props) {
|
||||
return <button type={type} className={cx(buttonClass(variant, { block, mobile }), className)} {...rest} />
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user