swarm-ui: one stream hook, and one rule for confirming a change
Part of the unification order in the UI sweep issue (steps e and f). The terminal, turn-state and message feeds each opened an EventSource, tracked connecting/open/reconnecting/closed, probed the session when the browser gave up, and reconnected through an attempt counter. openStream (no Preact, source injected, tested with a fake) and useEventStream now own that; each feed keeps only what it does with a message. A change confirmed itself three ways: a toast, inline, or nothing. The design guide now says forms confirm inline, actions on a card in a refreshing list confirm with a toast, and a change the page already shows says nothing. Fire now, saving a schedule and removing an egress entry gain the toast they lacked.
This commit is contained in:
parent
7f8d6abc58
commit
bbdd32c2e0
4 changed files with 294 additions and 138 deletions
|
|
@ -27,6 +27,7 @@ import { Badge } from "@hive/shared/badge.js";
|
|||
import { ActionRow } from "../../ui/action-row/ActionRow.js";
|
||||
import { BadgePopover } from "../../ui/badge-popover/BadgePopover.js";
|
||||
import { Button } from "../../ui/button/Button.js";
|
||||
import { showToast } from "../../ui/toast/toasts.js";
|
||||
import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js";
|
||||
import { FormStack } from "../../ui/form-stack/FormStack.js";
|
||||
import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js";
|
||||
|
|
@ -89,7 +90,7 @@ export function AllowedDomains({ agent }: { agent: string }) {
|
|||
) {
|
||||
return;
|
||||
}
|
||||
void remove(e);
|
||||
if (await remove(e)) showToast(`removed ${e.domain}`, "ok");
|
||||
}
|
||||
|
||||
// A list already loaded stays under a failed refetch, so a change that
|
||||
|
|
|
|||
|
|
@ -34,8 +34,9 @@ function useDomainList(url: string) {
|
|||
reload,
|
||||
} = useResource((signal) => fetchJson<Listing>(url, signal), [url]);
|
||||
|
||||
async function remove(entry: AllowedDomain) {
|
||||
if (busy) return;
|
||||
/** True when the entry was removed. */
|
||||
async function remove(entry: AllowedDomain): Promise<boolean> {
|
||||
if (busy) return false;
|
||||
setBusy(true);
|
||||
try {
|
||||
const r = await fetch(url, {
|
||||
|
|
@ -45,12 +46,14 @@ function useDomainList(url: string) {
|
|||
});
|
||||
if (!r.ok) {
|
||||
setRemoveError(await readApiError(r));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
setRemoveError(null);
|
||||
reload();
|
||||
return true;
|
||||
} catch (e: unknown) {
|
||||
setRemoveError({ detail: String(e) });
|
||||
return false;
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,13 +15,12 @@
|
|||
// endpoint today that can seed turn_state/model/ctx/cost synchronously —
|
||||
// `GET /api/agents/status`'s snapshot carries only
|
||||
// `status_text`/`running`).
|
||||
import { useEffect, useState } from "preact/hooks";
|
||||
import { useState } from "preact/hooks";
|
||||
|
||||
import {
|
||||
connectionAfterError,
|
||||
useEventStream,
|
||||
type ConnectionState,
|
||||
} from "../../ui/stream/connection.js";
|
||||
import { probeSession } from "../../ui/session/session.js";
|
||||
} from "../../ui/stream/useEventStream.js";
|
||||
|
||||
export type { ConnectionState };
|
||||
|
||||
|
|
@ -68,35 +67,23 @@ export function useSwarmAgentStateStream(
|
|||
streamUrl: string | null,
|
||||
): UseSwarmAgentStateStreamResult {
|
||||
const [header, setHeader] = useState<AgentStateHeader | null>(null);
|
||||
const [connection, setConnection] = useState<ConnectionState>("connecting");
|
||||
// Bumped by `reconnect`, which reruns the effect below.
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
// `null`: a terminal with no header to follow (a subagent's).
|
||||
const { connection, reconnect } = useEventStream(streamUrl, () => {
|
||||
setHeader(null);
|
||||
setConnection("connecting");
|
||||
// `null`: a terminal with no header to follow (a subagent's).
|
||||
if (streamUrl === null) return;
|
||||
const es = new EventSource(streamUrl);
|
||||
es.onopen = () => setConnection("open");
|
||||
es.onmessage = (e) => {
|
||||
try {
|
||||
setHeader(JSON.parse(e.data) as AgentStateHeader);
|
||||
} catch {
|
||||
// Malformed payload: drop it silently. Unlike the terminal
|
||||
// preview there is no row list to append a "[parse err]" line
|
||||
// to here, and one bad header isn't worth degrading the whole
|
||||
// badge cluster over — the next valid header replaces it.
|
||||
}
|
||||
return {
|
||||
onMessage: (data) => {
|
||||
try {
|
||||
setHeader(JSON.parse(data) as AgentStateHeader);
|
||||
} catch {
|
||||
// Malformed payload: drop it silently. Unlike the terminal
|
||||
// preview there is no row list to append a "[parse err]" line
|
||||
// to here, and one bad header isn't worth degrading the whole
|
||||
// badge cluster over — the next valid header replaces it.
|
||||
}
|
||||
},
|
||||
};
|
||||
es.onerror = () => {
|
||||
const state = connectionAfterError(es.readyState);
|
||||
setConnection(state);
|
||||
// The browser gave up: say whether the session ended, which `EventSource` does not tell.
|
||||
if (state === "closed") probeSession();
|
||||
};
|
||||
return () => es.close();
|
||||
}, [streamUrl, attempt]);
|
||||
}, []);
|
||||
|
||||
return { header, connection, reconnect: () => setAttempt((n) => n + 1) };
|
||||
return { header, connection, reconnect };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,14 +6,13 @@
|
|||
// resumes a reconnect, so rows are kept across one. Rows that arrive within
|
||||
// 50 ms are folded in together, and `term-rows.ts` holds the rules for that:
|
||||
// coalesce by key, and keep the newest `MAX_ROWS`.
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { useRef, useState } from "preact/hooks";
|
||||
import type { TermMsg, TermRow } from "@hive/shared/term-msg.js";
|
||||
|
||||
import {
|
||||
connectionAfterError,
|
||||
useEventStream,
|
||||
type ConnectionState,
|
||||
} from "../../ui/stream/connection.js";
|
||||
import { probeSession } from "../../ui/session/session.js";
|
||||
} from "../../ui/stream/useEventStream.js";
|
||||
import { appendRows } from "./term-rows.js";
|
||||
|
||||
export type { ConnectionState };
|
||||
|
|
@ -33,14 +32,10 @@ export function useSwarmTermStream(
|
|||
streamUrl: string,
|
||||
): UseSwarmTermStreamResult {
|
||||
const [rows, setRows] = useState<TermRow[]>([]);
|
||||
const [connection, setConnection] = useState<ConnectionState>("connecting");
|
||||
const keySeqRef = useRef(0);
|
||||
// Bumped by `reconnect`, which reruns the effect below.
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const { connection, reconnect } = useEventStream(streamUrl, () => {
|
||||
setRows([]);
|
||||
setConnection("connecting");
|
||||
keySeqRef.current = 0;
|
||||
let batch: TermRow[] = [];
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
|
@ -54,36 +49,29 @@ export function useSwarmTermStream(
|
|||
batch.push(row);
|
||||
if (timer === null) timer = setTimeout(flush, BATCH_MS);
|
||||
}
|
||||
const es = new EventSource(streamUrl);
|
||||
es.onopen = () => setConnection("open");
|
||||
es.onmessage = (e) => {
|
||||
keySeqRef.current += 1;
|
||||
const key = "r" + keySeqRef.current;
|
||||
let msg: TermMsg;
|
||||
try {
|
||||
msg = JSON.parse(e.data);
|
||||
} catch {
|
||||
push({
|
||||
key,
|
||||
level: "warn",
|
||||
fromHistory: false,
|
||||
summary: "[parse err] " + e.data,
|
||||
});
|
||||
return;
|
||||
}
|
||||
push({ ...msg, key, fromHistory: false });
|
||||
return {
|
||||
onMessage: (data) => {
|
||||
keySeqRef.current += 1;
|
||||
const key = "r" + keySeqRef.current;
|
||||
let msg: TermMsg;
|
||||
try {
|
||||
msg = JSON.parse(data);
|
||||
} catch {
|
||||
push({
|
||||
key,
|
||||
level: "warn",
|
||||
fromHistory: false,
|
||||
summary: "[parse err] " + data,
|
||||
});
|
||||
return;
|
||||
}
|
||||
push({ ...msg, key, fromHistory: false });
|
||||
},
|
||||
dispose: () => {
|
||||
if (timer !== null) clearTimeout(timer);
|
||||
},
|
||||
};
|
||||
es.onerror = () => {
|
||||
const state = connectionAfterError(es.readyState);
|
||||
setConnection(state);
|
||||
// The browser gave up: say whether the session ended, which `EventSource` does not tell.
|
||||
if (state === "closed") probeSession();
|
||||
};
|
||||
return () => {
|
||||
es.close();
|
||||
if (timer !== null) clearTimeout(timer);
|
||||
};
|
||||
}, [streamUrl, attempt]);
|
||||
}, []);
|
||||
|
||||
return { rows, connection, reconnect: () => setAttempt((n) => n + 1) };
|
||||
return { rows, connection, reconnect };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue