168 lines
6.9 KiB
TypeScript
168 lines
6.9 KiB
TypeScript
'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 <span className="text-faint">—</span>
|
|
return (
|
|
<span className="inline-flex items-center gap-2">
|
|
<Avatar name={name} />
|
|
{name}
|
|
</span>
|
|
)
|
|
}
|
|
|
|
function Due({ row, today }: { row: CapaRow; today: string }) {
|
|
const late = row.status === 'overdue' ? daysLate(row.due_date, today) : 0
|
|
return (
|
|
<span className="whitespace-nowrap">
|
|
{row.due_date}
|
|
{late > 0 && <span className="ml-1.5 text-xs text-danger">{`${late} day${late === 1 ? '' : 's'} late`}</span>}
|
|
</span>
|
|
)
|
|
}
|
|
|
|
export function CapaWorkspace({ capas, today }: { capas: CapaRow[]; today: string }) {
|
|
const router = useRouter()
|
|
const [mode, setMode] = useState<'list' | 'board'>('list')
|
|
const [filters, setFilters] = useState<CapaFilters>(EMPTY_FILTERS)
|
|
const [sort, setSort] = useState<CapaSort>({ key: 'due_date', dir: 'asc' })
|
|
const [page, setPage] = useState(1)
|
|
|
|
const patch = (next: Partial<CapaFilters>) => {
|
|
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<CapaRow>[] = [
|
|
{ key: 'action', header: 'Action', cell: r => <span className="line-clamp-2 font-medium">{r.description}</span> },
|
|
{ key: 'incident', header: 'Incident', cell: r => <span className="font-mono text-[12.5px] text-accent">{r.incident_ref ?? '—'}</span> },
|
|
{ key: 'owner', header: 'Owner', cell: r => <Owner name={r.owner_name} /> },
|
|
{ key: 'department', header: 'Department', cell: r => r.department },
|
|
{ key: 'priority', header: 'Priority', sortKey: 'priority', cell: r => <PriorityTag priority={r.priority} /> },
|
|
{ key: 'due', header: 'Due', sortKey: 'due_date', cell: r => <Due row={r} today={today} /> },
|
|
{ key: 'status', header: 'Status', sortKey: 'status', cell: r => <StatusTag status={r.status} /> },
|
|
]
|
|
|
|
const toolbar = (
|
|
<>
|
|
<label className="flex h-8 min-w-[260px] items-center gap-2 rounded-md bg-surface px-2.5 text-[13px] shadow-input">
|
|
<Search size={16} strokeWidth={2} className="text-muted" aria-hidden />
|
|
<input
|
|
type="search"
|
|
aria-label="Search actions"
|
|
placeholder="Search action, incident or owner"
|
|
value={filters.q}
|
|
onChange={e => patch({ q: e.target.value })}
|
|
className="flex-1 bg-transparent outline-none placeholder:text-faint"
|
|
/>
|
|
</label>
|
|
<FilterChip label="Owner" value={filters.owner} options={toOptions(owners)} onChange={v => patch({ owner: v })} />
|
|
<FilterChip label="Department" value={filters.department} options={toOptions(departments)} onChange={v => patch({ department: v })} />
|
|
<FilterChip label="Priority" value={filters.priority} options={PRIORITY_OPTIONS} onChange={v => patch({ priority: v })} />
|
|
{clearable && (
|
|
<button
|
|
type="button"
|
|
onClick={() => patch({ q: '', owner: '', department: '', priority: '' })}
|
|
className="cursor-pointer text-[12.5px] text-muted hover:text-ink"
|
|
>
|
|
Clear filters
|
|
</button>
|
|
)}
|
|
</>
|
|
)
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-[22px] font-[650] md:text-xl">CAPA</h1>
|
|
<Segmented
|
|
value={mode}
|
|
onChange={v => setMode(v as 'list' | 'board')}
|
|
options={[
|
|
{ value: 'list', label: <><List size={14} strokeWidth={2} aria-hidden />List</> },
|
|
{ value: 'board', label: <><Columns3 size={14} strokeWidth={2} aria-hidden />Board</> },
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
{mode === 'list' ? (
|
|
<TableCard
|
|
columns={columns}
|
|
rows={pageData.items}
|
|
rowKey={r => 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 => (
|
|
<div className="flex items-start gap-3">
|
|
<div className="min-w-0 flex-1 space-y-1">
|
|
<p className="line-clamp-2 text-[13px] font-medium">{r.description}</p>
|
|
<div className="flex items-center gap-2">
|
|
<StatusTag status={r.status} />
|
|
<PriorityTag priority={r.priority} />
|
|
</div>
|
|
<p className="text-xs text-faint">
|
|
{[r.incident_ref, r.owner_name, `Due ${r.due_date}`].filter(Boolean).join(' · ')}
|
|
</p>
|
|
</div>
|
|
<ChevronRight size={16} strokeWidth={2} className="mt-1 text-faint" aria-hidden />
|
|
</div>
|
|
)}
|
|
/>
|
|
) : (
|
|
<div className="space-y-3">
|
|
<div className="flex flex-wrap items-center gap-2">{toolbar}</div>
|
|
<CapaBoardView capas={boardRows} />
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|