import { useEffect, useState } from "react" import { Warning } from "@phosphor-icons/react" import type { BudgetStatusSummary, ProfileSnapshot } from "@shared/types" import { formatMoney } from "../../lib/formatters" import { ListFrame, ListRow } from "../ui" interface BudgetSummaryProps { agents: ProfileSnapshot["agents"] costs: ProfileSnapshot["costs"] companyId?: string } export function BudgetSummary({ agents, costs, companyId }: BudgetSummaryProps) { const [budgetStatus, setBudgetStatus] = useState(null) useEffect(() => { if (!companyId) return void (async () => { const result = await window.agentCompany.getBudgetStatus(companyId) if (result.ok) setBudgetStatus(result.data) })() }, [companyId]) const knownSpend = costs.reduce((sum, entry) => sum + (entry.amountUsd ?? 0), 0) const unattributedCount = costs.filter((entry) => entry.unattributed).length const hasPolicies = budgetStatus && budgetStatus.policies.length > 0 return (
Known spend
{formatMoney(knownSpend)}
Unattributed entries
{String(unattributedCount)}
{hasPolicies ? ( {budgetStatus.policies.map((status) => { const pct = status.amount > 0 ? (status.spent / status.amount) * 100 : 0 const isWarning = pct > 80 && pct <= 100 const isDanger = pct > 100 const barColor = isDanger ? "bg-[color:var(--danger)]" : isWarning ? "bg-[color:var(--warn)]" : "bg-[color:var(--success)]" const nameColor = isDanger ? "text-[color:var(--danger)]" : isWarning ? "text-[color:var(--warn)]" : "text-[color:var(--text)]" const scopeLabel = status.scopeType === "company" ? "Company" : status.scopeType === "agent" ? (agents.find((a) => a.id === status.scopeId)?.name ?? status.scopeId.slice(0, 8)) : status.scopeId.slice(0, 8) return (
{status.scopeType}
{scopeLabel}
{status.hasOpenIncident ? : null}
{formatMoney(status.spent / 100)} / {formatMoney(status.amount / 100)}
{Math.round(pct)}%
) })} ) : ( {agents.map((agent) => { const pct = agent.budgetMonthlyUsd > 0 ? (agent.spentMonthlyUsd / agent.budgetMonthlyUsd) * 100 : 0 const isWarning = pct > 80 && pct <= 100; const isDanger = pct > 100 const barColor = isDanger ? "bg-[color:var(--danger)]" : isWarning ? "bg-[color:var(--warn)]" : "bg-[color:var(--success)]" const nameColor = isDanger ? "text-[color:var(--danger)]" : isWarning ? "text-[color:var(--warn)]" : "text-[color:var(--text)]" return (
{agent.name}
{isDanger ? : null} {isWarning ? : null}
{formatMoney(agent.spentMonthlyUsd)} / {formatMoney(agent.budgetMonthlyUsd)}
{Math.round(pct)}%
) })} )}
) }