Files
ShellBook/apps/web/src/TerminalSlot.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

134 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useRef, useState } from "react";
import { Terminal } from "@xterm/xterm";
import { FitAddon } from "@xterm/addon-fit";
import "@xterm/xterm/css/xterm.css";
import { terminalTransport } from "./terminalTransport";
import type { TerminalTransport } from "./terminalTransport";
import type { TerminalClientMessage } from "@shellbook/core";
export function TerminalSlot({
collapsed = false,
connect = terminalTransport,
onClose,
}: {
collapsed?: boolean;
onClose?: () => void;
connect?: (session: string) => TerminalTransport;
}) {
const container = useRef<HTMLDivElement>(null);
const refit = useRef<() => void>(() => {});
const [attempt, setAttempt] = useState(0);
const [state, setState] = useState("Connecting");
useEffect(() => {
let active = true;
let ready = false;
setState("Connecting");
const terminal = new Terminal({
cursorBlink: true,
fontSize: 13,
scrollback: 5000,
});
const fit = new FitAddon();
terminal.loadAddon(fit);
terminal.open(container.current!);
const media = window.matchMedia?.("(prefers-color-scheme: light)");
const theme = () => {
terminal.options.theme = media?.matches
? { background: "#ffffff", foreground: "#233244", cursor: "#197b59" }
: { background: "#10151c", foreground: "#dce3ec", cursor: "#86dab9" };
};
theme();
media?.addEventListener("change", theme);
let socket: TerminalTransport;
let timer: ReturnType<typeof setTimeout>;
const send = (message: TerminalClientMessage) => {
if (ready) socket.send(JSON.stringify(message));
};
const resize = () => {
clearTimeout(timer);
timer = setTimeout(() => {
if (!active || !container.current?.getBoundingClientRect().width)
return;
fit.fit();
send({
type: "resize",
cols: Math.min(1000, Math.max(1, terminal.cols)),
rows: Math.min(1000, Math.max(1, terminal.rows)),
});
}, 100);
};
refit.current = resize;
const observer =
typeof ResizeObserver === "undefined"
? undefined
: new ResizeObserver(resize);
observer?.observe(container.current!);
window.addEventListener("resize", resize);
const input = terminal.onData((data) => send({ type: "input", data }));
try {
socket = connect("main");
socket.onopen = () => {
if (active) {
ready = true;
setState("Connected");
resize();
}
};
socket.onmessage = (event) => {
if (active) terminal.write(event.data);
};
const disconnected = () => {
if (active) {
ready = false;
setState("Disconnected");
}
};
socket.onclose = disconnected;
socket.onerror = () => {
disconnected();
socket.close();
};
} catch {
setState("Disconnected");
}
return () => {
active = false;
ready = false;
clearTimeout(timer);
observer?.disconnect();
window.removeEventListener("resize", resize);
media?.removeEventListener("change", theme);
input.dispose();
socket?.close();
terminal.dispose();
};
}, [attempt, connect]);
useEffect(() => {
if (!collapsed) refit.current();
}, [collapsed]);
return (
<aside className="terminal side-panel" aria-label="Terminal panel" hidden={collapsed}>
<header className="panel-header">
<h2>Terminal</h2>
<span role="status" className={`dot-status ${state === "Connected" ? "ok" : state === "Disconnected" ? "bad" : ""}`}>
{state}
</span>
{state === "Disconnected" && (
<button onClick={() => setAttempt((n) => n + 1)}>Reconnect</button>
)}
<span className="spacer" />
{onClose && (
<button className="icon" aria-label="Close terminal" onClick={onClose}>
×
</button>
)}
</header>
<p className="muted">
tmux session “main” on the ShellBook server. Run <code>claude</code>{" "}
here; notebook edits show up live.
</p>
<div className="terminal-screen" ref={container} />
</aside>
);
}