import React, { useState, useEffect } from 'react'; const IncidentQueue = ({ showToast }) => { const [incidents, setIncidents] = useState([]); const [currentTime, setCurrentTime] = useState(new Date()); const fetchIncidents = async () => { try { const res = await fetch(`/api/incidents`); const data = await res.json(); setIncidents(data); } catch (err) { console.error(err); } }; useEffect(() => { fetchIncidents(); const timer = setInterval(() => setCurrentTime(new Date()), 1000); return () => clearInterval(timer); }, []); const handleResolve = async (id) => { try { const res = await fetch(`/api/incidents/resolve`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ incident_id: id }) }); if (res.ok) { if (showToast) showToast(`Incident #${id} has been marked as Resolved.`, 'success'); fetchIncidents(); } } catch (err) { console.error(err); } }; const handleEscalate = async (id) => { try { const res = await fetch(`/api/incidents/escalate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ incident_id: id }) }); if (res.ok) { if (showToast) showToast(`🚨 Emergency Response Team (ERT) dispatched for Incident #${id}!`, 'danger'); fetchIncidents(); } } catch (err) { console.error(err); } }; return (

Live Incident Management (SOS Alerts)

{incidents.map(inc => { const slaTime = new Date(inc.sla_deadline); const diffMs = slaTime - currentTime; const isBreached = diffMs <= 0 && inc.status !== 'Resolved' && inc.status !== 'Escalated'; const minsLeft = Math.ceil(diffMs / 60000); let cardBgClass = "bg-surface-container/30 border-white/5"; let borderHighlightClass = "border"; if (inc.status === 'Escalated') { cardBgClass = "bg-error-red/5 border-error-red/20"; borderHighlightClass = "border-l-4 border-l-error-red border-t border-r border-b"; } else if (inc.status !== 'Resolved') { if (isBreached) { cardBgClass = "bg-error-red/5 border-error-red/20"; borderHighlightClass = "border-l-4 border-l-error-red border-t border-r border-b"; } else { cardBgClass = "bg-warning-orange/5 border-warning-orange/20"; borderHighlightClass = "border-l-4 border-l-warning-orange border-t border-r border-b"; } } return (
{inc.type === 'SOS' && ( )} {inc.type} Alert
{inc.status}
Reported By: {inc.reporter_name} ({inc.reporter_role})
{inc.reporter_role === 'Pool Host' && Vehicle: {inc.vehicle_no}
} Triggered At: {new Date(inc.created_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
{inc.status === 'Open' && (
{isBreached ? 'SLA BREACHED' : `SLA: ${minsLeft} mins left`}
)} {inc.status === 'Escalated' && (
ERT DISPATCHED & ACTIVE
)} {inc.status === 'Open' && (
)} {inc.status === 'Escalated' && ( )}
); })} {incidents.length === 0 && (
No active incidents in queue.
)}
); }; export default IncidentQueue;