import { useEffect, useState } from "react";
import type { Job, Overview } from "../lib/overview";
import { agent } from "../lib/agent";
import { ago } from "../lib/format";
import { Empty, Kpi, NotYet, Panel, Pill, Row, RowName, ScreenHead } from "../components/ui";
/** `GET /v2/jobs`: this node's live and finished jobs (the agent keeps 200). */
export function Jobs({ ov }: { ov: Overview | null }) {
const [jobs, setJobs] = useState<Job[] | null>(null);
useEffect(() => {
agent.jobs().then(setJobs, () => setJobs(null));
}, [ov?.jobs.done24h, ov?.jobs.live.length]);
const w = ov?.thisNode.workers;
return (
<>
<ScreenHead title="Jobs" sub="What this node runs and ran" />
<div className="grid grid-cols-3 gap-2.5">
<Kpi label="Running now" value={ov ? ov.jobs.live.length : "โ"} sub={w ? `${w.busy} of ${w.running} workers busy` : undefined} />
<Kpi label="Finished 24 h" value={ov ? ov.jobs.done24h : "โ"} />
<Kpi label="Requests 1 w" value={ov ? ov.thisNode.requests1w.toLocaleString("en-US") : "โ"} />
</div>
<Panel title="Jobs" aside="sources are attributed with zc#514">
{jobs === null ? (
<Empty>Waiting for the agent.</Empty>
) : jobs.length === 0 ? (
<Empty>No jobs yet. Each busy span of a worker shows up here as a job.</Empty>
) : (
jobs.slice(0, 100).map((j) => (
<Row key={j.id} cols="1fr auto auto">
<RowName name={`${j.id} ยท ${j.worker}`} sub={j.live ? `started ${ago(j.startedAt)}` : `finished ${ago(j.finishedAt)}`} />
<Pill tone={j.live ? "ok" : "neutral"}>{j.live ? "Live" : "Done"}</Pill>
<span className="tnum text-right font-semibold">{j.requests} req</span>
</Row>
))
)}
</Panel>
</>
);
}
export function Squads() {
return (
<>
<ScreenHead title="Squads" sub="Trust groups this node lends to" />
<NotYet what="Every node is in the public squad by default. Trusted squads you join by invite appear here with the grants this node gives them." route="GET /v2/squads" until="zc#517" />
</>
);
}