diff --git a/apps/web/e2e/docs.spec.ts b/apps/web/e2e/docs.spec.ts index 7210e79..a8a7ec2 100644 --- a/apps/web/e2e/docs.spec.ts +++ b/apps/web/e2e/docs.spec.ts @@ -26,7 +26,8 @@ async function signIn(page: Page, email: string) { async function seedDocs(workspaceId: string) { const { db } = openSqlite("file:.data/e2e.sqlite"); const now = new Date(); - const root = "R_docs:main:docs"; + // The whole repo (decision 038): root "" and the repo's own folders. + const root = "R_docs:main:"; await db.batch([ db .insert(ghRepos) @@ -34,10 +35,10 @@ async function seedDocs(workspaceId: string) { .onConflictDoNothing(), db.insert(ghProjects).values({ nodeId: "PVT_docs", installationId: 99, ownerLogin: "acme", number: 7, title: "Handbook", url: "https://github.com/orgs/acme/projects/7", syncedAt: now }).onConflictDoNothing(), db.insert(workspaceGithub).values({ workspaceId, installationId: 99, projectNodeId: "PVT_docs", linkedAt: now }), - db.insert(workspaceRepos).values({ workspaceId, repoNodeId: "R_docs" }), - db.insert(ghDocRoots).values({ id: root, repoNodeId: "R_docs", branch: "main", path: "docs", headOid: "c0ffee", treeOid: "7ree", syncedAt: now }), + db.insert(workspaceRepos).values({ workspaceId, repoNodeId: "R_docs", docsPath: "" }), + db.insert(ghDocRoots).values({ id: root, repoNodeId: "R_docs", branch: "main", path: "", headOid: "c0ffee", treeOid: "7ree", syncedAt: now }), db.insert(ghDocFiles).values([ - { rootId: root, path: "docs/README.md", blobSha: "a".repeat(40), size: README.length }, + { rootId: root, path: "README.md", blobSha: "a".repeat(40), size: README.length }, { rootId: root, path: "docs/internal/plan.md", blobSha: "b".repeat(40), size: SECRET.length }, { rootId: root, path: "docs/guide/setup.md", blobSha: "c".repeat(40), size: 9 }, ]), @@ -68,7 +69,7 @@ test("docs: tree, hide folder, rich-text editor, frontmatter panel", async ({ br // The hide folder, set through the settings form. await owner.goto(`/w/${workspaceId}/settings`); - await owner.getByLabel("Hide docs in this folder").fill("internal"); + await owner.getByLabel("Hide docs in this folder").fill("docs/internal"); const saved = owner.waitForResponse((r) => r.url().endsWith(`/workspaces/${workspaceId}/settings`) && r.status() === 204); await owner.getByRole("button", { name: "Save", exact: true }).click(); await saved; @@ -88,9 +89,13 @@ test("docs: tree, hide folder, rich-text editor, frontmatter panel", async ({ br // Owner: the whole tree, from the sidebar's Docs link. await owner.goto(`/w/${workspaceId}`); await owner.getByRole("link", { name: "Docs", exact: true }).click(); + // The repo is the top folder, with the repo's own folders inside. const tree = owner.getByRole("complementary", { name: "Docs" }); + await expect(tree.getByRole("button", { name: "handbook" })).toBeVisible(); await expect(tree.getByRole("link", { name: "README" })).toBeVisible(); + await expect(tree.getByRole("button", { name: "docs" })).toBeVisible(); await expect(tree.getByRole("button", { name: "internal" })).toBeVisible(); + await expect(tree.getByRole("button", { name: "guide" })).toBeVisible(); // Read Only: no hidden folder in the tree, the API, or by URL. await reader.goto(`/w/${workspaceId}/docs`); @@ -99,11 +104,11 @@ test("docs: tree, hide folder, rich-text editor, frontmatter panel", async ({ br const list = await (await reader.request.get(`/api/v1/workspaces/${workspaceId}/docs`)).text(); expect(list).not.toContain("internal"); expect((await reader.request.get(`/api/v1/workspaces/${workspaceId}/docs/file?repo=R_docs&path=docs/internal/plan.md`)).status()).toBe(404); - const r = await reader.request.get(`/api/v1/workspaces/${workspaceId}/docs/file?repo=R_docs&path=docs/README.md`); + const r = await reader.request.get(`/api/v1/workspaces/${workspaceId}/docs/file?repo=R_docs&path=README.md`); expect(await r.json()).toMatchObject({ doc: { text: README, editable: false } }); // Read Only sees the doc rendered, with no toolbar or save. - await reader.getByRole("link", { name: "README" }).click(); + await reader.getByRole("complementary", { name: "Docs" }).getByRole("link", { name: "README" }).click(); await expect(reader.locator(".cm-lp-h1")).toHaveText("Handbook"); await expect(reader.getByRole("toolbar", { name: "Formatting" })).toHaveCount(0); await expect(reader.getByRole("button", { name: "Save" })).toHaveCount(0); diff --git a/apps/web/src/app/w/[workspaceId]/docs/doc-editor.tsx b/apps/web/src/app/w/[workspaceId]/docs/doc-editor.tsx index 20d9e12..82c5d96 100644 --- a/apps/web/src/app/w/[workspaceId]/docs/doc-editor.tsx +++ b/apps/web/src/app/w/[workspaceId]/docs/doc-editor.tsx @@ -5,7 +5,7 @@ import { syntaxHighlighting } from "@codemirror/language"; import { searchKeymap } from "@codemirror/search"; import { Compartment, EditorState } from "@codemirror/state"; import { EditorView, drawSelection, dropCursor, keymap, placeholder } from "@codemirror/view"; -import { MAX_EDITABLE_BYTES, type TextChange, lineSeparatorOf, relativeLink, resolveRelative, safeFileName } from "@gitwork/core"; +import { MAX_EDITABLE_BYTES, type TextChange, folderOf, joinRoot, lineSeparatorOf, relativeLink, resolveRelative, safeFileName } from "@gitwork/core"; import type { DocView, Editor } from "@gitwork/server"; import { useRouter } from "next/navigation"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; @@ -107,7 +107,8 @@ export function DocEditor(props: { workspaceId: string; repoNodeId: string; path const base = safeFileName(file.name || "image.png"); const dot = base.lastIndexOf("."); const name = `${base.slice(0, dot > 0 ? dot : undefined)}-${Date.now().toString(36)}${dot > 0 ? base.slice(dot) : ".png"}`; - const abs = `${doc.root}/assets/${name}`; + // Next to the doc: /assets/ (decision 038). + const abs = joinRoot(folderOf(path), `assets/${name}`); const link = relativeLink(path, abs); images.current.set(abs, { data: await toBase64(file), url: URL.createObjectURL(file), link }); const alt = (file.name || "image").replace(/\.[^.]+$/, "").replace(/[[\]]/g, ""); @@ -285,7 +286,7 @@ export function DocEditor(props: { workspaceId: string; repoNodeId: string; path async function rename() { if (!doc) return; const clean = newName.trim().replace(/^\/+/, ""); - const target = `${doc.root}/${/\.(md|markdown)$/i.test(clean) ? clean : `${clean}.md`}`; + const target = joinRoot(doc.root, /\.(md|markdown)$/i.test(clean) ? clean : `${clean}.md`); try { const out = await docs.rename({ repoNodeId, path, baseSha: baseSha.current, newPath: target }); setRenaming(false); diff --git a/apps/web/src/app/w/[workspaceId]/docs/docs-tree.tsx b/apps/web/src/app/w/[workspaceId]/docs/docs-tree.tsx index 21f5d87..b37e388 100644 --- a/apps/web/src/app/w/[workspaceId]/docs/docs-tree.tsx +++ b/apps/web/src/app/w/[workspaceId]/docs/docs-tree.tsx @@ -1,8 +1,10 @@ "use client"; import type { DocFileView, DocRepoView } from "@gitwork/server"; +import type { ReactNode } from "react"; import Link from "next/link"; import { useParams, useRouter } from "next/navigation"; -import { useMemo, useState } from "react"; +import { joinRoot, relativeToRoot } from "@gitwork/core"; +import { useEffect, useMemo, useRef, useState } from "react"; import { Button, ErrorText, Input } from "@/components/kit"; import { api } from "@/lib/api"; import { decodeSegment, docHref, rawHref } from "./links"; @@ -15,14 +17,15 @@ interface Folder { } function buildTree(repo: DocRepoView): Folder { - const root: Folder = { name: repo.root, path: repo.root, folders: new Map(), files: [] }; + // The repo is the top folder; below it, the repo's own folders (decision 038). + const root: Folder = { name: repo.fullName.split("/")[1] ?? repo.fullName, path: repo.root, folders: new Map(), files: [] }; for (const file of repo.files) { - const parts = file.path.slice(repo.root.length + 1).split("/"); + const parts = relativeToRoot(file.path, repo.root).split("/"); let folder = root; for (const part of parts.slice(0, -1)) { let next = folder.folders.get(part); if (!next) { - next = { name: part, path: `${folder.path}/${part}`, folders: new Map(), files: [] }; + next = { name: part, path: joinRoot(folder.path, part), folders: new Map(), files: [] }; folder.folders.set(part, next); } folder = next; @@ -34,8 +37,8 @@ function buildTree(repo: DocRepoView): Folder { const STATUS_TEXT: Record = { ok: "", - unsynced: "Not loaded from GitHub yet.", - missing: "This folder does not exist on the branch yet. Create a doc to start it.", + unsynced: "Loading from GitHub…", + missing: "Nothing on this branch yet. Create a doc to start it.", unavailable: "The GitHub App cannot reach this repo right now.", }; @@ -48,23 +51,21 @@ export function DocsTree(props: { workspaceId: string; linked: boolean; repos: D const [creating, setCreating] = useState(null); const [name, setName] = useState(""); - async function refresh() { - setBusy(true); - setError(""); - try { - await api("POST", `/workspaces/${props.workspaceId}/docs/refresh`); - router.refresh(); - } catch (e) { - setError((e as Error).message); - } finally { - setBusy(false); - } - } + // Repos never read from GitHub load on their own: no button (decision 038). Once per page. + const loading = useRef(false); + const unsynced = props.repos.some((r) => r.status === "unsynced"); + useEffect(() => { + if (!unsynced || loading.current) return; + loading.current = true; + api("POST", `/workspaces/${props.workspaceId}/docs/refresh`) + .then(() => router.refresh()) + .catch((e: Error) => setError(e.message)); + }, [unsynced, props.workspaceId, router]); async function create(repo: DocRepoView) { const clean = name.trim().replace(/^\/+/, ""); if (!clean) return; - const path = `${repo.root}/${/\.(md|markdown)$/i.test(clean) ? clean : `${clean}.md`}`; + const path = joinRoot(repo.root, /\.(md|markdown)$/i.test(clean) ? clean : `${clean}.md`); setBusy(true); setError(""); try { @@ -85,10 +86,7 @@ export function DocsTree(props: { workspaceId: string; linked: boolean; repos: D