Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/pages/notifications/NodeInspector.tsx
iris d0a55502a8 swarm-ui: draw notification flows as graphs in the list and make a new or copied flow a draft until saved
The flows list draws each flow as the editor's graph, read-only and with the
dry run's hit counts in the boxes, in place of the indented tree. A box's line
of text is cut by the browser at the box's edge, so it no longer runs out of it.

create flow (a primary button beside the refresh interval) and duplicate open
the editor on an unsaved draft at /notifications/new, with ?from= naming the
flow to copy; the flow is made by save, under an id taken from its name. Edit
and duplicate are buttons in the flow's header row. The editor's head and
inspector are laid out as sections.
2026-10-25 02:05:54 +02:00

339 lines
9.3 KiB
TypeScript

// <NodeInspector> — the fields of the picked node, and what can be done to
// it: move it under another node, change when it runs among its siblings,
// remove it. Every change goes out as an `Edit` of the flow; the editor shows
// the reason when one is refused. New nodes come from the canvas palette.
import { Button } from "../../ui/button/Button.js";
import { Card } from "../../ui/card/Card.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 {
moveAmongSiblings,
parentOf,
removeNode,
reparent,
replaceNode,
siblingPosition,
type Edit,
type Limits,
withOp,
} from "./FlowDraft.js";
import {
kindValues,
ordinal,
type Condition,
type Described,
type Flow,
type FlowNode,
type FlowSchema,
} from "./FlowTypes.js";
type TodoNode = Extract<FlowNode, { kind: "todo" }>;
const OPS_FALLBACK = ["upsert", "clear"];
// The kinds this editor has fields for.
const KNOWN_KINDS: string[] = ["source", "match", "todo", "request_reviewer"];
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 parent = parentOf(flow, node.id);
// Where it can go: under its current parent, or under any node it could be
// moved to.
const parents = flow.nodes
.filter(
(n) =>
n.id === parent ||
(n.id !== node.id && reparent(flow, node.id, n.id, limits).ok),
)
.map((n) => ({ value: n.id, label: `${n.kind} ${n.id}` }));
const order = siblingPosition(flow, node.id);
return (
<Card 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}
{node.kind === "request_reviewer" ? (
<fieldset>
<legend>asks for a review</legend>
<TextField
id={`${key}-reviewer`}
label="reviewer (a forge login)"
value={node.reviewer}
onInput={(reviewer) => set({ ...node, reviewer })}
/>
</fieldset>
) : null}
{!KNOWN_KINDS.includes(node.kind) ? (
<p class="notify-hint">
This editor does not know how to edit a {node.kind}. It is kept as it
is when you save.
</p>
) : null}
{order.count > 1 ? (
<div class="notify-inspector-actions">
<span class="notify-hint">
runs {ordinal(order.index + 1)} of {order.count} below its parent
</span>
<Button
disabled={order.index === 0}
onClick={() => onEdit(moveAmongSiblings(flow, node.id, -1))}
>
run earlier
</Button>
<Button
disabled={order.index === order.count - 1}
onClick={() => onEdit(moveAmongSiblings(flow, node.id, 1))}
>
run later
</Button>
</div>
) : null}
<div class="notify-inspector-actions">
{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}
</Card>
);
}
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(withOp(node, op as TodoNode["op"]))}
/>
<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>
);
}