import { useCallback, useEffect, useState } from "react"; import { CheckCircle, Warning } from "../ui/icons"; import type { BudgetIncidentRecord } from "@shared/types"; import { ActionButton, EmptyState, ListFrame, ListRow } from "../ui"; import { formatTime } from "../../lib/formatters"; interface IncidentListProps { companyId: string; agents: Array<{ id: string; name: string }>; projects: Array<{ id: string; name: string }>; } const STATUS_STYLES: Record = { open: "text-destructive bg-destructive-muted", resolved: "text-success bg-success-muted", auto_resolved_limit_increase: "text-primary bg-accent", auto_closed: "text-muted-foreground bg-muted", }; const STATUS_LABELS: Record = { open: "Open", resolved: "Resolved", auto_resolved_limit_increase: "Auto-resolved", auto_closed: "Auto-closed", }; export function IncidentList({ companyId, agents, projects }: IncidentListProps) { const [incidents, setIncidents] = useState([]); const [filter, setFilter] = useState<"all" | "open">("open"); const loadData = useCallback(async () => { const result = await window.agentCompany.listBudgetIncidents({ companyId, status: filter }); if (result.ok) setIncidents(result.data); }, [companyId, filter]); useEffect(() => { void loadData(); }, [loadData]); function getScopeName(scopeType: string, scopeId: string): string { if (scopeType === "company") return "Company"; if (scopeType === "agent") { const agent = agents.find((a) => a.id === scopeId); return agent ? agent.name : scopeId.slice(0, 8); } if (scopeType === "project") { const project = projects.find((p) => p.id === scopeId); return project ? project.name : scopeId.slice(0, 8); } return scopeId.slice(0, 8); } async function handleResolve(incidentId: string) { const result = await window.agentCompany.resolveBudgetIncident({ companyId, incidentId }); if (result.ok) void loadData(); } return (

Budget Incidents

{(["open", "all"] as const).map((f) => ( ))}
{incidents.length === 0 ? ( ) : ( {incidents.map((incident) => { const isWarn = incident.thresholdType === "warn_threshold"; return (
{isWarn ? : null} {isWarn ? "Warning" : "Hard Stop"} {STATUS_LABELS[incident.status] ?? incident.status}
{incident.scopeType} · {getScopeName(incident.scopeType, incident.scopeId)}
${(incident.amountObserved / 100).toFixed(2)} / ${(incident.amountLimit / 100).toFixed(2)} {formatTime(incident.windowStart)} - {formatTime(incident.windowEnd)}
{incident.status === "open" ? ( void handleResolve(incident.id)} /> ) : null}
); })}
)}
); }