Public: anyone can read this; the crew can change it.
A template on /templates links to a read-only live preview of what a new forge from it looks like #14698
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14675-a-template-on-templates-links-to-a-read → main 4 commits fork 5 threads · 5 unresolved
4 files
changed +76
−1
docs/features/template-holds.md +4
| @@ -19,3 +19,7 @@ template and its lines (apps, starter budget, model default) now end with `Files | ||
| 19 | 19 | `templateHolds` the page uses. |
| 20 | 20 | |
| 21 | 21 | Pinned by `test/templates-page.test.ts` and `test/tui-app-templates.test.ts`. |
| 22 | ||
| 23 | Each row also carries one link, **Preview**, to the template's read-only page (`/templates/<id>`, an ordinary page with | |
| 24 | the same security headers), or says "no preview yet" when there is nothing to show: Blank, or a template repository with | |
| 25 | no commits. In the terminal the lines under the cursor read `Share:` and `Preview:` with that same path. | |
src/web/pages/templates.ts +2 −1
| @@ -15,6 +15,7 @@ import { templateUse, useWords, type TemplateUse } from "../../spawn/template_us | ||
| 15 | 15 | import { pageHead } from "../chrome.ts"; |
| 16 | 16 | import { html, type Html } from "../html.ts"; |
| 17 | 17 | import { page } from "../layout.ts"; |
| 18 | import { previewLink } from "../template_card.ts"; | |
| 18 | 19 | import { btn } from "../ui.ts"; |
| 19 | 20 | |
| 20 | 21 | export const TEMPLATES_WORDS = "Pick where your forge starts. You can change everything after."; |
| @@ -38,7 +39,7 @@ export function templateRows(rows: readonly TemplateRow[], held?: ReadonlyMap<st | ||
| 38 | 39 | (r) => html`<li data-template="${r.id}"> |
| 39 | 40 | <form method="get" action="/forges/new" class="row"> |
| 40 | 41 | <input type="hidden" name="template" value="${r.id}"> |
| 41 | <span><b>${r.title}</b> ${r.says}</span> | |
| 42 | <span><b>${r.title}</b> ${r.says}${held === undefined ? null : html` ${previewLink(r.id, r.title, held.get(r.id))}`}</span> | |
| 42 | 43 | ${btn(`Start a forge from ${r.title}`, { variant: "secondary" })} |
| 43 | 44 | </form> |
| 44 | 45 | ${used === undefined ? null : html`<p class="small muted">${useWords(used.get(r.id))}</p>`} |
outdated
A separate <p> per row adds a stacked line; fold it into the row or the holds dl (principles 3 and 7). |
src/web/template_card.ts +15
| @@ -10,6 +10,9 @@ import { readFileAt } from "../apps/checkout.ts"; | ||
| 10 | 10 | import type { Repo } from "../core/repos.ts"; |
| 11 | 11 | import { resolveOid } from "../git/merge.ts"; |
| 12 | 12 | import { repoDir } from "../git/repos.ts"; |
| 13 | import { DEFAULT_TEMPLATE } from "../spawn/templates.ts"; | |
| 14 | import { templatePath } from "../spawn/template_list.ts"; | |
| 15 | import type { TemplateHolds } from "../spawn/template_holds.ts"; | |
| 13 | 16 | import { templateFacts, templateRows, type TemplateFacts } from "../spawn/template_facts.ts"; |
| 14 | 17 | import { html, type Html } from "./html.ts"; |
| 15 | 18 | |
| @@ -32,3 +35,15 @@ export function templateCardHtml(facts: TemplateFacts): Html { | ||
| 32 | 35 | export async function templateCard(app: App, repo: Repo): Promise<Html | null> { |
| 33 | 36 | return repo.template ? templateCardHtml(await templateFactsOf(app, repo)) : null; |
| 34 | 37 | } |
| 38 | ||
| 39 | export const PREVIEW_NONE = "no preview yet"; | |
| 40 | ||
| 41 | /** | |
| 42 | * The one preview link on a template's row (#14675): the template's own read-only page | |
| 43 | * (`/templates/...`, an ordinary page with the same security headers) when there is something to | |
| 44 | * show (it holds files), else the words "no preview yet". Blank holds nothing, so it never has one. | |
| 45 | */ | |
| 46 | export function previewLink(id: string, title: string, holds: TemplateHolds | undefined): Html { | |
| 47 | if (id === DEFAULT_TEMPLATE || holds === undefined) return html`<span class="small muted" data-preview="none">${PREVIEW_NONE}</span>`; | |
| 48 | return html`<a class="small" data-preview="link" href="${templatePath(id)}" aria-label="Preview ${title}">Preview</a>`; | |
outdated
Link text 'Preview' is identical on every row; give it a per-template name (aria-label or 'Preview ${title}'). Principle 8 and keyboard/screen-reader reach. |
||
| 49 | } |
test/template-preview.test.ts added +55
| @@ -0,0 +1,55 @@ | ||
| 1 | /** A template's row links to its read-only preview, or says "no preview yet" (#14675). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 8 | import { previewLink } from "../src/web/template_card.ts"; | |
| 9 | ||
| 10 | let dir: string; | |
| 11 | let app: App; | |
| 12 | let servers: Servers; | |
| 13 | ||
| 14 | beforeAll(async () => { | |
| 15 | dir = await mkdtemp(join(tmpdir(), "forge-template-preview-")); | |
| 16 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0, forgeRepo: "alice/forge-code" }); | |
| 17 | servers = await serve(app); | |
| 18 | }, 30_000); | |
| 19 | ||
| 20 | afterAll(async () => { | |
| 21 | servers.stop(); | |
| 22 | app.close(); | |
| 23 | await rm(dir, { recursive: true, force: true }); | |
| 24 | }); | |
| 25 | ||
| 26 | const row = (text: string, id: string): string => text.split(`data-template="${id}"`)[1]!.split("</li>")[0]!; | |
| 27 | ||
| 28 | test("starter links to its preview, blank says no preview yet", async () => { | |
| 29 | const res = await fetch(`${servers.url}/templates`); | |
| 30 | const text = await res.text(); | |
| 31 | expect(row(text, "starter")).toContain('href="/templates/starter"'); | |
| 32 | expect(row(text, "starter")).toContain('aria-label="Preview Starter app"'); | |
| 33 | expect(row(text, "starter")).not.toContain("no preview yet"); | |
| 34 | expect((text.match(/<form[^>]*method="post"[^>]*>/gi) ?? []).filter((f) => !f.includes('action="/theme"'))).toEqual([]); | |
| 35 | expect(row(text, "blank")).toContain("no preview yet"); | |
| 36 | expect(row(text, "blank")).not.toContain("/templates/blank"); | |
| 37 | }); | |
| 38 | ||
| 39 | test("the preview is read-only and carries the security headers", async () => { | |
| 40 | const res = await fetch(`${servers.url}/templates/starter`); | |
| 41 | expect(res.status).toBe(200); | |
| 42 | expect(res.headers.get("content-security-policy")).toBeTruthy(); | |
| 43 | expect(res.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 44 | expect(res.headers.get("referrer-policy")).toBeTruthy(); | |
outdated
not.toBe(200) passes on a 500; assert the exact status, and assert the preview page has no mutating form. |
||
| 45 | const post = await fetch(`${servers.url}/templates/starter`, { method: "POST" }); | |
| 46 | expect([404, 405]).toContain(post.status); | |
| 47 | const text = await (await fetch(`${servers.url}/templates/starter`)).text(); | |
| 48 | expect((text.match(/<form[^>]*method="post"[^>]*>/gi) ?? []).filter((f) => !f.includes('action="/theme"'))).toEqual([]); | |
| 49 | }); | |
| 50 | ||
| 51 | test("a repository template with nothing to read has no preview", () => { | |
| 52 | expect(String(previewLink("alice/empty", "Empty", undefined))).toContain("no preview yet"); | |
| 53 | expect(String(previewLink("alice/full", "Full", { files: ["a"], more: 0, run: undefined, check: undefined }))).toContain('href="/templates/alice/full"'); | |
| 54 | expect(String(previewLink("alice/full", "Full", { files: ["a"], more: 0, run: undefined, check: undefined }))).toContain('aria-label="Preview Full"'); | |
| 55 | }); |
new above
offline
Non-blocking: this says the terminal shows a
Preview:line, but you removed it and onlyShare:remains. Correct the sentence.