Files
ShellBook/apps/web/src/ParamsForm.tsx
T
Florian Schmidt e38f9c453c feat(web): rework layout and quality of life (#12)
- App shell: top bar with connection state and Chat/Terminal toggles,
  per-column scrolling, sidebar only on the Notebooks page.
- Hash routes (#/notebooks/<path>, #/hosts, #/settings) so reloads and
  links keep the open notebook and page.
- Parameters sit beside the notebook on wide screens, collapsible, with
  missing-count summary and Reset; non-secret values are remembered per
  notebook in localStorage (secrets and undeclared names never stored).
- Sticky notebook header with breadcrumbs, check summary and run-all;
  Ctrl/Cmd+S saves the Markdown draft.
- Step cards show target, one deduplicated hint for missing values and
  copy feedback.
- Shared Dialog, Menu and CopyButton components; notebook and host
  actions move into menus and modal dialogs.
- Hosts page as a compact list; Settings split into cards with sign out
  for this browser.
2026-11-16 12:32:01 +01:00

123 lines
3.9 KiB
TypeScript

import { useState } from "react";
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,
onReset,
}: {
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;
/** Clears the values typed for this notebook. */
onReset?(): void;
}) {
const [open, setOpen] = useState(true);
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],
// 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<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] && (
// An empty field only needs a value: say so quietly; a wrong value is an error.
<small id={`${id}-error`} className={values[param.name] ? "error" : "muted"}>
{errors[param.name]}
</small>
)}
</label>
);
};
const shown = [
...notebook.params.filter((param) => !param.scope),
...groups.flatMap((group) => notebook.params.filter((param) => param.scope === group.scope)),
];
const missing = shown.filter((param) => param.required && !values[param.name]).length;
if (!shown.length) return null;
return (
<form
className="params"
aria-label="Notebook parameters"
onSubmit={(e) => e.preventDefault()}
>
<header>
<h2>Parameters</h2>
<small className={missing ? "pending" : "ok"}>
{missing ? `${missing} required missing` : "All required set"}
</small>
{onReset && open && (
<button type="button" className="link" onClick={onReset}>
Reset
</button>
)}
<button
type="button"
className="link"
aria-expanded={open}
onClick={() => setOpen(!open)}
>
{open ? "Hide" : "Show"}
</button>
</header>
{open && notebook.params.filter((param) => !param.scope).map(field)}
{open && 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>
);
}