sections.ts
This file
Path
apps/web/src/features/shared/shell/sections.ts
234 lines · 7.4 KiB · TypeScript
Last changed by
Merge build/1300-w13-foundation into mainoscabrielbefore adoption0e29c37
History of this fileTake it
Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 1–58why, one step back1/**2 * Where a reader can be: the site's sections, a project's, and the trail of3 * crumbs from the site to the page at an address. The rail, the top bar,4 * the bottom bar, the section row and the context bar all read them here,5 * so one address names one place everywhere.6 */7import type { Posture } from "@repo/protocol/posture";8import { Result } from "effect";910/** Which of a project's pages is being read. */11export type ProjectTab =12 | "code"13 | "env"14 | "landings"15 | "ledger"16 | "overview"17 | "people"18 | "refs";1920/** One of a project's sections: its word, its page, and the tab it is. */21export interface Section {22 readonly label: string;23 readonly tab: ProjectTab;24 readonly to: string;25}2627/**28 * A project's sections (`project` as the address names it), in the order29 * every list of them shows. The project's30 * home is its asks; its `people` page is its policy (who decides, vouches,31 * budgets), and reads so, at the address it has always had.32 */33export const projectSections = (project: string): readonly Section[] => [34 { label: "Asks", tab: "overview", to: `/${project}` },35 { label: "Code", tab: "code", to: `/${project}/tree/main` },36 { label: "Landings", tab: "landings", to: `/${project}/commits/main` },37 { label: "Refs", tab: "refs", to: `/${project}/refs` },38 { label: "Policy", tab: "people", to: `/${project}/people` },39 { label: "Ledger", tab: "ledger", to: `/${project}/ledger` },40 { label: "Env shape", tab: "env", to: `/${project}/env` },41];4243/** The first segment of each of a project's pages, by the section it sits in. */44const SECTION_OF = {45 answers: "overview",46 asks: "overview",47 blob: "code",48 commit: "landings",49 commits: "landings",50 env: "env",51 ledger: "ledger",52 people: "people",53 refs: "refs",54 tree: "code",55 why: "code",56} as const satisfies Record<string, ProjectTab>;5758/** The site's own first segments: every other one names a project. */Merge build/1300-w13-foundation into mainoscabrielbefore adoption0e29c37
line 59why, one step back59const SITE = new Set(["agents", "asks", "docs", "home", "people", "today"]);Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 60–142why, one step back6061/** `segment` decoded, or as it came when it does not decode. */62const decoded = (segment: string) =>63 Result.getOrElse(64 Result.try(() => decodeURIComponent(segment)),65 () => segment66 );6768/** The address's segments, decoded. */69const segmentsOf = (pathname: string) =>70 pathname71 .split("/")72 .filter((segment) => segment !== "")73 .map(decoded);7475/** The project an address is in, by its first segment; none on the site's own pages. */76export const projectOf = (pathname: string): string | undefined => {77 const [first] = segmentsOf(pathname);7879 return first === undefined || SITE.has(first) ? undefined : first;80};8182/** The project section an address sits in; none for a page under none (a decision, a proposal). */83export const sectionOf = (pathname: string): ProjectTab | undefined => {84 const [first, second] = segmentsOf(pathname);8586 if (first === undefined || SITE.has(first)) {87 return undefined;88 }8990 return second === undefined91 ? "overview"92 : Object.entries(SECTION_OF).find(([segment]) => segment === second)?.[1];93};9495/** One step of the trail to a page: its word, its page (none for the page itself), and whether it is an id. */96export interface Crumb {97 readonly label: string;98 readonly mono?: boolean;99 readonly to?: string;100}101102const SECTION_LABEL = {103 code: "Code",104 env: "Env shape",105 landings: "Landings",106 ledger: "Ledger",107 overview: "Asks",108 people: "Policy",109 refs: "Refs",110} as const satisfies Record<ProjectTab, string>;111112/** A section's crumb, linked to its page when a page sits below it. */113const sectionCrumb = (114 project: string,115 tab: ProjectTab,116 below: boolean117): Crumb => {118 const label = SECTION_LABEL[tab];119 const section = projectSections(project).find((each) => each.tab === tab);120121 return below && section !== undefined ? { label, to: section.to } : { label };122};123124/** A project's crumb, then its section's. */125const projectCrumbs = (126 project: string,127 tab: ProjectTab,128 below: boolean129): readonly Crumb[] => [130 { label: project, to: `/${project}` },131 sectionCrumb(project, tab, below),132];133134/** A code page's trail: Code, then the path in hand (none at the root). */135const pathCrumbs = (project: string, path: string): readonly Crumb[] =>136 path === ""137 ? projectCrumbs(project, "code", false)138 : [...projectCrumbs(project, "code", true), { label: path, mono: true }];139140/** The site's own pages' trails, by first segment. */141const SITE_CRUMBS = {142 agents: () => [{ label: "For agents" }],Merge build/1300-w13-foundation into mainoscabrielbefore adoption0e29c37
lines 143–148why, one step back143 asks: () => [{ label: "Asks" }],144 docs: (page: string | undefined) =>145 page === undefined146 ? [{ label: "Docs" }]147 : [{ label: "Docs", to: "/docs" }, { label: page }],148 home: () => [{ label: "Home" }],Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 149–234why, one step back149 people: (handle: string | undefined) => [{ label: handle ?? "People" }],150 today: () => [{ label: "Today" }],151} as const satisfies Record<152 string,153 (second: string | undefined) => readonly Crumb[]154>;155156/** A project's subject pages' trails (an ask, an answer, a commit, a path…), by second segment. */157const SUBJECT_CRUMBS = {158 answers: (project: string, ask: string) => [159 ...projectCrumbs(project, "overview", true),160 { label: ask, mono: true, to: `/${project}/asks/${ask}` },161 { label: "Answer" },162 ],163 asks: (project: string, ask: string) => [164 ...projectCrumbs(project, "overview", true),165 { label: ask, mono: true },166 ],167 blob: (project: string, _rev: string, path: string) =>168 pathCrumbs(project, path),169 commit: (project: string, sha: string) => [170 ...projectCrumbs(project, "landings", true),171 { label: sha.slice(0, 7), mono: true },172 ],173 decisions: (project: string, decision: string) => [174 { label: project, to: `/${project}` },175 { label: "Today", to: "/today" },176 { label: decision, mono: true },177 ],178 proposals: (project: string, proposal: string) => [179 ...projectCrumbs(project, "overview", true),180 proposal === "new" ? { label: "Propose" } : { label: proposal, mono: true },181 ],182 tree: (project: string, _rev: string, path: string) =>183 pathCrumbs(project, path),184 why: (project: string, _rev: string, path: string) =>185 pathCrumbs(project, path),186} as const satisfies Record<187 string,188 (project: string, third: string, rest: string) => readonly Crumb[]189>;190191/**192 * The trail from the site to the page at `pathname`: the project first, then193 * its section, then the thing in hand (an ask, a commit, a path), each a194 * link but the last. A page that knows better gives `Workspace` its own.195 */196export const crumbsFor = (pathname: string): readonly Crumb[] => {197 const [first, second, third, ...rest] = segmentsOf(pathname);198199 if (first === undefined) {200 return [{ label: "Asks" }];201 }202203 const site = Object.entries(SITE_CRUMBS).find(([name]) => name === first);204205 if (site !== undefined) {206 return site[1](second);207 }208209 const subject = Object.entries(SUBJECT_CRUMBS).find(210 ([name]) => name === second211 );212213 if (subject !== undefined && third !== undefined) {214 return subject[1](first, third, rest.join("/"));215 }216217 const tab = sectionOf(pathname);218219 return tab === undefined220 ? [221 { label: first, to: `/${first}` },222 { label: rest.at(-1) ?? third ?? second ?? first },223 ]224 : projectCrumbs(first, tab, false);225};226227/** A posture in one short word for the rail, beside its mark; the full label is its title. */228export const POSTURE_WORD = {229 open: "Open",230 openCore: "Core",231 openOnSchedule: "Scheduled",232 openWithSealedEvidence: "Sealed",233 sourceAvailable: "Source",234} as const satisfies Record<Posture, string>;