Files
ShellBook/apps/web/src/HostBlockCard.tsx
T
Florian Schmidt 845d6c828e 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.
2026-11-16 11:42:12 +01:00

100 lines
2.8 KiB
TypeScript

import { useState } from "react";
import type { HostInfo, RenderedHost } from "@shellbook/core";
/**
* A notebook's `::host` block: adds the machine a notebook created (e.g. an LXC container)
* to Hosts, so later steps can target it. Never overwrites a different existing host.
*/
export function HostBlockCard({
rendered,
existing,
captured,
onAdd,
onUse,
}: {
rendered: RenderedHost;
/** Host with the same name already in Hosts. */
existing?: HostInfo;
/** Runtime value of the block's capture, if any. */
captured?: string;
onAdd(host: HostInfo): Promise<void>;
onUse(name: string): void;
}) {
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const problems = Object.entries(rendered.invalid);
const ready = !rendered.missing.length && !problems.length;
const same =
!!existing &&
existing.address === rendered.address &&
existing.user === rendered.user &&
existing.port === rendered.port;
async function add() {
setBusy(true);
setError("");
try {
await onAdd({
name: rendered.name,
address: rendered.address,
user: rendered.user,
port: rendered.port,
tags: [],
});
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
} finally {
setBusy(false);
}
}
return (
<article className="step host-block" aria-label={`Add host ${rendered.name}`}>
<header>
<strong>Add to Hosts</strong>
<code>
{rendered.user}@{rendered.address}:{rendered.port}
</code>
<span>as {rendered.name}</span>
</header>
{rendered.missing.length > 0 && (
<p className="muted">
Available after: {rendered.missing.join(", ")}
</p>
)}
{problems.map(([field, message]) => (
<p className="error" key={field}>
{message}
</p>
))}
{ready &&
(existing ? (
same ? (
captured === existing.name ? (
<p role="status">✓ {existing.name} is in Hosts</p>
) : (
<button onClick={() => onUse(existing.name)}>
Use existing host {existing.name}
</button>
)
) : (
<p className="error" role="alert">
A different host named {rendered.name} already exists. Choose
another name or change it under Hosts.
</p>
)
) : (
<button className="primary" disabled={busy} onClick={() => void add()}>
{busy ? "Adding…" : "Add to Hosts"}
</button>
))}
{error && (
<p className="error" role="alert">
{error}
</p>
)}
<p className="muted">
The first connection asks you to confirm the new server's host key.
</p>
</article>
);
}