Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: edit a notification flow on a canvas

A custom flow opens at /notifications/<id>: its nodes drawn left to right
(source, then matches, then todos), and a panel to edit the picked node: the
source's events, a match's conditions, a todo's recipients, op, key and text.
Add a match or todo below a node, move a branch under another match, or remove
it; an edit the controller would refuse (a todo with children, a second
parent, a cycle, over the node or depth cap) is turned away with the reason.

Edits stay in a draft until save, which sends the whole document at the
revision read. A stale revision is told apart from a refused document: the
editor offers to reload or to keep the draft on top of the latest. The form
choices come from the controller's schema when it sends one.

The list page gains edit and duplicate on custom flows, duplicate on the
built-ins, and a new-flow box; a new flow's id avoids the built-ins' too.
This commit is contained in:
iris 2026-10-24 14:00:44 +02:00 • committed by mara
commit f28e1e9063

View file

@ -0,0 +1,305 @@
// <NodeInspector> — the fields of the picked node, and what can be done to
// it: add a child, move it under another node, remove it. Every change goes
// out as an `Edit` of the flow; the editor shows the reason when one is
// refused.
import { Button } from "../../ui/button/Button.js";
import { CheckboxField } from "../../ui/checkbox-field/CheckboxField.js";
import { SelectField } from "../../ui/select-field/SelectField.js";
import { TextAreaField } from "../../ui/text-area-field/TextAreaField.js";
import { TextField } from "../../ui/text-field/TextField.js";
import {
addChild,
blankNode,
parentOf,
removeNode,
reparent,
replaceNode,
type Edit,
type Limits,
} from "./FlowDraft.js";
import {
kindValues,
type Condition,
type Described,
type Flow,
type FlowNode,
type FlowSchema,
} from "./FlowTypes.js";
type TodoNode = Extract<FlowNode, { kind: "todo" }>;
const OPS_FALLBACK = ["upsert", "clear"];
function options(list: Described[]) {
return list.map((d) => ({
value: d.name,
label: d.description ? `${d.name} — ${d.description}` : d.name,
}));
}
export function NodeInspector({
flow,
node,
schema,
limits,
onEdit,
}: {
flow: Flow;
node: FlowNode;
schema: FlowSchema;
limits: Limits;
// `select` is the node to pick once the edit is applied.
onEdit: (edit: Edit, select?: string) => void;
}) {
const set = (next: FlowNode) => onEdit(replaceNode(flow, next));
const fieldOptions = options(schema.fields);
const firstField = schema.fields[0]?.name ?? "";
const key = `ne-${node.id}`;
const add = (kind: "match" | "todo") => {
const added = blankNode(flow, kind);
onEdit(addChild(flow, node.id, added, limits), added.id);
};
const parent = parentOf(flow, node.id);
const parents = flow.nodes
.filter((n) => n.id !== node.id && reparent(flow, node.id, n.id, limits).ok)
.map((n) => ({ value: n.id, label: `${n.kind} ${n.id}` }));
return (
<div class="notify-inspector">
<h4>
{node.kind}{" "}
{node.id !== node.kind ? (
<span class="notify-node-id">{node.id}</span>
) : null}
</h4>
{node.kind === "source" ? (
<fieldset>
<legend>forge events</legend>
{schema.source_events.map((ev) => (
<CheckboxField
key={ev.name}
id={`${key}-ev-${ev.name}`}
label={
ev.description ? `${ev.name} — ${ev.description}` : ev.name
}
checked={node.events.includes(ev.name)}
onChange={(on) =>
set({
...node,
events: on
? [...node.events, ev.name]
: node.events.filter((e) => e !== ev.name),
})
}
/>
))}
</fieldset>
) : null}
{node.kind === "match" ? (
<MatchFields
node={node}
keyPrefix={key}
fieldOptions={fieldOptions}
firstField={firstField}
onChange={set}
/>
) : null}
{node.kind === "todo" ? (
<TodoFields
node={node}
keyPrefix={key}
schema={schema}
fieldOptions={fieldOptions}
firstField={firstField}
onChange={set}
/>
) : null}
<div class="notify-inspector-actions">
{node.kind !== "todo" ? (
<>
<Button onClick={() => add("match")}>add a match below</Button>
<Button onClick={() => add("todo")}>add a todo below</Button>
</>
) : null}
{node.kind !== "source" ? (
<Button
onClick={() =>
onEdit(removeNode(flow, node.id), parent ?? undefined)
}
>
remove
{flow.edges.some((e) => e.from === node.id)
? " with its branch"
: ""}
</Button>
) : null}
</div>
{node.kind !== "source" && parent !== undefined ? (
<SelectField
id={`${key}-parent`}
label="move under"
value={parent}
options={parents}
onChange={(p) => onEdit(reparent(flow, node.id, p, limits))}
/>
) : null}
</div>
);
}
function MatchFields({
node,
keyPrefix,
fieldOptions,
firstField,
onChange,
}: {
node: Extract<FlowNode, { kind: "match" }>;
keyPrefix: string;
fieldOptions: { value: string; label: string }[];
firstField: string;
onChange: (next: FlowNode) => void;
}) {
const setWhen = (when: Condition[]) => onChange({ ...node, when });
return (
<fieldset>
<legend>passes when all of these hold</legend>
{node.when.length === 0 ? <p>no condition: passes everything</p> : null}
{node.when.map((c, i) => (
<div class="notify-condition" key={i}>
<SelectField
id={`${keyPrefix}-c${i}-field`}
label="field"
value={c.field}
options={fieldOptions}
onChange={(field) =>
setWhen(node.when.map((x, j) => (j === i ? { ...x, field } : x)))
}
/>
<TextField
id={`${keyPrefix}-c${i}-equals`}
label="equals"
value={c.equals}
onInput={(equals) =>
setWhen(node.when.map((x, j) => (j === i ? { ...x, equals } : x)))
}
/>
<Button onClick={() => setWhen(node.when.filter((_, j) => j !== i))}>
remove condition
</Button>
</div>
))}
<Button
onClick={() =>
setWhen([...node.when, { field: firstField, equals: "" }])
}
>
add a condition
</Button>
</fieldset>
);
}
function TodoFields({
node,
keyPrefix,
schema,
fieldOptions,
firstField,
onChange,
}: {
node: TodoNode;
keyPrefix: string;
schema: FlowSchema;
fieldOptions: { value: string; label: string }[];
firstField: string;
onChange: (next: FlowNode) => void;
}) {
const ops = kindValues(schema, "todo", "op");
const opList = (ops.length > 0 ? ops : OPS_FALLBACK).map((o) => ({
value: o,
label: o,
}));
const byField = "field" in node.to;
const hint = schema.fields.map((f) => `{${f.name}}`).join(" ");
return (
<fieldset>
<legend>emits a todo</legend>
<SelectField
id={`${keyPrefix}-op`}
label="op"
value={node.op}
options={opList}
onChange={(op) =>
onChange({
...node,
op: op as TodoNode["op"],
// A clear carries no text.
text: op === "clear" ? "" : node.text,
})
}
/>
<SelectField
id={`${keyPrefix}-to-kind`}
label="to"
value={byField ? "field" : "agent"}
options={[
{ value: "field", label: "the agents an event field names" },
{ value: "agent", label: "one agent" },
]}
onChange={(kind) =>
onChange({
...node,
to: kind === "field" ? { field: firstField } : { agent: "" },
})
}
/>
{"field" in node.to ? (
<SelectField
id={`${keyPrefix}-to-field`}
label="event field"
value={node.to.field}
options={fieldOptions}
onChange={(field) => onChange({ ...node, to: { field } })}
/>
) : (
<TextField
id={`${keyPrefix}-to-agent`}
label="agent"
value={node.to.agent}
onInput={(agent) => onChange({ ...node, to: { agent } })}
/>
)}
<TextField
id={`${keyPrefix}-key`}
label="todo key"
value={node.key}
onInput={(k) => onChange({ ...node, key: k })}
/>
{node.op === "clear" ? (
<CheckboxField
id={`${keyPrefix}-prefix`}
label="the key is a prefix"
checked={node.prefix}
onChange={(prefix) => onChange({ ...node, prefix })}
/>
) : (
<TextAreaField
id={`${keyPrefix}-text`}
label="todo text"
value={node.text}
rows={3}
onInput={(text) => onChange({ ...node, text })}
/>
)}
<p class="notify-hint">The key and text can use {hint}</p>
</fieldset>
);
}