feat(notebooks): manage and author notebooks in the web UI (#9)

Add atomic, versioned notebook mutations, a searchable folder tree, and editor insertion forms. Validate notebook paths and symlink roots and retain drafts after external changes.

Add yaml as a direct web dependency, reusing the existing workspace library for comment-preserving frontmatter edits.
This commit is contained in:
Florian Schmidt
2026-11-16 10:16:43 +01:00
parent 4fe7e4e376
commit 911387264b
22 changed files with 1302 additions and 23 deletions
+214
View File
@@ -0,0 +1,214 @@
import "./notebooks.css";
import { useState } from "react";
import type { NotebookSummary } from "@shellbook/core";
import type { Api } from "./api";
import { newNotebook } from "./authoring";
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 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) => (
<button
className={item.path === path ? "selected" : ""}
key={item.path}
onClick={() => onSelect(item.path)}
>
{item.title}
<small>{item.path}</small>
</button>
))}
</>
);
return (
<>
<label>
Search notebooks
<input
type="search"
value={search}
onChange={(e) => setSearch(e.target.value)}
/>
</label>
<button
onClick={() => {
setAction("new");
setError("");
}}
>
New notebook
</button>
<nav aria-label="Notebooks">
{render(
tree(
list.filter((n) =>
`${n.path} ${n.title}`
.toLowerCase()
.includes(search.toLowerCase()),
),
),
)}
</nav>
{path && (
<div className="actions">
{(["rename", "duplicate", "delete"] as const).map((op) => (
<button
key={op}
onClick={() => {
setAction(op);
setError("");
}}
>
{op === "rename"
? "Rename / move"
: op === "duplicate"
? "Duplicate"
: "Delete"}
</button>
))}
</div>
)}
{action && (
<form
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);
}
}}
>
<strong>
{action === "delete"
? `Delete ${path}? This cannot be undone.`
: action === "new"
? "New notebook"
: `${action} ${path}`}
</strong>
{action === "new" ? (
<>
<label>
Name
<input name="name" required />
</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>
)
)}
<button disabled={busy}>
{action === "delete" ? "Confirm delete" : "Apply"}
</button>
<button
type="button"
disabled={busy}
onClick={() => setAction(undefined)}
>
Cancel
</button>
{error && <p role="alert">{error}</p>}
</form>
)}
</>
);
}