import type { Overview } from "../lib/overview";
import type { AgentHook } from "../lib/useAgent";
import { perHour, yen } from "../lib/format";
import { Button, Kpi, Meter, Panel, Pill, Row, RowName, ScreenHead } from "../components/ui";
export function Contribution({ hook, ov }: { hook: AgentHook; ov: Overview | null }) {
const w = ov?.thisNode.workers;
return (
<>
<ScreenHead title="Contribution" sub={ov ? `${ov.env} · ${ov.nodesOnline} of ${ov.nodesTotal} nodes online` : "Waiting for the agent"}>
<Button disabled={!ov || hook.busy} onClick={() => hook.refresh()}>
Refresh
</Button>
<Button variant={ov?.thisNode.sharing ? "default" : "primary"} disabled={!ov || hook.busy} onClick={() => ov && hook.setSharing(!ov.thisNode.sharing)}>
{ov?.thisNode.sharing ? "Pause sharing" : "Resume sharing"}
</Button>
</ScreenHead>
<div className="grid grid-cols-4 gap-2.5">
<Kpi label="Earned today" value={yen(ov?.earnings?.today)} sub={ov?.earnings?.tz} />
<Kpi label="Earned 7 d" value={yen(ov?.earnings?.last7d)} sub="all nodes" />
<Kpi label="Requests" value={ov ? ov.thisNode.requests5h.toLocaleString("en-US") : "—"} sub={ov ? `last 5 h · ${ov.thisNode.requests1w.toLocaleString("en-US")} this week` : undefined} />
<Kpi label="Balance" value={yen(ov?.account?.balance)} sub="credits, ¥ units" />
</div>
<div className="grid grid-cols-[1.35fr_1fr] gap-3">
<Panel title="Earned per hour" aside="last 12 h">
<HourBars hours={ov?.hours ?? []} />
</Panel>
<Panel title="This node" aside={ov?.thisNode.name}>
<Row>
<RowName name="Sharing" sub={ov ? `broker ${ov.thisNode.broker}` : undefined} />
<span />
<Pill tone={ov?.thisNode.sharing ? "ok" : "neutral"}>{ov?.thisNode.sharing ? "Sharing" : "Paused"}</Pill>
</Row>
<div className="grid gap-1">
<div className="flex justify-between text-[12.5px] font-semibold">
Workers <span className="tnum font-normal text-fg-dim">{w ? `${w.running} of ${w.max} · ${w.busy} busy` : "—"}</span>
</div>
<Meter parts={w ? [{ pct: (w.busy / w.max) * 100 }, { pct: ((w.running - w.busy) / w.max) * 100, dim: true }] : []} />
</div>
<Row>
<RowName name="Price" sub={ov ? priceSource(ov) : undefined} />
<span />
<span className="tnum font-semibold">{perHour(ov?.prices.thisNode.effective)}</span>
</Row>
</Panel>
</div>
<Panel title="Nodes" aside="from the hub">
{ov?.nodes?.length ? (
ov.nodes.map((n) => (
<Row key={n.id} cols="1fr auto auto">
<RowName name={n.thisNode ? `${n.name} (this node)` : n.name} sub={`${n.workers} workers`} />
<Pill tone={n.online ? "ok" : "neutral"}>{n.online ? "Online" : n.freshness}</Pill>
<span className="tnum text-right font-semibold">{n.priceMin === null ? "—" : n.priceMin === n.priceMax ? perHour(n.priceMin) : `${yen(n.priceMin)}–${perHour(n.priceMax)}`}</span>
</Row>
))
) : (
<span className="text-[12px] text-fg-dim">{ov?.nodes ? "The hub lists no nodes for this account." : "No node list yet."}</span>
)}
</Panel>
</>
);
}
function priceSource(ov: Overview): string {
switch (ov.prices.thisNode.state) {
case "set":
return "fixed for this node";
case "inherited":
return "account default";
case "mixed":
return "mixed across workers (lowest shown)";
case "disabled":
return "workers disabled on the hub";
}
}
function HourBars({ hours }: { hours: { start: string; credits: number }[] }) {
if (hours.length === 0) return <span className="text-[12px] text-fg-dim">No samples yet. The agent records one bucket per hour while the hub is reachable.</span>;
const max = Math.max(...hours.map((h) => h.credits), 1);
return (
<div className="grid gap-1">
<div className="flex h-[104px] items-end gap-1 border-b border-hairline">
{hours.map((h, i) => (
<span key={h.start} className="flex flex-1 flex-col justify-end" title={`${new Date(h.start).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })} · ${yen(h.credits)}`}>
<i className={"block rounded-t-[2px] bg-wine " + (i === hours.length - 1 ? "" : "opacity-55")} style={{ height: `${(h.credits / max) * 100}px` }} />
</span>
))}
</div>
<div className="flex justify-between font-mono text-[10px] text-fg-dim">
<span>{new Date(hours[0]!.start).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}</span>
<span>now</span>
</div>
</div>
);
}