usePolled reports when a request is in flight (through a new onBusy of the poller) and holds it on for at least 0.4 seconds, so a quick answer still reads as a refresh. The picker swaps its clock for the spinner, in the same box so its width stays, and the spinner stands still where motion is off. Every page with a picker passes it; a page with several polled requests shows it while any of them runs. The Jobs page fetches through shared components and has none yet.
182 lines
6.9 KiB
TypeScript
182 lines
6.9 KiB
TypeScript
// <AgentsPage> — the swarm's agent roster, merged with each agent's open
|
|
// config-PR status, swarm-wide health status, and declared wanted state.
|
|
// A roster with no per-row detail is thin, and neither a config-PR nor a
|
|
// status panel has anything to render against without a roster to embed
|
|
// in — so all three ended up as one page.
|
|
//
|
|
// One fetch, on a refresh-interval cadence like the hives card: `GET
|
|
// /api/agents/status` — one row per roster agent (identity store is the
|
|
// roster, so a never-reported agent still gets a row), each already
|
|
// carrying its freshness/snapshot/config PR/wanted state. Used to be
|
|
// three separate fetches joined client-side by name; per operator
|
|
// review feedback ("the view should be filled by a single backend
|
|
// call") the join moved server-side instead — see
|
|
// `swarm-controller/src/routes/agents.rs`'s `get_agents_status` handler. No more
|
|
// per-page joining left to do here: the wire row *is* the row this page renders.
|
|
//
|
|
// Owns the "+ agent" trigger too — the roster this populates is the
|
|
// natural home for the action that populates it. The form itself
|
|
// (`CreateAgentForm`) mounts inside a `Dialog` here, not its own route.
|
|
//
|
|
// The page is a column of separate cards: the hives card, a filter card (with
|
|
// the "+ agent" button and the refresh picker), then one card per agent, each
|
|
// opening in place into its details (mara: "one column of expandable cards";
|
|
// the agents are not wrapped in one big card). This file owns the page's own
|
|
// state and the filter fields only — `AgentRow` and friends live in
|
|
// `AgentTypes.ts`, the wanted-state control in `WantedMenu.tsx`, the actions
|
|
// on an agent (declare a state, link an account) in `AgentActions.tsx`, the
|
|
// card in `AgentCard.tsx` and its opened body in `AgentDetail.tsx`
|
|
// (mara: "agentspage is now giant and deserves a split"). The filter bar is
|
|
// `FilterableView` (`ui/filterable-view/`, see its own comment).
|
|
import { useState } from "preact/hooks";
|
|
import { ApiErrorPanel } from "@hive/shared/api-error-panel.js";
|
|
import { useAgentActions } from "../../agent/roster/AgentActions.js";
|
|
import { AgentCard } from "../../agent/roster/AgentCard.js";
|
|
import { FRESHNESS, type AgentRow } from "../../agent/AgentTypes.js";
|
|
import { Button } from "../../ui/button/Button.js";
|
|
import { Dialog } from "../../ui/dialog/Dialog.js";
|
|
import { FilterableView } from "../../ui/filterable-view/FilterableView.js";
|
|
import {
|
|
RefreshIntervalPicker,
|
|
type RefreshIntervalMs,
|
|
} from "../../ui/refresh-interval/RefreshInterval.js";
|
|
import { fetchJson, usePolled } from "../../ui/polled/usePolled.js";
|
|
import { type FilterColumn } from "../../ui/table/Table.js";
|
|
import { CreateAgentForm } from "../CreateAgentForm.js";
|
|
|
|
// Same 30s default + same reasoning as the hives card: no inputs on this page
|
|
// for a refresh to clobber, so the out-of-the-box behaviour should just
|
|
// solve staleness rather than require an opt-in every visit.
|
|
const DEFAULT_INTERVAL_MS: RefreshIntervalMs = 30_000;
|
|
|
|
// What the filter bar filters on, one control per field. The card shows the
|
|
// values itself, so a field only says how to read one off a row.
|
|
const FIELDS: FilterColumn<AgentRow>[] = [
|
|
{
|
|
key: "name",
|
|
header: "name",
|
|
sortBy: (a) => a.name,
|
|
filterValue: (a) => a.name,
|
|
},
|
|
{
|
|
key: "hive",
|
|
header: "hive",
|
|
sortBy: (a) => a.hive ?? "",
|
|
filterValues: (a) => [a.hive ?? "—"],
|
|
filterMode: "multiselect",
|
|
},
|
|
{
|
|
key: "status",
|
|
header: "status",
|
|
sortBy: (a) => FRESHNESS[a.freshness].label,
|
|
filterValues: (a) => [FRESHNESS[a.freshness].label],
|
|
filterMode: "multiselect",
|
|
},
|
|
{
|
|
key: "message",
|
|
header: "message",
|
|
filterValue: (a) => a.snapshot?.status_text ?? "",
|
|
},
|
|
{
|
|
key: "wanted",
|
|
header: "wanted",
|
|
sortBy: (a) => a.wanted ?? "",
|
|
filterValues: (a) => [a.wanted ?? "no declaration"],
|
|
filterMode: "multiselect",
|
|
},
|
|
{
|
|
key: "config-pr",
|
|
header: "config PR",
|
|
sortBy: (a) => a.config_pr?.pr_number ?? 0,
|
|
filterValue: (a) => (a.config_pr ? `#${a.config_pr.pr_number}` : ""),
|
|
},
|
|
];
|
|
|
|
export function AgentsPage() {
|
|
const [intervalMs, setIntervalMs] =
|
|
useState<RefreshIntervalMs>(DEFAULT_INTERVAL_MS);
|
|
const {
|
|
data: rows,
|
|
error,
|
|
refreshing,
|
|
setData: setRows,
|
|
} = usePolled(intervalMs, (signal) =>
|
|
fetchJson<AgentRow[]>("/api/agents/status", signal),
|
|
);
|
|
const [createOpen, setCreateOpen] = useState(false);
|
|
// The PUT response is the hive's whole declaration set (every agent's
|
|
// `wanted` state) — same shape `GET /api/hives/{hive}/wanted` returns — so
|
|
// patch just that field into the rows already on screen rather than firing
|
|
// a second, full `/api/agents/status` round-trip merely to learn one
|
|
// hive's `wanted` column changed. Everything else on the grid (freshness,
|
|
// snapshot, config PR) still catches up on the next interval-driven
|
|
// `refresh()`.
|
|
const actions = useAgentActions((row, declared) => {
|
|
setRows((prev) => {
|
|
if (!prev) return prev;
|
|
const byAgent = new Map(declared.map((d) => [d.agent, d.state]));
|
|
return prev.map((r) =>
|
|
r.hive === row.hive && byAgent.has(r.name)
|
|
? { ...r, wanted: byAgent.get(r.name) ?? r.wanted }
|
|
: r,
|
|
);
|
|
});
|
|
});
|
|
|
|
const barActions = (
|
|
<>
|
|
<Button variant="primary" onClick={() => setCreateOpen(true)}>
|
|
+ agent
|
|
</Button>
|
|
<RefreshIntervalPicker
|
|
id="agents-refresh"
|
|
value={intervalMs}
|
|
onChange={setIntervalMs}
|
|
refreshing={refreshing}
|
|
/>
|
|
</>
|
|
);
|
|
|
|
return (
|
|
<>
|
|
{/* Until the roster loads (or when it fails) there are no filters, but the
|
|
buttons stay, so an agent can still be created. */}
|
|
{rows === null ? (
|
|
<div class="ui-filterable-view-filter-bar ui-filterable-view-filter-card">
|
|
<div class="ui-filterable-view-actions">{barActions}</div>
|
|
</div>
|
|
) : null}
|
|
{error ? (
|
|
<ApiErrorPanel
|
|
context="failed to load the agent roster"
|
|
problem={error}
|
|
/>
|
|
) : null}
|
|
{!error && rows === null ? <p>loading…</p> : null}
|
|
{rows ? (
|
|
<FilterableView
|
|
view="cards"
|
|
standalone
|
|
barActions={barActions}
|
|
columns={FIELDS}
|
|
rows={rows}
|
|
rowKey={(a) => a.name}
|
|
storageKey="swarm-ui:agents:table-filters"
|
|
emptyMessage="no agents yet — the swarm-wide identity store has no agents registered on any hive"
|
|
// Each card gets its live row from `rows` on every render, so an
|
|
// opened card follows a refresh or a state change made inside it.
|
|
renderCard={(a) => <AgentCard row={a} actions={actions} />}
|
|
/>
|
|
) : null}
|
|
<Dialog
|
|
open={createOpen}
|
|
onClose={() => setCreateOpen(false)}
|
|
label="create agent"
|
|
plain
|
|
>
|
|
<CreateAgentForm onClose={() => setCreateOpen(false)} />
|
|
</Dialog>
|
|
{actions.dialogs}
|
|
</>
|
|
);
|
|
}
|