import m from '@garage44/mithril' import {type Ctx, draw, inline, langOf} from './components' import {Initial, SiteBar, type Step} from './sitebar' import {HOME, NO_MENU, type PageData, type SiteMenu, isTextRef, pageHref, resolve, siteText} from './tree' export {siteText} /** * What a content page draws, on the server and in the browser alike: the site's * bar (`sitebar.tsx`) and the page's tree under it. * * The simulator renders this to HTML for every visit (`ssr.tsx`) and the * browser then takes that HTML over (`pages.tsx`), keeping its elements and * attaching handlers. That works only while both draw the same tree, node for * node, so nothing here may read the browser — `location`, `localStorage`, the * clock, the theme: where the page is comes in as `where`, who is visiting as * `visitor` (null until after the takeover), and anything else that differs * per visitor belongs in a component's `oncreate`. Mithril repairs a difference * without saying so (`ssr.test.ts` holds the two to one output). */ export interface Where { lang: string slug: string } /** Who is reading, as far as this browser knows: set only after the takeover. */ export interface Visitor { name: string admin: boolean } export interface FrameAttrs { lang: string body: m.Children languages: string[] ctx: Ctx | null where: Where | null draft: boolean menu: SiteMenu visitor: Visitor | null } /** The page's word in the path: its menu label when it has one, else its title. */ function pageStep(ctx: Ctx | null, slug: string, menu: SiteMenu): m.Children { const item = menu.pages.find((p) => p.slug === slug) if (item) return {item.label.text} const title = ctx?.data.tree.p?.title if (!ctx || !isTextRef(title)) return slug const r = resolve(ctx.data, title.t) return r ? {inline(r)} : slug } export function Frame(a: FrameAttrs) { const {ctx, draft, lang, menu, where} = a const label = (key: string) => { if (!ctx) return siteText(lang, key) const r = resolve(ctx.data, key) return {inline(r)} } const home = where?.slug === HOME const path: Step[] | null = home ? null : where ? [{label: pageStep(ctx, where.slug, menu)}] : [] const languages = where && a.languages.length > 1 ? a.languages.map((l) => ({href: `${pageHref(l, where.slug)}${draft ? '?draft' : ''}`, lang: l})) : [] return (