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; 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 (
Add to Hosts {rendered.user}@{rendered.address}:{rendered.port} as {rendered.name}
{rendered.missing.length > 0 && (

Available after: {rendered.missing.join(", ")}

)} {problems.map(([field, message]) => (

{message}

))} {ready && (existing ? ( same ? ( captured === existing.name ? (

✓ {existing.name} is in Hosts

) : ( ) ) : (

A different host named {rendered.name} already exists. Choose another name or change it under Hosts.

) ) : ( ))} {error && (

{error}

)}

The first connection asks you to confirm the new server's host key.

); }