Public: anyone can read this; the crew can change it.
A feature's menu entry carries its declared summary as its title #9686
mason/forge:mason/9681-a-feature-s-menu-entry-carries-its-decla → main 3 commits fork
7 files
changed +72
−15
src/actions/features.ts +19 −4
| @@ -204,10 +204,24 @@ export interface NavFeature { | ||
| 204 | 204 | readonly name: string; |
| 205 | 205 | /** The app path to open: the feature's page. */ |
| 206 | 206 | readonly page: string; |
| 207 | /** What the app declared about itself when absorbed, for the entry's title. */ | |
| 208 | readonly summary: string | null; | |
| 207 | 209 | /** The app record already read, so the menu does not fetch it again. */ |
| 208 | 210 | readonly record: AppRecord; |
| 209 | 211 | } |
| 210 | 212 | |
| 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 | ||
| 211 | 225 | /** Absorbed apps the actor may read, for the forge's nav. Cheap enough for every page: no git reads. */ |
| 212 | 226 | export function absorbedForNav(app: App, actor: User | undefined): NavFeature[] { |
| 213 | 227 | const out: NavFeature[] = []; |
| @@ -216,7 +230,7 @@ export function absorbedForNav(app: App, actor: User | undefined): NavFeature[] | ||
| 216 | 230 | if (record === undefined) continue; |
| 217 | 231 | const repo = getRepoById(app.db, record.repoId); |
| 218 | 232 | 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 }); | |
| 220 | 234 | } |
| 221 | 235 | return out; |
| 222 | 236 | } |
| @@ -242,7 +256,7 @@ export async function absorbApp(app: App, actor: User | undefined, slug: string) | ||
| 242 | 256 | if (!siteAdmin(actor)) return err("forbidden"); |
| 243 | 257 | const declared = await declaredFeature(app, found.repo); |
| 244 | 258 | 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)) { | |
| 246 | 260 | app.bus.append({ kind: "app.absorbed", repoId: found.repo.id, actorId: actor.id, payload: { slug, name: declared.value.name } }); |
| 247 | 261 | await redeployAll(app, found.record, found.repo); |
| 248 | 262 | } |
| @@ -268,9 +282,10 @@ export async function acceptDeclaration( | ||
| 268 | 282 | const declared = await declaredFeature(app, found.repo); |
| 269 | 283 | if (!declared.ok || declared.value === undefined) return err("not_a_feature"); |
| 270 | 284 | const { name, page } = declared.value; |
| 285 | const summary = declared.value.summary ?? null; | |
| 271 | 286 | 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); | |
| 274 | 289 | app.bus.append({ |
| 275 | 290 | kind: "app.feature_declared", |
| 276 | 291 | repoId: found.repo.id, |
src/core/features.ts +8 −7
| @@ -12,10 +12,11 @@ export interface Absorbed { | ||
| 12 | 12 | readonly absorbedAt: string; |
| 13 | 13 | readonly name: string; |
| 14 | 14 | readonly page: string; |
| 15 | readonly summary: string | null; | |
| 15 | 16 | } |
| 16 | 17 | |
| 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 }); | |
| 19 | 20 | |
| 20 | 21 | export function getAbsorbed(db: Database, appId: string): Absorbed | undefined { |
| 21 | 22 | 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[] { | ||
| 28 | 29 | } |
| 29 | 30 | |
| 30 | 31 | /** 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 { | |
| 32 | 33 | return ( |
| 33 | 34 | 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 | |
| 36 | 37 | ); |
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | /** 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); | |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | 45 | /** 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"; | ||
| 15 | 15 | import { appsForRepos } from "../core/apps.ts"; |
| 16 | 16 | import { foundryHref } from "../actions/foundry_door.ts"; |
| 17 | 17 | import { appsRuntime } from "../apps/runtime.ts"; |
| 18 | import { absorbedForNav } from "../actions/features.ts"; | |
| 18 | import { absorbedForNav, menuTitle } from "../actions/features.ts"; | |
| 19 | 19 | import { |
| 20 | 20 | enterableApps, |
| 21 | 21 | getRepo, |
| @@ -277,7 +277,8 @@ function featureItems(ctx: Ctx, user: User): MenuItem[] { | ||
| 277 | 277 | runtime === undefined |
| 278 | 278 | ? `/${f.repo}` |
| 279 | 279 | : `/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 }) }; | |
| 281 | 282 | }); |
| 282 | 283 | // Under a heading of their own, so an app's chosen name cannot pass for a forge item. |
| 283 | 284 | return entries.length === 0 |
src/web/ui.ts +3
| @@ -1107,6 +1107,8 @@ export interface MenuItem { | ||
| 1107 | 1107 | disabled?: string; |
| 1108 | 1108 | /** The key that does the same, drawn beside the label. */ |
| 1109 | 1109 | hint?: string; |
| 1110 | /** Hover text for a link entry; none when absent. */ | |
| 1111 | title?: string; | |
| 1110 | 1112 | } |
| 1111 | 1113 | |
| 1112 | 1114 | /** A dropdown built on `<details>` — the only construct correct with JS off. */ |
| @@ -1161,6 +1163,7 @@ export function menu(o: { | ||
| 1161 | 1163 | return html`<a |
| 1162 | 1164 | class="menu-item" |
| 1163 | 1165 | href="${i.href ?? "#"}" |
| 1166 | ${i.title ? html`title="${i.title}"` : null} | |
| 1164 | 1167 | ${i.current ? raw('aria-current="true"') : null} |
| 1165 | 1168 | >${i.icon ? icon(i.icon) : null}<span class="grow">${i.label}</span |
| 1166 | 1169 | >${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"; | ||
| 15 | 15 | import { statements } from "./helpers/statements.ts"; |
| 16 | 16 | import { expectWork } from "./helpers/work.ts"; |
| 17 | 17 | 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"; | |
| 19 | 19 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 20 | 20 | import { checkSetting, featureEnv, parseFeature, settingEnvName } from "../src/apps/feature.ts"; |
| 21 | 21 | import { featureVarsAt } from "../src/apps/feature_env.ts"; |
| 22 | 22 | import { appEnv } from "../src/apps/process.ts"; |
| 23 | 23 | import { createApp as insertApp, deleteApp } from "../src/core/apps.ts"; |
| 24 | 24 | 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"; | |
| 26 | 26 | import type { Repo } from "../src/core/repos.ts"; |
| 27 | 27 | import { createToken } from "../src/core/tokens.ts"; |
| 28 | 28 | import { createUser, type User } from "../src/core/users.ts"; |
| @@ -300,6 +300,36 @@ describe("absorbing an app", () => { | ||
| 300 | 300 | expect(strangers).not.toContain("Radar"); |
| 301 | 301 | }); |
| 302 | 302 | |
| 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="<script>alert(1)</script>"'); | |
| 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 | ||
| 303 | 333 | test("a production push that renames the feature leaves the menu unchanged until an admin accepts", async () => { |
| 304 | 334 | const renamed = JSON.stringify({ app: { run: "x" }, feature: { ...FEATURE, name: "Radar Pro", page: "/pro" } }); |
| 305 | 335 | featureVarsAt(app.db, appId, renamed); |
new above
offline