'use client' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' type Addendum = { id: string body: string created_at: string author: { name: string } | null } interface Props { incidentId: string status: string canClose: boolean canAddAddenda: boolean } export function ClosurePanel({ incidentId, status, canClose, canAddAddenda }: Props) { const router = useRouter() const [addenda, setAddenda] = useState([]) const [draft, setDraft] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState(null) const isClosed = status === 'closed' useEffect(() => { if (!isClosed) return fetch(`/ims/api/incidents/${incidentId}/addenda`) .then(r => (r.ok ? r.json() : Promise.reject())) .then((data: Addendum[]) => setAddenda(Array.isArray(data) ? data : [])) .catch(() => {}) }, [incidentId, isClosed]) const closeIncident = async () => { if (!confirm('Close this incident? The record will be locked — only addenda can be added afterwards.')) return setBusy(true) setError(null) const res = await fetch(`/ims/api/incidents/${incidentId}/close`, { method: 'POST' }) setBusy(false) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Failed to close incident') return } router.refresh() } const addAddendum = async () => { const text = draft.trim() if (!text) return setBusy(true) setError(null) const res = await fetch(`/ims/api/incidents/${incidentId}/addenda`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ body: text }), }) setBusy(false) if (!res.ok) { const data = await res.json().catch(() => ({})) setError(data.error ?? 'Failed to add addendum') return } setDraft('') const list = await fetch(`/ims/api/incidents/${incidentId}/addenda`).then(r => r.json()).catch(() => []) setAddenda(Array.isArray(list) ? list : []) } if (!isClosed && !canClose) return null if (!isClosed) { return (
{status === 'verification' ? ( ) : null} {error &&

{error}

}
) } return (

Addenda

This incident is closed and locked. New information is recorded as addenda.

{addenda.length === 0 ? (

No addenda.

) : (
    {addenda.map(a => (
  • {a.body}

    {a.author?.name ?? 'Unknown'} · {new Date(a.created_at).toLocaleString('en-MY', { timeZone: 'Asia/Kuala_Lumpur' })}

  • ))}
)} {canAddAddenda && (