import { useEffect, useState } from "react";
import type { Overview } from "../lib/overview";
import type { AgentHook } from "../lib/useAgent";
import { perHour } from "../lib/format";
import { Button, Notice, NotYet, Panel, Row, RowName, ScreenHead, Toggle } from "../components/ui";
/** Workers and price are v1 writes; the CPU/RAM/GPU budget is policy (v2).
* `prefill` is a hub proposal from a confirmed `zakuro://share/budget` link:
* it fills the form, and nothing is saved until the owner saves. */
export function BudgetPrice({ hook, ov, prefill, onPrefillDone }: { hook: AgentHook; ov: Overview | null; prefill?: Record<string, unknown> | null; onPrefillDone?: () => void }) {
const [price, setPrice] = useState<string>("");
const [defaultPrice, setDefaultPrice] = useState<string>("");
const proposed = typeof prefill?.price_per_hour === "number" ? (prefill.price_per_hour as number) : null;
useEffect(() => {
if (!ov) return;
setDefaultPrice(ov.prices.accountDefault?.toString() ?? "");
// Reset the inputs only when the agent's values change, not on every poll.
}, [ov?.prices.accountDefault]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
// A hub proposal owns the field until it is saved or dismissed: the
// agent's price arriving (or moving) must not put back the old value.
if (!ov || proposed !== null) return;
setPrice(ov.prices.thisNode.perHour?.toString() ?? "");
}, [ov?.prices.thisNode.perHour, proposed]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
// Keyed on the prefill object, not the number: a second link with the
// same proposal fills the field again.
if (proposed !== null) setPrice(String(proposed));
}, [prefill]); // eslint-disable-line react-hooks/exhaustive-deps
const w = ov?.thisNode.workers;
const parse = (s: string) => (s.trim() === "" ? null : Number(s));
const valid = (s: string) => {
const n = parse(s);
return n === null || (ov !== null && Number.isFinite(n) && n >= ov.prices.min && n <= ov.prices.max);
};
return (
<>
<ScreenHead title="Budget & price" sub={ov ? `${ov.thisNode.name} · what this node lends` : undefined} />
{prefill && (
<Notice title="Proposed from the hub" onClose={onPrefillDone}>
{proposed !== null ? `This node's price is filled in at ¥${proposed}/h. Review it, then Save to apply.` : "The link proposed no change this zc can apply yet."}
{Object.keys(prefill).some((k) => k !== "price_per_hour") && " CPU, memory and GPU limits arrive with zc#513."}
</Notice>
)}
<div className="grid grid-cols-2 gap-3">
<Panel title="Sharing" aside="this node">
<Row cols="1fr auto">
<RowName name="Share this node" sub="Drains running work before it stops" />
<Toggle label="Share this node" on={!!ov?.thisNode.sharing} disabled={!ov || hook.busy} onChange={(v) => hook.setSharing(v)} />
</Row>
<Row cols="1fr auto">
<RowName name="Workers" sub={w ? `up to ${w.max} on this node · ${w.running} running` : undefined} />
<div className="flex items-center gap-2">
<Button size="sm" disabled={!w || hook.busy || w.desired <= 0} onClick={() => w && hook.setWorkers(w.desired - 1)}>
−
</Button>
<span className="tnum w-6 text-center font-mono">{w?.desired ?? "—"}</span>
<Button size="sm" disabled={!w || hook.busy || w.desired >= w.max} onClick={() => w && hook.setWorkers(w.desired + 1)}>
+
</Button>
</div>
</Row>
</Panel>
<Panel title="Price" aside={ov ? `per hour · ${ov.prices.min}–${ov.prices.max}` : "per hour"}>
<PriceRow
label="This node"
hint={ov ? `now ${perHour(ov.prices.thisNode.effective)} (${ov.prices.thisNode.state}) · empty = account default` : undefined}
value={price}
onChange={setPrice}
valid={valid(price)}
disabled={!ov || hook.busy}
onSave={() => {
hook.setPrice("device", parse(price));
onPrefillDone?.();
}}
/>
<PriceRow
label="Account default"
hint="Every node without its own price · empty = none"
value={defaultPrice}
onChange={setDefaultPrice}
valid={valid(defaultPrice)}
disabled={!ov || hook.busy}
onSave={() => hook.setPrice("default", parse(defaultPrice))}
/>
</Panel>
</div>
<NotYet what="CPU, memory, disk and GPU budget, when the node lends (idle, always, schedule) and which squads may use it. Saving resizes the sandbox." route="GET/PUT /v2/policy" until="zc#513 and zc#514" />
</>
);
}
function PriceRow(props: { label: string; hint?: string; value: string; onChange: (s: string) => void; valid: boolean; disabled: boolean; onSave: () => void }) {
return (
<Row cols="1fr auto auto">
<RowName name={props.label} sub={props.hint} />
<label className="flex items-center gap-1.5">
<span className="text-fg-dim">¥</span>
<input
aria-label={`${props.label} price per hour`}
inputMode="decimal"
value={props.value}
onChange={(e) => props.onChange(e.target.value)}
className={"tnum w-20 rounded-sm border bg-bg px-2 py-1 font-mono text-[12.5px] outline-none " + (props.valid ? "border-hairline" : "border-wine")}
/>
<span className="text-[12px] text-fg-dim">/ h</span>
</label>
<Button size="sm" variant="primary" disabled={props.disabled || !props.valid} onClick={props.onSave}>
Save
</Button>
</Row>
);
}