Files
ims/components/incidents/incident-detail.tsx
T
adminandClaude Fable 5 576557181a feat: Phase 5 & 6 — usability, compliance hardening, analytics
Phase 5 (usability + compliance):
- In-app notification bell/badge: migration 016 adds read state + per-user
  RLS + create_in_app_notification SECURITY DEFINER RPC; /api/notifications;
  wired into incident creation, CAPA assign/verify, escalation cron
- Incident closure: new POST /api/incidents/[id]/close (requires verification
  status + all CAPAs verified); migration 017 locks closed incidents at DB
  level (update/delete triggers) with append-only incident_addenda + UI panel
- Server-side pagination on HSE/supervisor incident inboxes (.range, 25/page)
- Investigation form: alcohol/urine test result + witness statement refs
  (existing schema columns, now editable)
- Type-specific intake fields: migration 018 adds incidents.type_details
  JSONB; whitelist validation; environmental/asset/security/fire field
  groups in report form; EN/MS/ZH labels; offline queue support
- JKKP 8 annual register CSV export (/api/reports/jkkp8) + dashboard button
  + January statutory deadline banner
- Admin page: user invite (service-role client), role/site/active management,
  site + zone CRUD with QR report links — replaces Phase 0 stub
- Evidence gallery thumbnails via Supabase render transform with fallback

Phase 6 (analytics):
- 12-month stacked trend chart (leading/lagging/other) + top root causes
  (lib/dashboard/trends.ts pure helpers)
- AI rising-risk zones: /api/dashboard/ai/risk-flags aggregates 90-day
  zone stats, claude-opus-4-8 forced tool_use, panel on HSE + management
  dashboards, suggestion audit-logged

Also fixes 9 pre-existing missing /ims basePath prefixes in client fetches
and download links.

132 tests passing, tsc clean, next build clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CPf5Rc8QPx6V8KLEEgfKEQ
2026-07-12 10:25:08 +08:00

126 lines
4.4 KiB
TypeScript

import { EvidenceGallery } from './evidence-gallery'
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',
}
const STATUS_COLORS: Record<string, string> = {
reported: 'bg-yellow-100 text-yellow-800',
triaged: 'bg-blue-100 text-blue-800',
investigating: 'bg-purple-100 text-purple-800',
capa_pending: 'bg-orange-100 text-orange-800',
verification: 'bg-indigo-100 text-indigo-800',
closed: 'bg-green-100 text-green-800',
}
const MEDICAL_LABELS: Record<string, string> = {
none: 'None',
first_aid: 'First Aid',
medical_treatment: 'Medical Treatment',
lti: 'Lost Time Injury (LTI)',
}
export type Incident = {
id: string
reference_no: string | null
incident_type: string
description: string
status: string
severity: number | null
injury_involved: boolean
asset_involved: boolean
medical_status: string | null
lost_days: number | null
reported_at: string
closed_at: string | null
type_details?: Record<string, string | boolean> | null
sites: { id: string; name: string } | null
zones: { id: string; name: string } | null
reporter: { id: string; name: string; email: string } | null
evidence_files: Array<{
id: string
stage: string
file_url: string
file_type: string
uploaded_at: string
}>
}
interface Props {
incident: Incident
}
function Field({ label, value }: { label: string; value: React.ReactNode }) {
return (
<div>
<dt className="text-xs text-gray-500 uppercase tracking-wide">{label}</dt>
<dd className="text-sm text-gray-900 mt-0.5">{value ?? '—'}</dd>
</div>
)
}
export function IncidentDetail({ incident }: Props) {
const reportStageFiles = incident.evidence_files.filter(f => f.stage === 'report')
return (
<div className="space-y-6">
<div className="bg-white rounded-xl shadow-sm p-5">
<div className="flex items-start justify-between mb-4">
<div>
<h1 className="text-xl font-bold text-gray-900 font-mono">
{incident.reference_no ?? 'Pending reference'}
</h1>
<p className="text-sm text-gray-600 mt-0.5">{TYPE_LABELS[incident.incident_type] ?? incident.incident_type}</p>
</div>
<span className={`text-xs px-2.5 py-1 rounded-full font-medium ${STATUS_COLORS[incident.status] ?? 'bg-gray-100'}`}>
{incident.status.replace(/_/g, ' ').toUpperCase()}
</span>
</div>
<dl className="grid grid-cols-2 gap-4 sm:grid-cols-3">
<Field label="Site" value={incident.sites?.name} />
<Field label="Zone" value={incident.zones?.name} />
<Field label="Reported by" value={incident.reporter?.name} />
<Field label="Reported at" value={new Date(incident.reported_at).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })} />
<Field label="Severity" value={incident.severity ? `Level ${incident.severity}` : 'Not yet assigned'} />
{incident.injury_involved && (
<Field label="Medical status" value={MEDICAL_LABELS[incident.medical_status ?? 'none']} />
)}
{incident.injury_involved && incident.lost_days != null && (
<Field label="Lost days" value={`${incident.lost_days} day(s)`} />
)}
{incident.type_details &&
Object.entries(incident.type_details).map(([key, value]) => (
<Field
key={key}
label={key.replace(/_/g, ' ')}
value={typeof value === 'boolean' ? (value ? 'Yes' : 'No') : value}
/>
))}
</dl>
</div>
<div className="bg-white rounded-xl shadow-sm p-5">
<h2 className="text-sm font-semibold text-gray-700 uppercase tracking-wide mb-2">Description</h2>
<p className="text-sm text-gray-800 leading-relaxed whitespace-pre-wrap">{incident.description}</p>
</div>
<div className="bg-white rounded-xl shadow-sm p-5">
<h2 className="text-sm font-semibold text-gray-700 uppercase tracking-wide mb-3">
Evidence Report Stage
<span className="ml-2 text-gray-400 font-normal normal-case">
{reportStageFiles.length} file{reportStageFiles.length !== 1 ? 's' : ''}
</span>
</h2>
<EvidenceGallery files={incident.evidence_files} stage="report" />
</div>
</div>
)
}