// Loading the swarm's display name from `GET /api/swarm`, once. The header and // the start page both ask for it and it does not change within a page visit, // so the loader remembers the first request and every caller shares it. The // label is cosmetic, so a failed request, an error response or an unconfigured // name all answer `null` instead of throwing: callers fall back to their own // default, as an operator who never set a name sees. type Fetch = (url: string) => Promise<{ ok: boolean; json: () => Promise; }>; export function createSwarmNameLoader( fetchImpl: Fetch, ): () => Promise { let pending: Promise | null = null; return () => { if (pending === null) { pending = (async () => { const r = await fetchImpl("/api/swarm"); if (!r.ok) return null; const data = (await r.json()) as { name?: string | null }; return data.name || null; })().catch(() => null); } return pending; }; }