From 352de29013ec66347893fd25a2fd19fda09aad1b Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Sat, 31 Oct 2026 08:37:45 +0100 Subject: [PATCH] Retire shell-context and render the dashboard from ctx.chrome --- README.md | 2 +- src/http/app.ts | 3 +-- src/ui/chrome.test.ts | 8 +++++++- src/ui/chrome.ts | 29 ++++++++++++++++++++++++++++- todo.md | 6 ------ views/index.ejs | 17 ++++++++--------- 6 files changed, 45 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 252e5d0..c02d72d 100644 --- a/README.md +++ b/README.md @@ -1825,7 +1825,7 @@ src/ The app — strict tsc, no build step. *.test.ts sit beside plugin-host/ plugin.ts (the contract) · plugin-api.ts (the `@plainpages/plugin-api` barrel) · system.ts (ctx.system) · discovery · router · hooks · view-resolver · storage (the rules) · storage-provisioning (the DDL; bootstrap-only, holds the driver) - ui/ chrome (the one global menu) · shell-context · dashboard · nav (composeNav) · + ui/ chrome (the one global menu) · nav (composeNav) · menu-config (`#menu-config`) · icons (lucide sprite builder) · list-query · paginate plugin-api/ The `@plainpages/plugin-api` package — the author barrel, linked into /node_modules diff --git a/src/http/app.ts b/src/http/app.ts index b354c63..f55baff 100644 --- a/src/http/app.ts +++ b/src/http/app.ts @@ -8,7 +8,6 @@ import { buildPluginChrome, type PageChrome } from "../ui/chrome.ts"; import { buildContext, type RequestContext, type User } from "./context.ts"; import { csrfCookie, ensureCsrfToken, verifyCsrfRequest } from "../auth/csrf.ts"; import type { Denylist } from "../auth/denylist.ts"; -import { buildDashboardModel } from "../ui/dashboard.ts"; import { PLUGINS_DIR } from "../plugin-host/discovery.ts"; import { GuardError, loginRedirect } from "../auth/guards.ts"; import { ENGLISH_I18N } from "../i18n/english.ts"; @@ -168,7 +167,7 @@ export function createApp(options: AppOptions = {}): Server { await sendResult(ctx.res, result, pluginViewsFor(pluginCtx, dashboardPlugin.id), pluginCtx.localeHref); return null; } - return { data: { model: buildDashboardModel({ csrfToken: csrf.token, menu, user: ctx.user, nav: ctx.chrome.nav, t: ctx.t }) }, view: "index" }; + return { data: { chrome: ctx.chrome }, view: "index" }; }; // The internal route table, matched after plugin routes: the auth/OAuth2 group (src/auth/ diff --git a/src/ui/chrome.test.ts b/src/ui/chrome.test.ts index df39503..87b79fd 100644 --- a/src/ui/chrome.test.ts +++ b/src/ui/chrome.test.ts @@ -1,6 +1,6 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { buildPluginChrome } from "./chrome.ts"; +import { buildPluginChrome, shellUser } from "./chrome.ts"; import { DEFAULT_MENU, type MenuConfig } from "./menu-config.ts"; import type { NavNode } from "./nav.ts"; import type { Plugin } from "../plugin-host/plugin.ts"; @@ -74,3 +74,9 @@ test("branding logo + default theme flow through when set", () => { assert.equal(chrome.brand.name, "Acme"); assert.equal(chrome.theme, "dark"); }); + +test("shellUser derives the profile from the real user; anonymous → Guest", () => { + assert.deepEqual(shellUser(null), { email: "", initials: "G", name: "Guest" }); + // Real user: name = email local part, email kept, initials = first two letters of the local part. + assert.deepEqual(shellUser({ email: "ada@example.com", id: "u1", permissions: [] }), { email: "ada@example.com", initials: "AD", name: "ada" }); +}); diff --git a/src/ui/chrome.ts b/src/ui/chrome.ts index 984cd87..d5f42fb 100644 --- a/src/ui/chrome.ts +++ b/src/ui/chrome.ts @@ -8,10 +8,15 @@ import type { Translate } from "../i18n/translate.ts"; import { type MenuConfig } from "./menu-config.ts"; import { composeNav, type NavNode } from "./nav.ts"; import type { Plugin } from "../plugin-host/plugin.ts"; -import { branding, shellUser, type ShellUser } from "./shell-context.ts"; const DASHBOARD_NAV: NavNode = { href: "/dashboard", icon: "i-grid", id: "dashboard", label: "nav.dashboard", session: true }; +export interface ShellUser { + email: string; + initials: string; + name: string; +} + export interface PageChrome { brand: { logo?: string; name: string; sub?: string }; csrfToken: string; // double-submit token for the shell's Sign-out form + a plugin's own forms @@ -65,6 +70,28 @@ export function buildPluginChrome(opts: ChromeOptions): PageChrome { }; } +// The User carries no display name (the JWT holds only id/email/permissions), so the profile shows +// the email's local part as the name; anonymous ⇒ "Guest". +export function shellUser(user: User | null | undefined, t: Translate = ENGLISH): ShellUser { + if (!user) { + const guest = t("shell.guest"); + return { email: "", initials: ([...guest][0] ?? "?").toUpperCase(), name: guest }; // by character: the word is translated + } + const local = user.email.split("@")[0] || user.email; + return { email: user.email, initials: (local.slice(0, 2) || "U").toUpperCase(), name: local }; +} + +// `name`/`sub` pass through `t`, so a catalog key is translated and an operator's own wording renders as written. +function branding(menu: MenuConfig, t: Translate): { logo?: string; name: string; sub?: string; theme?: string } { + const b = menu.branding; + return { + ...(b.logo != null ? { logo: b.logo } : {}), + name: t(b.name), + ...(b.sub != null ? { sub: t(b.sub) } : {}), + ...(b.theme != null ? { theme: b.theme } : {}), + }; +} + function translateNav(nodes: NavNode[], t: Translate): NavNode[] { return nodes.map((node) => ({ ...node, diff --git a/todo.md b/todo.md index 87050da..6c4759e 100644 --- a/todo.md +++ b/todo.md @@ -12,7 +12,6 @@ | ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | |---|---|---|---|---|---|---|---|---|---|---|---| -| 24 | 0.4.21 | | | **Retire `src/ui/shell-context.ts` and render the dashboard from `ctx.chrome`.** | 2 | 1 | 2 | 3 | 4 | code health by 3 | 9.0 | | 21 | 0.4.21 | | | **Fail the gate on a function that exceeds a size or complexity limit.** | 3 | 1 | 1 | 3 | 5 | code health by 4 | 7.2 | | 32 | 0.4.21 | | | **Fail the gate on a stylesheet feature the browser floor lacks, unless it is guarded.** | 3 | 1 | 1 | 3 | 4 | code health by 2 | 6.0 | | 20 | 0.4.21 | | | **Redirect an anonymous hit on a session-needing flow page to sign-in, instead of a 500.** | 2 | 2 | 2 | 4 | 3 | 2 by 4 | 4.2 | @@ -34,11 +33,6 @@ ## Details -### 24. Retire `src/ui/shell-context.ts` and render the dashboard from `ctx.chrome`. - -`ShellModel`/`buildShellContext` has one consumer, the dashboard, and duplicates `PageChrome` on -almost every field. Keep `shellUser` as the shared primitive. - ### 21. Fail the gate on a function that exceeds a size or complexity limit. Only `tsc --strict` runs today. A new tool is a new dependency, so justify it. diff --git a/views/index.ejs b/views/index.ejs index 29a9dee..b5a9e53 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -2,9 +2,9 @@ Dashboard: the gated post-login app home. The built-in default is a short instructional starter — what /dashboard is and how to replace it from a plugin. Composes the app shell (the one global menu via model.nav) around the prose. A plugin owns the real dashboard via a `dashboard` - handler; this renders until then. Data: model { nav, shell }. + handler; this renders until then. Data: chrome (PageChrome). %><% - const nav = include("partials/nav-tree", { nodes: model.nav }); + const nav = include("partials/nav-tree", { nodes: chrome.nav }); // These four messages carry markup ( spans), so they render raw — the documented // markup-carrying case. They interpolate nothing, so there is no untrusted data to escape. const body = ` @@ -27,12 +27,11 @@ -%> <%- include("partials/shell", { body, - brand: model.shell.brand, - breadcrumbs: model.shell.breadcrumbs, - csrfToken: model.shell.csrfToken, + brand: chrome.brand, + breadcrumbs: [{ label: t("dashboard.title") }], + csrfToken: chrome.csrfToken, nav, - signInHref: model.shell.signInHref, - theme: model.shell.theme, - title: model.shell.title, - user: model.shell.user, + theme: chrome.theme, + title: t("dashboard.title"), + user: chrome.user, }) %>