'use client' import { useEffect, useState } from 'react' type SimilarIncident = { id: string reference_no: string | null incident_type: string description: string severity: number | null similarity: number } const TYPE_LABELS: Record = { injury: 'Injury', near_miss: 'Near Miss', hazard: 'Hazard', asset_damage: 'Asset Damage', environmental: 'Environmental', security: 'Security', fire: 'Fire', } interface Props { incidentId: string } export function SimilarIncidentsPanel({ incidentId }: Props) { const [similar, setSimilar] = useState([]) const [loading, setLoading] = useState(true) const [error, setError] = useState(false) useEffect(() => { fetch(`/ims/api/incidents/${incidentId}/similar`) .then(r => { if (!r.ok) throw new Error('failed') return r.json() }) .then(data => setSimilar(Array.isArray(data) ? data : [])) .catch(() => setError(true)) .finally(() => setLoading(false)) }, [incidentId]) if (loading) { return (

Loading similar incidents…

) } if (error || similar.length === 0) return null return (

Similar Past Incidents

{similar.map(inc => (
{inc.reference_no ?? inc.id.slice(0, 8)} · {TYPE_LABELS[inc.incident_type] ?? inc.incident_type} {Math.round(inc.similarity * 100)}% similar

{inc.description}

))}
) }