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

A feature's menu entry carries its declared summary as its title #9686

Parked opened this
Closed without merging

mason/forge:mason/9681-a-feature-s-menu-entry-carries-its-decla → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +72 −15
src/actions/features.ts +19 −4
@@ -204,10 +204,24 @@ export interface NavFeature {
204204 readonly name: string;
205205 /** The app path to open: the feature's page. */
206206 readonly page: string;
207 /** What the app declared about itself when absorbed, for the entry's title. */
208 readonly summary: string | null;
207209 /** The app record already read, so the menu does not fetch it again. */
208210 readonly record: AppRecord;
209211 }
210212
213 const TITLE_MAX = 200;
214
215 /** A summary as a menu title: at most 200 characters, cut at a word with an ellipsis; none when blank. */
216 export function menuTitle(summary: string | null): string | undefined {
217 const text = (summary ?? "").replace(/\s+/g, " ").trim();
218 if (text === "") return undefined;
219 if (text.length <= TITLE_MAX) return text;
220 const cut = text.slice(0, TITLE_MAX - 1);
221 const at = cut.lastIndexOf(" ");
222 return `${(at > 0 ? cut.slice(0, at) : cut).replace(/[\s.,;:!?-]+$/, "")}…`;
223 }
224
211225 /** Absorbed apps the actor may read, for the forge's nav. Cheap enough for every page: no git reads. */
212226 export function absorbedForNav(app: App, actor: User | undefined): NavFeature[] {
213227 const out: NavFeature[] = [];
@@ -216,7 +230,7 @@ export function absorbedForNav(app: App, actor: User | undefined): NavFeature[]
216230 if (record === undefined) continue;
217231 const repo = getRepoById(app.db, record.repoId);
218232 if (repo === undefined || !canRead(app.db, repo, actor)) continue;
219 out.push({ appId: record.id, slug: record.slug, repo: `${repo.ownerName}/${repo.name}`, name: row.name, page: row.page, record });
233 out.push({ appId: record.id, slug: record.slug, repo: `${repo.ownerName}/${repo.name}`, name: row.name, page: row.page, summary: row.summary, record });
220234 }
221235 return out;
222236 }
@@ -242,7 +256,7 @@ export async function absorbApp(app: App, actor: User | undefined, slug: string)
242256 if (!siteAdmin(actor)) return err("forbidden");
243257 const declared = await declaredFeature(app, found.repo);
244258 if (!declared.ok || declared.value === undefined) return err("not_a_feature");
245 if (absorb(app.db, found.record.id, actor.id, declared.value.name, declared.value.page)) {
259 if (absorb(app.db, found.record.id, actor.id, declared.value.name, declared.value.page, declared.value.summary ?? null)) {
246260 app.bus.append({ kind: "app.absorbed", repoId: found.repo.id, actorId: actor.id, payload: { slug, name: declared.value.name } });
247261 await redeployAll(app, found.record, found.repo);
248262 }
@@ -268,9 +282,10 @@ export async function acceptDeclaration(
268282 const declared = await declaredFeature(app, found.repo);
269283 if (!declared.ok || declared.value === undefined) return err("not_a_feature");
270284 const { name, page } = declared.value;
285 const summary = declared.value.summary ?? null;
271286 if (name !== reviewed.name || page !== reviewed.page) return err("declaration_changed");
272 if (name !== before.name || page !== before.page) {
273 keepDeclaration(app.db, found.record.id, name, page);
287 if (name !== before.name || page !== before.page || summary !== before.summary) {
288 keepDeclaration(app.db, found.record.id, name, page, summary);
274289 app.bus.append({
275290 kind: "app.feature_declared",
276291 repoId: found.repo.id,
src/core/features.ts +8 −7
@@ -12,10 +12,11 @@ export interface Absorbed {
1212 readonly absorbedAt: string;
1313 readonly name: string;
1414 readonly page: string;
15 readonly summary: string | null;
1516 }
1617
17 type Row = { app_id: string; absorbed_by: string; absorbed_at: string; name: string; page: string };
18 const fromRow = (r: Row): Absorbed => ({ appId: r.app_id, absorbedBy: r.absorbed_by, absorbedAt: r.absorbed_at, name: r.name, page: r.page });
18 type Row = { app_id: string; absorbed_by: string; absorbed_at: string; name: string; page: string; summary: string | null };
19 const fromRow = (r: Row): Absorbed => ({ appId: r.app_id, absorbedBy: r.absorbed_by, absorbedAt: r.absorbed_at, name: r.name, page: r.page, summary: r.summary });
1920
2021 export function getAbsorbed(db: Database, appId: string): Absorbed | undefined {
2122 const row = db.query<Row, [string]>("SELECT * FROM app_feature WHERE app_id = ?1").get(appId);
@@ -28,17 +29,17 @@ export function listAbsorbed(db: Database): Absorbed[] {
2829 }
2930
3031 /** True when the app was not absorbed before. */
31 export function absorb(db: Database, appId: string, by: string, name: string, page: string): boolean {
32 export function absorb(db: Database, appId: string, by: string, name: string, page: string, summary: string | null = null): boolean {
3233 return (
3334 db
34 .query("INSERT OR IGNORE INTO app_feature (app_id, absorbed_by, name, page) VALUES (?1, ?2, ?3, ?4)")
35 .run(appId, by, name, page).changes > 0
35 .query("INSERT OR IGNORE INTO app_feature (app_id, absorbed_by, name, page, summary) VALUES (?1, ?2, ?3, ?4, ?5)")
36 .run(appId, by, name, page, summary).changes > 0
3637 );
3738 }
3839
3940 /** Keep this name and page for the nav: an admin's act (accepting a changed declaration), never a deploy's. */
40 export function keepDeclaration(db: Database, appId: string, name: string, page: string): void {
41 db.query("UPDATE app_feature SET name = ?2, page = ?3 WHERE app_id = ?1").run(appId, name, page);
41 export function keepDeclaration(db: Database, appId: string, name: string, page: string, summary: string | null = null): void {
42 db.query("UPDATE app_feature SET name = ?2, page = ?3, summary = ?4 WHERE app_id = ?1").run(appId, name, page, summary);
4243 }
4344
4445 /** True when the app was absorbed. Its settings stay, so absorbing again picks them up. */
src/core/migration-pins/20261108180000-app-feature-summary.pin added +1
@@ -0,0 +1,1 @@
1 52c003f8c6851c64f9e83e1fea6098e7fefae8949dbb1f99abe2bedab328e97a
src/core/migrations/20261108180000-app-feature-summary.ts added +6
@@ -0,0 +1,6 @@
1 // The summary an absorbed app declared, kept beside its name and page so the
2 // menu can show it as the entry's title without a git read (#9681). NULL when
3 // the app declared none, or was absorbed before this column. Additive.
4 export default `
5 ALTER TABLE app_feature ADD COLUMN summary TEXT;
6 `;
src/web/layout.ts +3 −2
@@ -15,7 +15,7 @@ import { administersAny } from "../core/admin_repos.ts";
1515 import { appsForRepos } from "../core/apps.ts";
1616 import { foundryHref } from "../actions/foundry_door.ts";
1717 import { appsRuntime } from "../apps/runtime.ts";
18 import { absorbedForNav } from "../actions/features.ts";
18 import { absorbedForNav, menuTitle } from "../actions/features.ts";
1919 import {
2020 enterableApps,
2121 getRepo,
@@ -277,7 +277,8 @@ function featureItems(ctx: Ctx, user: User): MenuItem[] {
277277 runtime === undefined
278278 ? `/${f.repo}`
279279 : `/apps/open?host=${encodeURIComponent(new URL(runtime.urlFor(f.record, "production")).host)}&to=${encodeURIComponent(f.page)}`;
280 return { label: f.name, href, icon: "rocket" as const };
280 const title = menuTitle(f.summary);
281 return { label: f.name, href, icon: "rocket" as const, ...(title === undefined ? {} : { title }) };
281282 });
282283 // Under a heading of their own, so an app's chosen name cannot pass for a forge item.
283284 return entries.length === 0
src/web/ui.ts +3
@@ -1107,6 +1107,8 @@ export interface MenuItem {
11071107 disabled?: string;
11081108 /** The key that does the same, drawn beside the label. */
11091109 hint?: string;
1110 /** Hover text for a link entry; none when absent. */
1111 title?: string;
11101112 }
11111113
11121114 /** A dropdown built on `<details>` — the only construct correct with JS off. */
@@ -1161,6 +1163,7 @@ export function menu(o: {
11611163 return html`<a
11621164 class="menu-item"
11631165 href="${i.href ?? "#"}"
1166 ${i.title ? html`title="${i.title}"` : null}
11641167 ${i.current ? raw('aria-current="true"') : null}
11651168 >${i.icon ? icon(i.icon) : null}<span class="grow">${i.label}</span
11661169 >${i.hint ? keyHint(i.hint) : null}${i.current ? icon("check") : null}</a
test/features.test.ts +32 −2
@@ -15,14 +15,14 @@ import { join } from "node:path";
1515 import { statements } from "./helpers/statements.ts";
1616 import { expectWork } from "./helpers/work.ts";
1717 import { createApp, type App } from "../src/app.ts";
18 import { absorbApp, acceptDeclaration, declaresFeature, absorbedForNav, declaredFeature, changeFeatureSettings, getFeature, unabsorbApp } from "../src/actions/features.ts";
18 import { absorbApp, acceptDeclaration, declaresFeature, absorbedForNav, menuTitle, declaredFeature, changeFeatureSettings, getFeature, unabsorbApp } from "../src/actions/features.ts";
1919 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
2020 import { checkSetting, featureEnv, parseFeature, settingEnvName } from "../src/apps/feature.ts";
2121 import { featureVarsAt } from "../src/apps/feature_env.ts";
2222 import { appEnv } from "../src/apps/process.ts";
2323 import { createApp as insertApp, deleteApp } from "../src/core/apps.ts";
2424 import { listEvents } from "../src/core/events.ts";
25 import { absorb, featureSettings, getAbsorbed } from "../src/core/features.ts";
25 import { absorb, featureSettings, getAbsorbed, keepDeclaration } from "../src/core/features.ts";
2626 import type { Repo } from "../src/core/repos.ts";
2727 import { createToken } from "../src/core/tokens.ts";
2828 import { createUser, type User } from "../src/core/users.ts";
@@ -300,6 +300,36 @@ describe("absorbing an app", () => {
300300 expect(strangers).not.toContain("Radar");
301301 });
302302
303 test("the menu entry carries the declared summary as its title, escaped; none without one", async () => {
304 const id = absorbedForNav(app, ada)[0]!.appId;
305 const before = getAbsorbed(app.db, id)!;
306 const entry = async () => {
307 const page = await (await web(adaCookie, "/settings")).text();
308 return page.match(/<a\s[^>]*href="\/cy\/radar"[^>]*>/)?.[0] ?? "";
309 };
310 try {
311 expect(await entry()).toContain('title="What the platform runs against what exists"');
312 keepDeclaration(app.db, id, before.name, before.page, null);
313 expect(await entry()).not.toContain("title=");
314 keepDeclaration(app.db, id, before.name, before.page, "<script>alert(1)</script>");
315 const hostile = await entry();
316 expect(hostile).not.toContain("<script>");
317 expect(hostile).toContain('title="&lt;script&gt;alert(1)&lt;/script&gt;"');
318 } finally {
319 keepDeclaration(app.db, id, before.name, before.page, before.summary);
320 }
321 });
322
323 test("a summary over 200 characters is shortened at a word with an ellipsis", () => {
324 const long = Array.from({ length: 60 }, () => "word").join(" ");
325 const cut = menuTitle(long)!;
326 expect(cut.length).toBeLessThanOrEqual(200);
327 expect(cut.endsWith("word…")).toBe(true);
328 expect(menuTitle("short one")).toBe("short one");
329 expect(menuTitle(" ")).toBeUndefined();
330 expect(menuTitle(null)).toBeUndefined();
331 });
332
303333 test("a production push that renames the feature leaves the menu unchanged until an admin accepts", async () => {
304334 const renamed = JSON.stringify({ app: { run: "x" }, feature: { ...FEATURE, name: "Radar Pro", page: "/pro" } });
305335 featureVarsAt(app.db, appId, renamed);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs