Public: anyone can read this; the crew can change it.

The New forge page shows a template picker (default: today's behaviour) that works at phone width #16730

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/7534-the-new-forge-page-shows-a-template-pick → main 0 commits fork 1 thread · 1 unresolved

  • Merged. scout/7534-the-new-forge-page-shows-a-template-pick was merged into main as 354c752.

Review

Sign in to review this change.
4 files changed +94 −34
docs/features/new-forge-template.md +1 −1
@@ -38,7 +38,7 @@ The terminal has the same list: the "Templates" command (`src/tui/screens/templa
3838
3939 ## Choosing on the page: radios
4040
41 The New forge page lists the built-in templates as radio buttons, Blank checked, and needs no script; `/forges/new?template=starter` opens the form with Starter app checked. Pick one and press Make. In the terminal the New forge screen cycles the same choices with left and right, so both doors offer the same list. Code: `src/web/pages/new-forge.ts`. Test: `test/new-forge-template.test.ts`.
41 The New forge page lists the built-in templates as radio buttons, Blank checked, and needs no script; `/forges/new?template=starter` opens the form with Starter app checked. Pick one and press Make. In the terminal the New forge screen cycles the same choices with left and right, so both doors offer the same list. Code: `src/web/pages/new-forge-template.ts` (the picker, called from `src/web/pages/new-forge.ts`); the terminal side is `src/tui/screens/newforge.ts`. Test: `test/new-forge-template.test.ts`.
4242
4343 ### Order of the list
4444
src/web/pages/new-forge-template.ts added +54
@@ -0,0 +1,54 @@
1 /**
2 * The Template picker of /forges/new: a radio list (a few choices want to be seen), one row
3 * each with its title and sentence, Blank first and checked unless another was asked for.
4 * A refusal on `template` shows beside the field, linked by `aria-describedby`.
5 */
6
7 import { DEFAULT_TEMPLATE, TEMPLATES, templateOf } from "../../spawn/templates.ts";
8 import { headWords } from "../../spawn/template_head.ts";
9 import { html, type Html } from "../html.ts";
10 import { timeAgo } from "../time_element.ts";
11
12 const lower = (t: string): string => t.charAt(0).toLowerCase() + t.slice(1);
13
14 /** ` · updated 3d ago` (and ` · stale` past 180 days) for a template whose head is known. */
15 const headLabel = (at: string | undefined): string => (at === undefined ? "" : ` · ${headWords(at, timeAgo).join("")}`);
16
17 /**
18 * The templates in order, Blank first; the one asked for chosen (Blank when it is not one of them).
19 */
20 export function templateOptions(
21 chosen: string,
22 repos: readonly string[],
23 heads: ReadonlyMap<string, string> = new Map(),
24 ): { value: string; label: string; selected?: boolean }[] {
25 const known = TEMPLATES.some((t) => t.id === chosen) || repos.includes(chosen);
26 const at = known ? chosen : DEFAULT_TEMPLATE;
27 return [
28 ...TEMPLATES.map((t) => ({ value: t.id, label: `${t.title}: ${lower(t.says)}`, selected: t.id === at })),
29 ...repos.map((ref) => ({
30 value: ref,
31 label: `${ref}: ${lower(templateOf(ref).says)}${headLabel(heads.get(ref))}`,
32 selected: ref === at,
33 })),
34 ];
35 }
36
37 export function templatePicker(
38 chosen: string,
39 repos: readonly string[],
40 heads: ReadonlyMap<string, string>,
41 error: string | undefined,
42 ): Html {
43 return html`<fieldset class="field" id="f-template" aria-describedby="f-template-hint${error ? " f-template-err" : ""}" ${error ? html`data-invalid` : null}>
44 <legend>Template</legend>
45 ${templateOptions(chosen, repos, heads).map(
46 (o) => html`<label class="check">
47 <input type="radio" name="template" value="${o.value}" ${o.selected ? html`checked` : null} />
48 <span>${o.label}</span>
49 </label>`,
50 )}
51 <span class="muted small" id="f-template-hint">What the new forge starts from besides the apps you fork. <a href="/templates">Templates</a></span>
52 ${error ? html`<span class="error" id="f-template-err">${error}</span>` : null}
53 </fieldset>`;
54 }
src/web/pages/new-forge.ts +4 −33
@@ -33,7 +33,7 @@ import {
3333 MIN_BUDGET_USD,
3434 } from "../../spawn/plan.ts";
3535 import { DEFAULT_SIZE, SIZES, sizesView } from "../../spawn/sizes.ts";
36 import { DEFAULT_TEMPLATE, parseTemplate, TEMPLATES, templateOf } from "../../spawn/templates.ts";
36 import { DEFAULT_TEMPLATE, parseTemplate, templateOf } from "../../spawn/templates.ts";
3737 import { SENDS_ONE_LINK } from "../../spawn/send_words.ts";
3838 import { FOLLOW_LABELS, FOLLOW_WORDS } from "../../spawn/follow_words.ts";
3939 import { dropGift, keepGift, keptGift } from "../gift_cookie.ts";
@@ -41,9 +41,9 @@ import { notFoundPage, pageHead } from "../chrome.ts";
4141 import { field, readForm, type Form } from "../form.ts";
4242 import { templateListUnsorted } from "../../spawn/template_list.ts";
4343 import { templateRows } from "./templates.ts";
44 import { headWords, templateHeads } from "../../spawn/template_head.ts";
44 import { templateHeads } from "../../spawn/template_head.ts";
4545 import { html, type Html } from "../html.ts";
46 import { timeAgo } from "../time_element.ts";
46 import { templatePicker } from "./new-forge-template.ts";
4747 import { noteForgeMade } from "../funnel_door.ts";
4848 import { page, redirect } from "../layout.ts";
4949 import { refreshForPage } from "../../actions/places_refresh.ts";
@@ -184,25 +184,6 @@ function sizeOptions(chosen: string): { value: string; label: string; selected?:
184184 }));
185185 }
186186
187 /** ` · updated 3d ago` (and ` · stale` past 180 days) for a template whose head is known. */
188 const headLabel = (at: string | undefined): string => (at === undefined ? "" : ` · ${headWords(at, timeAgo).join("")}`);
189
190 /**
191 * The templates in order, Blank first; the one asked for chosen (Blank when it is not one of them).
192 */
193 function templateOptions(chosen: string, repos: readonly string[], heads: ReadonlyMap<string, string> = new Map()): { value: string; label: string; selected?: boolean }[] {
194 const known = TEMPLATES.some((t) => t.id === chosen) || repos.includes(chosen);
195 const at = known ? chosen : DEFAULT_TEMPLATE;
196 return [
197 ...TEMPLATES.map((t) => ({
198 value: t.id,
199 label: `${t.title}: ${lower(t.says)}`,
200 selected: t.id === at,
201 })),
202 ...repos.map((ref) => ({ value: ref, label: `${ref}: ${lower(templateOf(ref).says)}${headLabel(heads.get(ref))}`, selected: ref === at })),
203 ];
204 }
205
206187 /** The page's words for what the data names in the API's terms (#10787); the data keeps its own. */
207188 const PLAIN: Readonly<Record<string, string>> = {
208189 tokens: "access passes",
@@ -337,17 +318,7 @@ function formView(
337318 ...(freshErr ? { error: freshErr } : {}),
338319 })}
339320 ${modeRows(values, errors["modes"])}
340 <fieldset class="field" id="f-template" aria-describedby="f-template-hint${errors["template"] ? " f-template-err" : ""}" ${errors["template"] ? html`data-invalid` : null}>
341 <legend>Template</legend>
342 ${templateOptions(values.template, templates, heads).map(
343 (o) => html`<label class="check">
344 <input type="radio" name="template" value="${o.value}" ${o.selected ? html`checked` : null} />
345 <span>${o.label}</span>
346 </label>`,
347 )}
348 <span class="muted small" id="f-template-hint">What the new forge starts from besides the apps you fork. <a href="/templates">Templates</a></span>
349 ${errors["template"] ? html`<span class="error" id="f-template-err">${errors["template"]}</span>` : null}
350 </fieldset>
321 ${templatePicker(values.template, templates, heads, errors["template"])}
351322 <details class="disclosure" ${Object.keys(errors).some((k) => MORE_FIELDS.includes(k)) ? html`open` : null}>
352323 <summary>More settings</summary>
353324 <fieldset class="field" ${errors["followMode"] ? html`data-invalid` : null}>
test/new-forge-template.test.ts +35
@@ -13,6 +13,8 @@ import { createUser } from "../src/core/users.ts";
1313 import { serve, type Servers } from "../src/http/server.ts";
1414 import * as door from "../src/spawn/door.ts";
1515 import { TEMPLATES } from "../src/spawn/templates.ts";
16 import { templatePicker } from "../src/web/pages/new-forge-template.ts";
17 import { css } from "../src/web/style.ts";
1618 import { quietLogs } from "./helpers/globals.ts";
1719
1820 quietLogs();
@@ -75,4 +77,37 @@ describe("New forge template radios", () => {
7577 spy.mockRestore();
7678 }
7779 });
80
81 test("the picker lists the offered templates with blank checked", async () => {
82 const found = radios(await page());
83 expect(found.map((m) => m[1])).toEqual(TEMPLATES.map((t) => t.id));
84 expect(found.filter((m) => m[2]).map((m) => m[1])).toEqual(["blank"]);
85 });
86
87 test("the blank choice sends no template", async () => {
88 const spy = spyOn(door, "spawnDoor");
89 try {
90 const form = new URLSearchParams({ name: "tplblank", do: "plan", template: "blank" });
91 await fetch(`${servers.url}/forges/new`, { method: "POST", headers: { cookie, origin: "https://forge.test" }, body: form });
92 expect("template" in spy.mock.calls.at(-1)![2]).toBe(false);
93 } finally {
94 spy.mockRestore();
95 }
96 });
97
98 test("a refusal on template is linked to its field", () => {
99 const out = String(templatePicker("blank", [], new Map(), "template must be blank"));
100 expect(out).toContain('aria-describedby="f-template-hint f-template-err"');
101 expect(out).toContain('id="f-template-err"');
102 expect(out).toContain("template must be blank");
103 });
104
105 test("the page has no horizontal overflow at 390 px", async () => {
106 const sheet = typeof css === "string" ? css : [...css].join("\n");

Next: this checks CSS text, not layout, so it would pass with horizontal scroll present. Measure scrollWidth at 390 px when a browser can run, and tie the 44 px check to the rendered row.

107 // Rows are at least 44px tall on a phone, and wrap their sentence instead of widening the page.
108 expect(sheet).toMatch(/label\.check[^{}]*\{[^}]*min-height: 44px/);
109 expect(sheet).toContain("max-width: 100%");
110 const html = await page();
111 expect(html).not.toMatch(/<(?:input|label|fieldset)[^>]*style="[^"]*(?:width|min-width):\s*\d{3,}px/);
112 });
78113 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs