Files
DECNET/decnet_web/src/hooks/useSwarmHosts.ts
anti a1bc8a878b feat(web/mazenet): show target host in topology list + war map
Renders the swarm host (or "master") that a topology is deployed to,
both as a meta line on each topology list card and in the war-map
header. Operators can now distinguish master-local from agent-targeted
topologies at a glance — previously the only signal was the abstract
"mode: agent" label, with no hint of which agent.

Adds useSwarmHosts() hook for the uuid → host lookup. Falls back to a
short uuid prefix when the hosts list is unavailable so the UI never
hard-fails on a missing /swarm/hosts response.

TopologySummary gains target_host_uuid in the frontend type so the
field actually narrows when checked.
2026-04-25 03:29:49 -04:00

43 lines
1.1 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import api from '../utils/api';
export interface SwarmHost {
uuid: string;
name: string;
address: string;
agent_port: number;
status: string;
last_heartbeat: string | null;
}
/**
* Lookup of enrolled swarm hosts. One-shot fetch on mount, with a manual
* refresh callback. Used to resolve `target_host_uuid` → display name in
* places where we don't already have a host name in hand (topology list,
* war-map header).
*
* Failure is treated as "no agents enrolled" — callers fall back to the
* uuid prefix or a generic label rather than blocking on this lookup.
*/
export function useSwarmHosts(): {
hosts: SwarmHost[];
byUuid: Map<string, SwarmHost>;
refresh: () => Promise<void>;
} {
const [hosts, setHosts] = useState<SwarmHost[]>([]);
const refresh = useCallback(async () => {
try {
const { data } = await api.get<SwarmHost[]>('/swarm/hosts');
setHosts(data ?? []);
} catch {
setHosts([]);
}
}, []);
useEffect(() => { refresh(); }, [refresh]);
const byUuid = new Map(hosts.map((h) => [h.uuid, h]));
return { hosts, byUuid, refresh };
}