import { useState, useCallback, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { useAgents } from "../hooks/useAgents"; import { useCompany } from "../context/CompanyContext"; import { useBreadcrumbs } from "../context/BreadcrumbContext"; import { agentsApi } from "../api/agents"; import { StatusBadge } from "../components/StatusBadge"; import { EntityRow } from "../components/EntityRow"; import { EmptyState } from "../components/EmptyState"; import { formatCents } from "../lib/utils"; import { Bot } from "lucide-react"; export function Agents() { const { selectedCompanyId } = useCompany(); const { data: agents, loading, error, reload } = useAgents(selectedCompanyId); const { setBreadcrumbs } = useBreadcrumbs(); const navigate = useNavigate(); const [actionError, setActionError] = useState(null); useEffect(() => { setBreadcrumbs([{ label: "Agents" }]); }, [setBreadcrumbs]); async function invoke(e: React.MouseEvent, agentId: string) { e.stopPropagation(); setActionError(null); try { await agentsApi.invoke(agentId); reload(); } catch (err) { setActionError(err instanceof Error ? err.message : "Failed to invoke agent"); } } if (!selectedCompanyId) { return ; } return (

Agents

{loading &&

Loading...

} {error &&

{error.message}

} {actionError &&

{actionError}

} {agents && agents.length === 0 && ( )} {agents && agents.length > 0 && (
{agents.map((agent) => { const budgetPct = agent.budgetMonthlyCents > 0 ? Math.round((agent.spentMonthlyCents / agent.budgetMonthlyCents) * 100) : 0; return ( navigate(`/agents/${agent.id}`)} leading={ } trailing={
90 ? "bg-red-400" : budgetPct > 70 ? "bg-yellow-400" : "bg-green-400" }`} style={{ width: `${Math.min(100, budgetPct)}%` }} />
{formatCents(agent.spentMonthlyCents)} / {formatCents(agent.budgetMonthlyCents)}
} /> ); })}
)}
); }