From 381c7f555ea5d0beef1ad1ef3ff79f403c2f08d2 Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Mon, 16 Nov 2026 16:49:47 +0100 Subject: [PATCH] feat(web): parameter fields in the text and collapsible groups (#16) Inline fields share the value with the parameter panel. Groups and included notebooks render as collapsible sections whose outline shows the input status (orange missing, green complete, grey optional) and remember their open state. The panel groups use the same colors. The authoring toolbar inserts groups and fields; the demo serves the guided example. --- apps/web/src/App.tsx | 92 +++++++++++------- apps/web/src/AuthoringToolbar.tsx | 32 +++++++ apps/web/src/GroupSection.tsx | 76 +++++++++++++++ apps/web/src/ParamsForm.tsx | 138 +++++++++++++++++---------- apps/web/src/authoring.ts | 23 ++++- apps/web/src/mock/api.ts | 49 ++++++---- apps/web/src/style.css | 102 +++++++++++++++++--- apps/web/src/tests/authoring.test.ts | 11 +++ apps/web/src/tests/groups.test.tsx | 99 +++++++++++++++++++ 9 files changed, 504 insertions(+), 118 deletions(-) create mode 100644 apps/web/src/GroupSection.tsx create mode 100644 apps/web/src/tests/groups.test.tsx diff --git a/apps/web/src/App.tsx b/apps/web/src/App.tsx index 14127ba..4439340 100644 --- a/apps/web/src/App.tsx +++ b/apps/web/src/App.tsx @@ -14,6 +14,7 @@ import { import ReactMarkdown from "react-markdown"; import { blockActive, + groupInputs, notebookIncludes, parseNotebook, renderHost, @@ -38,7 +39,8 @@ import { HostsPage } from "./HostsPage"; import { HttpError } from "./api"; import type { Api } from "./api"; import { emptySession, eventReducer, stepKey } from "./events"; -import { ParamsForm, type ParamGroup } from "./ParamsForm"; +import { ParamField, ParamsForm, type ParamGroup } from "./ParamsForm"; +import { GroupSection } from "./GroupSection"; import { HostBlockCard } from "./HostBlockCard"; import { StepCard } from "./StepCard"; import { ChatPanel } from "./ChatPanel"; @@ -55,21 +57,14 @@ const Editor = lazy(() => function steps(blocks: Block[]): Step[] { return blocks.flatMap((block) => - block.type === "step" - ? [block.step] - : block.type === "markdown" || block.type === "host" - ? [] - : steps(block.blocks), + block.type === "step" ? [block.step] : "blocks" in block ? steps(block.blocks) : [], ); } -/** Included notebooks (`::use`, nested too) as parameter groups. */ -function includedGroups(blocks: Block[]): ParamGroup[] { - return blocks.flatMap((block): ParamGroup[] => - block.type === "use" - ? [{ scope: block.scope, title: `${block.title ?? block.path} (${block.scope})` }, ...includedGroups(block.blocks)] - : block.type === "after" || block.type === "when" - ? includedGroups(block.blocks) - : [], +type UseBlock = Extract; +/** Included notebooks (`::use`, nested too), whose parameters the form shows as groups. */ +function includes(blocks: Block[]): UseBlock[] { + return blocks.flatMap((block): UseBlock[] => + block.type === "use" ? [block, ...includes(block.blocks)] : "blocks" in block ? includes(block.blocks) : [], ); } /** Values that are set; unset ones stay visible as `{{name}}` in prose. */ @@ -239,8 +234,14 @@ export function App({ api }: { api: Api }) { ? resolveParams(notebook, values, hostNames, runtime) : { values: {}, errors: {} }; const active = (id: string) => !!notebook && blockActive(notebook, id, resolved.values); - const groups = notebook - ? includedGroups(notebook.blocks).filter((group) => scopeActive(notebook, group.scope, resolved.values)) + const groups: ParamGroup[] = notebook + ? includes(notebook.blocks) + .filter((block) => scopeActive(notebook, block.scope, resolved.values)) + .map((block) => ({ + scope: block.scope, + title: `${block.title ?? block.path} (${block.scope})`, + status: groupInputs(notebook, block, values, hostNames, runtime).status, + })) : []; const activeChecks = notebook ? steps(notebook.blocks).filter((s) => s.kind === "check" && active(s.attrs.id)) @@ -302,21 +303,46 @@ export function App({ api }: { api: Api }) { setConfirm(request); else void start(request); } + const changeValue = (name: string, value: string) => changeValues({ ...values, [name]: value }); function blocks(items: Block[]): React.ReactNode { if (!notebook) return null; + const prose = (text: string) => renderText(text, setValuesOnly({ ...runtime, ...resolved.values }), notebook); return items.map((block, i) => { if (block.type === "markdown") return (
- - {renderText( - block.text, - setValuesOnly({ ...runtime, ...resolved.values }), - notebook, - )} - + {prose(block.text)}
); + if (block.type === "param") { + const def = notebook.params.find((param) => param.name === block.name); + // The parser reports unknown names; the panel still lists every parameter. + return def ? ( +
+ +
+ ) : null; + } + if (block.type === "group") + return ( + + {blocks(block.blocks)} + + ); if (block.type === "when") return block.values.includes(resolved.values[block.param] ?? "") ? (
@@ -325,19 +351,21 @@ export function App({ api }: { api: Api }) { ) : null; if (block.type === "use") return ( -
- - {block.title ?? block.path} - - {block.path} · as {block.scope} - - + {block.blocks.length ? ( blocks(block.blocks) ) : (

Save the notebook to load {block.path}.

)} -
+ ); if (block.type === "host") { const rendered = renderHost(notebook, block, resolved.values, runtime, hostNames); @@ -599,7 +627,7 @@ export function App({ api }: { api: Api }) { writeFlag("params-open", next); setParamsOpen(next); }} - onChange={(name, value) => changeValues({ ...values, [name]: value })} + onChange={changeValue} onReset={() => changeValues({})} />
diff --git a/apps/web/src/AuthoringToolbar.tsx b/apps/web/src/AuthoringToolbar.tsx index d78a2e9..599160a 100644 --- a/apps/web/src/AuthoringToolbar.tsx +++ b/apps/web/src/AuthoringToolbar.tsx @@ -3,7 +3,9 @@ import { parseNotebook, type ParamDef } from "@shellbook/core"; import { addParameter, afterSnippet, + groupSnippet, hostSnippet, + paramFieldSnippet, notebookSteps, stepSnippet, useSnippet, @@ -28,6 +30,7 @@ export function AuthoringToolbar({ const [after, setAfter] = useState(""); const [when, setWhen] = useState(""); const [include, setInclude] = useState(false); + const [field, setField] = useState(""); const steps = notebookSteps(notebook.blocks); const choices = notebook.params.flatMap((p) => p.type === "choice" ? (p.options ?? []).map((o) => ({ param: p.name, option: o })) : [], @@ -104,6 +107,27 @@ export function AuthoringToolbar({ > Insert when section + + + @@ -227,6 +251,14 @@ export function AuthoringToolbar({ adds a created machine to Hosts. {"{{shellbook.public_key}}"} is ShellBook's SSH public key.

+

+ Guided text: {"::param{name=host}"} on its own line shows that + parameter's input there (same value as the panel).{" "} + {':::group{title="Network" collapsed}'} … {":::"} is a collapsible + section for anything; its outline is orange while needed values are + missing, green when all are filled, grey when only optional ones remain. + {" collapsed"} starts it closed. Groups never block steps. +

Artifact and upload snippets are examples: choose files and commands appropriate to your runbook before running. diff --git a/apps/web/src/GroupSection.tsx b/apps/web/src/GroupSection.tsx new file mode 100644 index 0000000..9b6eb22 --- /dev/null +++ b/apps/web/src/GroupSection.tsx @@ -0,0 +1,76 @@ +import { useState, type ReactNode } from "react"; +import type { GroupInputs, ParamDef } from "@shellbook/core"; +import { paramLabel } from "./ParamsForm"; +import { readFlag, writeFlag } from "./uiPrefs"; + +/** What the outline color means, in words (color is never the only signal). */ +function statusText(inputs: GroupInputs, labelOf: (name: string) => string): string { + if (inputs.status === "missing") { + const names = [...inputs.missing, ...inputs.invalid]; + const shown = names.slice(0, 3).map(labelOf).join(", "); + const more = names.length > 3 ? ` +${names.length - 3}` : ""; + return inputs.missing.length ? `needs ${shown}${more}` : `check ${shown}${more}`; + } + if (inputs.status === "filled") return "complete"; + if (inputs.status === "optional") return "optional"; + return ""; +} + +/** + * A titled, collapsible container (`:::group`, and `::use` sections). The outline shows + * whether its inputs are filled in: orange — needed values missing, green — complete, + * grey — optional or no inputs. The open state is remembered per `storageKey`. + */ +export function GroupSection({ + title, + subtitle, + inputs, + params, + storageKey, + defaultOpen = true, + className = "", + children, +}: { + title: string; + subtitle?: string; + inputs: GroupInputs; + /** The notebook's parameters, for labels in the status text. */ + params: ParamDef[]; + storageKey: string; + defaultOpen?: boolean; + className?: string; + children: ReactNode; +}) { + const flag = `group:${storageKey}`; + const [open, setOpen] = useState(() => readFlag(flag, defaultOpen)); + const labelOf = (name: string) => { + const def = params.find((param) => param.name === name); + return def ? paramLabel(def) : name; + }; + const status = statusText(inputs, labelOf); + return ( +

+
+ + {subtitle && {subtitle}} + {status && {status}} +
+ +
+ ); +} diff --git a/apps/web/src/ParamsForm.tsx b/apps/web/src/ParamsForm.tsx index dce4bbc..b239ad8 100644 --- a/apps/web/src/ParamsForm.tsx +++ b/apps/web/src/ParamsForm.tsx @@ -1,15 +1,84 @@ import { useEffect, useState } from "react"; -import type { HostInfo, Notebook, ParamDef, ParamValues } from "@shellbook/core"; +import type { HostInfo, InputStatus, Notebook, ParamDef, ParamValues } from "@shellbook/core"; import { readFlag, writeFlag } from "./uiPrefs"; /** An included notebook (`::use`) whose parameters the form shows as a group. */ export interface ParamGroup { scope: string; title: string; + /** Input status of the included notebook (core groupInputs), shown as the outline color. */ + status?: InputStatus; } const groupFlag = (title: string) => `params-group:${title}`; +/** The label a parameter is shown with; included ones without a label use their local name. */ +export const paramLabel = (param: ParamDef) => + param.label ?? (param.scope ? param.name.slice(param.scope.length + 1) : param.name); + +/** + * One parameter input, used by the parameter panel and by `::param` fields in the text. + * `idPrefix` keeps element ids unique when the same parameter appears in both places. + */ +export function ParamField({ + param, + value, + error, + hosts, + idPrefix = "param", + onChange, +}: { + param: ParamDef; + value: string | undefined; + error: string | undefined; + hosts: HostInfo[]; + idPrefix?: string; + onChange(name: string, value: string): void; +}) { + const id = `${idPrefix}-${param.name}`; + const label = paramLabel(param); + const common = { + id, + required: param.required, + value: value ?? "", + "aria-labelledby": `${id}-label`, + "aria-invalid": !!error, + // Red only for a wrong value; an empty field just still needs one. + className: error && value ? "invalid" : undefined, + "aria-describedby": error ? `${id}-error` : undefined, + onChange: (e: React.ChangeEvent) => onChange(param.name, e.target.value), + }; + return ( + + ); +} + export function ParamsForm({ notebook, values, @@ -58,8 +127,7 @@ export function ParamsForm({ const missingOf = (params: ParamDef[]) => params.filter((param) => param.required && !values[param.name]).length; const missing = missingOf(shown); - const labelOf = (param: ParamDef) => - param.label ?? (param.scope ? param.name.slice(param.scope.length + 1) : param.name); + const labelOf = paramLabel; const reveal = (param: ParamDef) => { if (!open) onToggle?.(true); @@ -107,54 +175,16 @@ export function ParamsForm({ ); }; - const field = (param: ParamDef) => { - const id = `param-${param.name}`; - const common = { - id, - required: param.required, - value: values[param.name] ?? "", - "aria-labelledby": `${id}-label`, - "aria-invalid": !!errors[param.name], - // Red only for a wrong value; an empty field just still needs one. - className: errors[param.name] && values[param.name] ? "invalid" : undefined, - "aria-describedby": errors[param.name] ? `${id}-error` : undefined, - onChange: ( - e: React.ChangeEvent, - ) => onChange(param.name, e.target.value), - }; - return ( - - ); - }; + const field = (param: ParamDef) => ( + + ); return (
+