zc2 0.0.45

P2P compute broker with credit-based billing, WAL, and broker mesh support
import { useEffect, useState } from "react";
import type { Overview } from "../lib/overview";
import type { AgentHook } from "../lib/useAgent";
import { perHour } from "../lib/format";
import { Button, ComingWithV2, Panel, Row, RowName, ScreenHead, Toggle } from "../components/ui";

/** Workers and price are v1 writes; the CPU/RAM/GPU budget is policy (v2). */
export function BudgetPrice({ hook, ov }: { hook: AgentHook; ov: Overview | null }) {
  const [price, setPrice] = useState<string>("");
  const [defaultPrice, setDefaultPrice] = useState<string>("");
  useEffect(() => {
    if (!ov) return;
    setPrice(ov.prices.thisNode.perHour?.toString() ?? "");
    setDefaultPrice(ov.prices.accountDefault?.toString() ?? "");
    // Reset the inputs only when the agent's values change, not on every poll.
  }, [ov?.prices.thisNode.perHour, ov?.prices.accountDefault]); // 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} />
      <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))}
          />
          <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>
      <ComingWithV2 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" />
    </>
  );
}

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>
  );
}