swarm-ui: preview a draft flow's hit counts in the editor with the dry-run route
The editor's preview button posts the draft to POST /api/notify/dry-run and puts each box's hit count on the canvas; any edit hides the numbers. Boxes of kind request_reviewer label themselves "reviewer", so the long kind name no longer runs into the hit count shown beside it.
This commit is contained in:
parent
e8538d8690
commit
404d8334aa
1 changed files with 97 additions and 6 deletions
|
|
@ -38,6 +38,7 @@ import {
|
|||
reparent,
|
||||
savedAs,
|
||||
saveOutcome,
|
||||
previewRequest,
|
||||
saveRequest,
|
||||
seedFlow,
|
||||
parentOf,
|
||||
|
|
@ -48,6 +49,7 @@ import {
|
|||
import { useLeaveGuard } from "./useLeaveGuard.js";
|
||||
import { NodeInspector } from "./NodeInspector.js";
|
||||
import { FALLBACK_SCHEMA, type Flow, type FlowsView } from "./FlowTypes.js";
|
||||
import { DRY_RUN_URL, hitsOf, type DryRun } from "./RouterLive.js";
|
||||
import "./NotificationsPage.css";
|
||||
|
||||
const FLOWS_URL = "/api/notify/flows";
|
||||
|
|
@ -75,6 +77,14 @@ export function FlowEditorPage({
|
|||
const [saveError, setSaveError] = useState<ProblemDetails | null>(null);
|
||||
const [stale, setStale] = useState(false);
|
||||
const [notice, setNotice] = useState<string | null>(null);
|
||||
// The last preview and the draft it ran, which stops counting once the
|
||||
// draft is edited.
|
||||
const [preview, setPreview] = useState<{
|
||||
draft: Flow;
|
||||
flowId: string;
|
||||
dry: DryRun;
|
||||
} | null>(null);
|
||||
const [previewError, setPreviewError] = useState<ProblemDetails | null>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [confirmDelete, setConfirmDelete] = useState(false);
|
||||
|
||||
|
|
@ -148,6 +158,32 @@ export function FlowEditorPage({
|
|||
return false;
|
||||
}
|
||||
|
||||
// Run the draft, saved or not, over the newest forge events, and put the
|
||||
// number each node would have caught on its box.
|
||||
async function runPreview() {
|
||||
if (!view || !draft) return;
|
||||
const flow = isNew ? savedAs(view, draft) : draft;
|
||||
setBusy(true);
|
||||
setPreviewError(null);
|
||||
try {
|
||||
const r = await fetch(
|
||||
DRY_RUN_URL,
|
||||
previewRequest(withFlow(view.flows, flow), view.disabled_builtins),
|
||||
);
|
||||
if (r.ok) {
|
||||
setPreview({ draft, flowId: flow.id, dry: (await r.json()) as DryRun });
|
||||
} else {
|
||||
setPreview(null);
|
||||
setPreviewError(await readApiError(r));
|
||||
}
|
||||
} catch (err) {
|
||||
setPreview(null);
|
||||
setPreviewError({ detail: String(err) });
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
function onEdit(result: Edit, select?: string) {
|
||||
if (!result.ok) {
|
||||
setNotice(result.reason);
|
||||
|
|
@ -217,6 +253,8 @@ export function FlowEditorPage({
|
|||
const schema = view.schema ?? FALLBACK_SCHEMA;
|
||||
const limits = limitsOf(view.schema);
|
||||
const node = draft.nodes.find((n) => n.id === selected) ?? null;
|
||||
const shown = preview?.draft === draft ? preview : null;
|
||||
const hits = shown ? hitsOf(shown.dry, shown.flowId) : undefined;
|
||||
|
||||
return (
|
||||
<Panel
|
||||
|
|
@ -241,6 +279,12 @@ export function FlowEditorPage({
|
|||
{saveError ? (
|
||||
<ApiErrorPanel context="the flow was not saved" problem={saveError} />
|
||||
) : null}
|
||||
{previewError ? (
|
||||
<ApiErrorPanel
|
||||
context="the preview did not run"
|
||||
problem={previewError}
|
||||
/>
|
||||
) : null}
|
||||
{notice ? (
|
||||
<p class="notify-notice" role="status">
|
||||
{notice}
|
||||
|
|
@ -288,6 +332,9 @@ export function FlowEditorPage({
|
|||
>
|
||||
discard edits
|
||||
</Button>
|
||||
<Button disabled={busy} onClick={() => void runPreview()}>
|
||||
preview
|
||||
</Button>
|
||||
{isNew ? null : (
|
||||
<Button disabled={busy} onClick={() => setConfirmDelete(true)}>
|
||||
delete flow
|
||||
|
|
@ -295,6 +342,13 @@ export function FlowEditorPage({
|
|||
)}
|
||||
</div>
|
||||
</div>
|
||||
{shown ? (
|
||||
<p class="notify-hint" role="status">
|
||||
{hits
|
||||
? `Preview: each box shows how many of the newest ${shown.dry.events} forge events reached it. Edit the flow and the numbers go.`
|
||||
: "The preview skipped this flow because it is off."}
|
||||
</p>
|
||||
) : null}
|
||||
|
||||
<div class="notify-editor-body">
|
||||
<FlowCanvas
|
||||
|
|
@ -302,6 +356,7 @@ export function FlowEditorPage({
|
|||
selected={selected}
|
||||
limits={limits}
|
||||
palette={paletteKinds(schema)}
|
||||
hits={hits}
|
||||
onSelect={setSelected}
|
||||
onAdd={(kind, parentId) => {
|
||||
const added = blankNode(draft, kind);
|
||||
|
|
|
|||
Loading…
Reference in a new issue