feat: chained notebooks with when sections and host blocks (#11)
A notebook can include another one and continue with what it produced:
- core: parse :::when{param=a,b}, ::use{notebook= as= with=} and
::host{name= address= user= port= capture=}; composeNotebook namespaces
an included notebook's params, steps, captures and templates by alias
(cycles, missing files, depth > 4 and child errors are reported on the
use line). Defaults may reference captures and builtins
({{shellbook.public_key}}); required params and secrets gate only steps
of their own scope; secretEnv passes a scope's secrets under local names.
- protocol: NotebookResponse gains includes and builtins.
- server: compose includes on load, inject builtins (client values are
ignored), refuse steps in inactive when sections.
- web: render when/use/host blocks, group included params in the form,
add a created machine to Hosts and capture its name, reload when an
included notebook changes, run all checks only for active steps,
authoring snippets for when, use and host.
This commit is contained in:
+65
-45
@@ -1,68 +1,88 @@
|
||||
import type { HostInfo, Notebook, ParamValues } from "@shellbook/core";
|
||||
import type { HostInfo, Notebook, ParamDef, ParamValues } from "@shellbook/core";
|
||||
|
||||
/** An included notebook (`::use`) whose parameters the form shows as a group. */
|
||||
export interface ParamGroup {
|
||||
scope: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
export function ParamsForm({
|
||||
notebook,
|
||||
values,
|
||||
errors,
|
||||
hosts,
|
||||
groups = [],
|
||||
onChange,
|
||||
}: {
|
||||
notebook: Notebook;
|
||||
values: ParamValues;
|
||||
errors: Record<string, string>;
|
||||
hosts: HostInfo[];
|
||||
/** Active included notebooks; parameters of inactive ones are hidden. */
|
||||
groups?: ParamGroup[];
|
||||
onChange(name: string, value: string): void;
|
||||
}) {
|
||||
const field = (param: ParamDef) => {
|
||||
const id = `param-${param.name}`;
|
||||
const local = param.scope ? param.name.slice(param.scope.length + 1) : param.name;
|
||||
const common = {
|
||||
id,
|
||||
required: param.required,
|
||||
value: values[param.name] ?? "",
|
||||
"aria-labelledby": `${id}-label`,
|
||||
"aria-invalid": !!errors[param.name],
|
||||
"aria-describedby": errors[param.name] ? `${id}-error` : undefined,
|
||||
onChange: (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
||||
) => onChange(param.name, e.target.value),
|
||||
};
|
||||
return (
|
||||
<label key={param.name} htmlFor={id}>
|
||||
<span id={`${id}-label`}>
|
||||
{param.label ?? local}
|
||||
{param.required && <b title="Required"> *</b>}
|
||||
</span>
|
||||
{param.type === "host" || param.type === "choice" ? (
|
||||
<select {...common}>
|
||||
<option value="">Select…</option>
|
||||
{(param.type === "host"
|
||||
? hosts.map((h) => h.name)
|
||||
: (param.options ?? [])
|
||||
).map((option) => (
|
||||
<option key={option}>{option}</option>
|
||||
))}
|
||||
</select>
|
||||
) : (
|
||||
<input
|
||||
{...common}
|
||||
type={param.type === "secret" ? "password" : "text"}
|
||||
autoComplete={param.type === "secret" ? "new-password" : "off"}
|
||||
spellCheck={false}
|
||||
/>
|
||||
)}
|
||||
{errors[param.name] && (
|
||||
<small id={`${id}-error`} className="error">
|
||||
{errors[param.name]}
|
||||
</small>
|
||||
)}
|
||||
</label>
|
||||
);
|
||||
};
|
||||
return (
|
||||
<form
|
||||
className="params"
|
||||
aria-label="Notebook parameters"
|
||||
onSubmit={(e) => e.preventDefault()}
|
||||
>
|
||||
{notebook.params.map((param) => {
|
||||
const id = `param-${param.name}`;
|
||||
const common = {
|
||||
id,
|
||||
required: param.required,
|
||||
value: values[param.name] ?? "",
|
||||
"aria-labelledby": `${id}-label`,
|
||||
"aria-invalid": !!errors[param.name],
|
||||
"aria-describedby": errors[param.name] ? `${id}-error` : undefined,
|
||||
onChange: (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
||||
) => onChange(param.name, e.target.value),
|
||||
};
|
||||
return (
|
||||
<label key={param.name} htmlFor={id}>
|
||||
<span id={`${id}-label`}>
|
||||
{param.label ?? param.name}
|
||||
{param.required && <b title="Required"> *</b>}
|
||||
</span>
|
||||
{param.type === "host" || param.type === "choice" ? (
|
||||
<select {...common}>
|
||||
<option value="">Select…</option>
|
||||
{(param.type === "host"
|
||||
? hosts.map((h) => h.name)
|
||||
: (param.options ?? [])
|
||||
).map((option) => (
|
||||
<option key={option}>{option}</option>
|
||||
))}
|
||||
</select>
|
||||
) : (
|
||||
<input
|
||||
{...common}
|
||||
type={param.type === "secret" ? "password" : "text"}
|
||||
autoComplete={param.type === "secret" ? "new-password" : "off"}
|
||||
spellCheck={false}
|
||||
/>
|
||||
)}
|
||||
{errors[param.name] && (
|
||||
<small id={`${id}-error`} className="error">
|
||||
{errors[param.name]}
|
||||
</small>
|
||||
)}
|
||||
</label>
|
||||
);
|
||||
{notebook.params.filter((param) => !param.scope).map(field)}
|
||||
{groups.map((group) => {
|
||||
const params = notebook.params.filter((param) => param.scope === group.scope);
|
||||
return params.length ? (
|
||||
<fieldset key={group.scope}>
|
||||
<legend>{group.title}</legend>
|
||||
{params.map(field)}
|
||||
</fieldset>
|
||||
) : null;
|
||||
})}
|
||||
</form>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user