- 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.
261 lines
7.5 KiB
TypeScript
261 lines
7.5 KiB
TypeScript
import "./notebooks.css";
|
|
import { useState } from "react";
|
|
import type { NotebookSummary } from "@shellbook/core";
|
|
import type { Api } from "./api";
|
|
import { newNotebook } from "./authoring";
|
|
import { Dialog, Menu } from "./ui";
|
|
type Node = { folders: Map<string, Node>; files: NotebookSummary[] };
|
|
function tree(list: NotebookSummary[]): Node {
|
|
const root: Node = { folders: new Map(), files: [] };
|
|
for (const file of list) {
|
|
let node = root;
|
|
for (const part of file.path.split("/").slice(0, -1)) {
|
|
if (!node.folders.has(part))
|
|
node.folders.set(part, { folders: new Map(), files: [] });
|
|
node = node.folders.get(part)!;
|
|
}
|
|
node.files.push(file);
|
|
}
|
|
return root;
|
|
}
|
|
export function NotebookList({
|
|
api,
|
|
list,
|
|
path,
|
|
version,
|
|
onSelect,
|
|
onChanged,
|
|
canLeave,
|
|
}: {
|
|
api: Api;
|
|
list: NotebookSummary[];
|
|
path: string;
|
|
version?: string;
|
|
onSelect(path: string): void;
|
|
onChanged(path?: string): Promise<void>;
|
|
canLeave(): boolean;
|
|
}) {
|
|
const [search, setSearch] = useState("");
|
|
const [action, setAction] = useState<
|
|
"new" | "rename" | "duplicate" | "delete"
|
|
>();
|
|
const [error, setError] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
const choose = (op: "rename" | "duplicate" | "delete") => {
|
|
setAction(op);
|
|
setError("");
|
|
};
|
|
const render = (node: Node) => (
|
|
<>
|
|
{[...node.folders]
|
|
.sort(([a], [b]) => a.localeCompare(b))
|
|
.map(([name, child]) => (
|
|
<details key={name} open>
|
|
<summary>{name}</summary>
|
|
{render(child)}
|
|
</details>
|
|
))}
|
|
{node.files.map((item) => (
|
|
<div
|
|
className={`notebook-item ${item.path === path ? "selected" : ""}`}
|
|
key={item.path}
|
|
>
|
|
<button
|
|
aria-current={item.path === path ? "page" : undefined}
|
|
title={item.path}
|
|
onClick={() => onSelect(item.path)}
|
|
>
|
|
{item.title}
|
|
</button>
|
|
{item.path === path && (
|
|
<Menu label={`Actions for ${item.title}`}>
|
|
{(close) =>
|
|
(["rename", "duplicate", "delete"] as const).map((op) => (
|
|
<button
|
|
key={op}
|
|
role="menuitem"
|
|
className={op === "delete" ? "danger" : ""}
|
|
onClick={() => {
|
|
close();
|
|
choose(op);
|
|
}}
|
|
>
|
|
{op === "rename"
|
|
? "Rename / move"
|
|
: op === "duplicate"
|
|
? "Duplicate"
|
|
: "Delete"}
|
|
</button>
|
|
))
|
|
}
|
|
</Menu>
|
|
)}
|
|
</div>
|
|
))}
|
|
</>
|
|
);
|
|
const shown = list.filter((n) =>
|
|
`${n.path} ${n.title}`.toLowerCase().includes(search.toLowerCase()),
|
|
);
|
|
return (
|
|
<>
|
|
<div className="sidebar-tools">
|
|
<input
|
|
type="search"
|
|
aria-label="Search notebooks"
|
|
placeholder="Search notebooks"
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
/>
|
|
<button
|
|
className="icon"
|
|
aria-label="New notebook"
|
|
title="New notebook"
|
|
onClick={() => {
|
|
setAction("new");
|
|
setError("");
|
|
}}
|
|
>
|
|
+
|
|
</button>
|
|
</div>
|
|
<nav aria-label="Notebooks">
|
|
{render(tree(shown))}
|
|
{!shown.length && (
|
|
<p className="muted">
|
|
{list.length ? "No notebook matches." : "No notebooks yet."}
|
|
</p>
|
|
)}
|
|
</nav>
|
|
{action && (
|
|
<Dialog
|
|
title={
|
|
action === "new"
|
|
? "New notebook"
|
|
: action === "rename"
|
|
? "Rename or move notebook"
|
|
: action === "duplicate"
|
|
? "Duplicate notebook"
|
|
: "Delete notebook"
|
|
}
|
|
onClose={() => {
|
|
if (!busy) setAction(undefined);
|
|
}}
|
|
>
|
|
<form
|
|
className="stack"
|
|
key={`${action}:${path}`}
|
|
aria-label="Manage notebook"
|
|
onSubmit={async (e) => {
|
|
e.preventDefault();
|
|
if (busy) return;
|
|
const data = new FormData(e.currentTarget);
|
|
if (!canLeave()) return;
|
|
setBusy(true);
|
|
setError("");
|
|
try {
|
|
let next: string | undefined;
|
|
if (action === "new") {
|
|
const folder = String(data.get("folder")).trim();
|
|
let name = String(data.get("name")).trim();
|
|
if (!name.endsWith(".md")) name += ".md";
|
|
const result = await api.create(
|
|
(folder ? folder + "/" : "") + name,
|
|
newNotebook(String(data.get("title"))),
|
|
);
|
|
next = result.path;
|
|
} else {
|
|
if (!version)
|
|
throw new Error("Reload the notebook before changing it");
|
|
if (action === "delete")
|
|
await api.deleteNotebook(path, version);
|
|
else
|
|
next = (
|
|
await api.move(
|
|
path,
|
|
String(data.get("path")).trim(),
|
|
version,
|
|
action === "duplicate",
|
|
)
|
|
).path;
|
|
}
|
|
await onChanged(next);
|
|
setAction(undefined);
|
|
} catch (err) {
|
|
setError(
|
|
`${String(err)}. Refresh the list and retry if the notebook changed.`,
|
|
);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}}
|
|
>
|
|
{action === "delete" ? (
|
|
<p>
|
|
Delete <code>{path}</code>? This cannot be undone.
|
|
</p>
|
|
) : (
|
|
action !== "new" && (
|
|
<p className="muted">
|
|
From <code>{path}</code>
|
|
</p>
|
|
)
|
|
)}
|
|
{action === "new" ? (
|
|
<>
|
|
<label>
|
|
Name
|
|
<input name="name" required placeholder="debian-upgrade" />
|
|
</label>
|
|
<label>
|
|
Folder (optional)
|
|
<input name="folder" placeholder="operations/linux" />
|
|
</label>
|
|
<label>
|
|
Title
|
|
<input name="title" required />
|
|
</label>
|
|
</>
|
|
) : (
|
|
action !== "delete" && (
|
|
<label>
|
|
Destination path
|
|
<input
|
|
name="path"
|
|
defaultValue={
|
|
action === "duplicate"
|
|
? path.replace(/\.md$/, "-copy.md")
|
|
: path
|
|
}
|
|
required
|
|
/>
|
|
</label>
|
|
)
|
|
)}
|
|
{error && (
|
|
<p role="alert" className="error">
|
|
{error}
|
|
</p>
|
|
)}
|
|
<div className="dialog-actions">
|
|
<button
|
|
type="button"
|
|
disabled={busy}
|
|
onClick={() => setAction(undefined)}
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
className={action === "delete" ? "danger" : "primary"}
|
|
disabled={busy}
|
|
>
|
|
{action === "delete" ? "Confirm delete" : "Apply"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</Dialog>
|
|
)}
|
|
</>
|
|
);
|
|
}
|