130 lines
4.9 KiB
TypeScript
130 lines
4.9 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
import Link from 'next/link'
|
|
import { PriorityTag } from '@/components/ui/status-tag'
|
|
|
|
type CapaStatus = 'open' | 'in_progress' | 'overdue' | 'pending_verification' | 'verified' | 'reopened' | 'closed'
|
|
|
|
type CapaAction = {
|
|
id: string
|
|
incident_id: string
|
|
description: string
|
|
department: string
|
|
due_date: string
|
|
priority: 'low' | 'med' | 'high'
|
|
status: CapaStatus
|
|
incidents: { reference_no: string | null } | null
|
|
owner: { name: string } | null
|
|
}
|
|
|
|
const STATUS_ORDER: CapaStatus[] = ['open', 'in_progress', 'overdue', 'pending_verification', 'verified', 'closed']
|
|
const STATUS_LABELS: Record<CapaStatus, string> = {
|
|
open: 'Open',
|
|
in_progress: 'In Progress',
|
|
overdue: 'Overdue',
|
|
pending_verification: 'Pending Verification',
|
|
verified: 'Verified',
|
|
reopened: 'Reopened',
|
|
closed: 'Closed',
|
|
}
|
|
const COLUMN_BORDER: Record<CapaStatus, string> = {
|
|
open: 'border-divider',
|
|
in_progress: 'border-divider',
|
|
overdue: 'border-accent',
|
|
pending_verification: 'border-divider',
|
|
verified: 'border-divider',
|
|
reopened: 'border-accent',
|
|
closed: 'border-divider',
|
|
}
|
|
|
|
interface Props {
|
|
capas: CapaAction[]
|
|
}
|
|
|
|
export function CapaBoard({ capas }: Props) {
|
|
const [view, setView] = useState<'board' | 'table'>('board')
|
|
|
|
const byStatus = STATUS_ORDER.reduce<Record<CapaStatus, CapaAction[]>>((acc, s) => {
|
|
acc[s] = capas.filter(c => c.status === s)
|
|
return acc
|
|
}, {} as Record<CapaStatus, CapaAction[]>)
|
|
|
|
if (view === 'table') {
|
|
return (
|
|
<div>
|
|
<div className="flex justify-end mb-3">
|
|
<button onClick={() => setView('board')} className="text-sm text-blue-600 hover:underline">Board view</button>
|
|
</div>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-sm">
|
|
<thead>
|
|
<tr className="text-left text-xs text-gray-500 uppercase border-b">
|
|
<th className="pb-2 pr-4">Ref</th>
|
|
<th className="pb-2 pr-4">Action</th>
|
|
<th className="pb-2 pr-4">Owner</th>
|
|
<th className="pb-2 pr-4">Due</th>
|
|
<th className="pb-2 pr-4">Priority</th>
|
|
<th className="pb-2">Status</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-divider">
|
|
{capas.map(c => (
|
|
<tr key={c.id}>
|
|
<td className="py-2 pr-4 text-gray-500 text-xs">
|
|
{(c.incidents as { reference_no: string | null } | null)?.reference_no ?? '—'}
|
|
</td>
|
|
<td className="py-2 pr-4">
|
|
<Link href={`/hse/capa/${c.id}`} className="text-blue-600 hover:underline line-clamp-2">
|
|
{c.description}
|
|
</Link>
|
|
</td>
|
|
<td className="py-2 pr-4 text-gray-600">{(c.owner as { name: string } | null)?.name ?? '—'}</td>
|
|
<td className="py-2 pr-4 text-gray-600">{c.due_date}</td>
|
|
<td className="py-2 pr-4">
|
|
<PriorityTag priority={c.priority} />
|
|
</td>
|
|
<td className="py-2 text-gray-600 text-xs">{STATUS_LABELS[c.status]}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<div className="flex justify-end mb-3">
|
|
<button onClick={() => setView('table')} className="text-sm text-accent-700 hover:underline">Table view</button>
|
|
</div>
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
|
{STATUS_ORDER.filter(s => s !== 'reopened').map(status => (
|
|
<div key={status} className={`border-2 p-4 bg-ground ${COLUMN_BORDER[status]}`}>
|
|
<h3 className="text-xs font-extrabold uppercase tracking-wide text-ink/70 mb-3 pb-2 border-b-2 border-divider">
|
|
{STATUS_LABELS[status]} ({byStatus[status].length})
|
|
</h3>
|
|
<div className="space-y-2 max-h-96 overflow-y-auto pr-2">
|
|
{byStatus[status].map(c => (
|
|
<Link key={c.id} href={`/hse/capa/${c.id}`}
|
|
className="block bg-surface p-3 shadow-sm hover:shadow-md transition-shadow border border-divider">
|
|
<p className="text-sm text-gray-800 line-clamp-2 mb-1">{c.description}</p>
|
|
<div className="flex items-center justify-between text-xs text-gray-500">
|
|
<span>{(c.owner as { name: string } | null)?.name ?? '—'}</span>
|
|
<PriorityTag priority={c.priority} />
|
|
</div>
|
|
<p className="text-xs text-gray-400 mt-1">Due {c.due_date}</p>
|
|
</Link>
|
|
))}
|
|
{byStatus[status].length === 0 && (
|
|
<p className="text-xs text-gray-400 text-center py-3">Empty</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|