sections.ts
This file
Path
apps/web/src/features/shared/shell/sections.ts
289 lines · 7.9 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290import { projectAddressOf } from "@repo/protocol/ids";import { Option } from "effect";
import { INBOX_PATH, segmentsOf } from "../paths.ts";
/** A project's eight tabs (ADR 0041), in the header's order. */export type ProjectTab = | "asks" | "code" | "landings" | "overview" | "people" | "policy" | "proposals" | "visibility";
export interface Section { readonly label: string; readonly tab: ProjectTab; readonly to: string; /** A tab whose own page has not been built yet opens the region of Overview that holds it. */ readonly hash?: string;}
/** * Each tab opens a page inside the project's frame that exists today: Asks, * Landings, Proposals and People open their regions of Overview (the history * of commits is the Code tab's), Policy the policy at the address it has * always had, and Visibility its own page. Starting a * proposal is a focused page of its own, so it is Contribute's, not a tab's. */export const projectSections = (project: string): readonly Section[] => [ { label: "Overview", tab: "overview", to: `/${project}` }, { label: "Code", tab: "code", to: `/${project}/tree/main` }, { hash: "open-asks", label: "Asks", tab: "asks", to: `/${project}` }, { hash: "recent-landings", label: "Landings", tab: "landings", to: `/${project}`, }, { hash: "proposals", label: "Proposals", tab: "proposals", to: `/${project}`, }, { hash: "who-decides", label: "People", tab: "people", to: `/${project}` }, { label: "Policy", tab: "policy", to: `/${project}/people` }, { label: "Visibility", tab: "visibility", to: `/${project}/visibility` },];
/** The tab each page of a project sits under, by its first segment. */const TAB_OF = { answers: "landings", asks: "asks", blob: "code", commit: "code", commits: "code", decisions: "asks", people: "policy", proposals: "proposals", raw: "code", refs: "code", tree: "code", visibility: "visibility", why: "code",} as const satisfies Record<string, ProjectTab>;
/** A crumb names the page itself, which may be narrower than its tab. */type PageSection = | "code" | "landings" | "overview" | "people" | "refs" | "visibility";
const PAGE_TO = (project: string) => ({ code: `/${project}/tree/main`, landings: `/${project}/commits/main`, overview: `/${project}`, people: `/${project}/people`, refs: `/${project}/refs`, visibility: `/${project}/visibility`, }) as const satisfies Record<PageSection, string>;
const SECTION_OF = { answers: "overview", asks: "overview", blob: "code", commit: "landings", commits: "landings", people: "people", refs: "refs", tree: "code", visibility: "visibility", why: "code",} as const satisfies Record<string, PageSection>;
const SITE = new Set(["agents", "asks", "docs", "import", "inbox", "people"]);
const addressOf = (pathname: string) => { const segments = segmentsOf(pathname); const [first] = segments;
return first === undefined || SITE.has(first) ? undefined : Option.getOrUndefined(projectAddressOf(segments));};
export const projectOf = (pathname: string): string | undefined => addressOf(pathname)?.spelled;
/** The tab a project's page sits under; Overview for the project's own address. */export const projectTabOf = (pathname: string): ProjectTab | undefined => { const address = addressOf(pathname);
if (address === undefined) { return undefined; }
const [page] = address.rest;
return page === undefined ? "overview" : Object.entries(TAB_OF).find(([segment]) => segment === page)?.[1];};
/** * The pages under a project that its route draws in another shell: starting * a proposal is focused (`staticData.shell` is `focus`), so it has no header. */const OUTSIDE_FRAME: readonly (readonly string[])[] = [["proposals", "new"]];
/** Whether a path is a project's page drawn in the project's frame, under its header. */export const inProjectFrame = (pathname: string): boolean => { const address = addressOf(pathname);
return ( address !== undefined && !OUTSIDE_FRAME.some( (page) => page.length === address.rest.length && page.every((segment, at) => address.rest[at] === segment) ) );};
const sectionOf = (pathname: string): PageSection | undefined => { const address = addressOf(pathname);
if (address === undefined) { return undefined; }
const [page] = address.rest;
return page === undefined ? "overview" : Object.entries(SECTION_OF).find(([segment]) => segment === page)?.[1];};
export interface Crumb { readonly label: string; readonly mono?: boolean; readonly to?: string;}
const SECTION_LABEL = { code: "Code", landings: "Landings", overview: "Asks", people: "Policy", refs: "Refs", visibility: "Visibility",} as const satisfies Record<PageSection, string>;
const sectionCrumb = ( project: string, tab: PageSection, below: boolean): Crumb => { const label = SECTION_LABEL[tab];
return below ? { label, to: PAGE_TO(project)[tab] } : { label };};
const projectCrumbs = ( project: string, tab: PageSection, below: boolean): readonly Crumb[] => [ { label: project, to: `/${project}` }, sectionCrumb(project, tab, below),];
const pathCrumbs = (project: string, path: string): readonly Crumb[] => path === "" ? projectCrumbs(project, "code", false) : [...projectCrumbs(project, "code", true), { label: path, mono: true }];
const SITE_CRUMBS = { asks: () => [{ label: "Asks" }], docs: (page: string | undefined) => page === undefined ? [{ label: "Docs" }] : [{ label: "Docs", to: "/docs" }, { label: page }], inbox: () => [{ label: "Inbox" }], people: (handle: string | undefined) => [{ label: handle ?? "People" }],} as const satisfies Record< string, (second: string | undefined) => readonly Crumb[]>;
const SUBJECT_CRUMBS = { answers: (project: string, ask: string) => [ ...projectCrumbs(project, "overview", true), { label: ask, mono: true, to: `/${project}/asks/${ask}` }, { label: "Answer" }, ], asks: (project: string, ask: string) => [ ...projectCrumbs(project, "overview", true), { label: ask, mono: true }, ], blob: (project: string, _rev: string, path: string) => pathCrumbs(project, path), commit: (project: string, sha: string) => [ ...projectCrumbs(project, "landings", true), { label: sha.slice(0, 7), mono: true }, ], decisions: (project: string, decision: string) => [ { label: project, to: `/${project}` }, { label: "Inbox", to: INBOX_PATH }, { label: decision, mono: true }, ], proposals: (project: string, proposal: string) => [ ...projectCrumbs(project, "overview", true), proposal === "new" ? { label: "Propose" } : { label: proposal, mono: true }, ], tree: (project: string, _rev: string, path: string) => pathCrumbs(project, path), why: (project: string, _rev: string, path: string) => pathCrumbs(project, path),} as const satisfies Record< string, (project: string, third: string, rest: string) => readonly Crumb[]>;
export const crumbsFor = (pathname: string): readonly Crumb[] => { const [first, second] = segmentsOf(pathname);
if (first === undefined) { return [{ label: "Asks" }]; }
const site = Object.entries(SITE_CRUMBS).find(([name]) => name === first);
if (site !== undefined) { return site[1](second); }
const address = addressOf(pathname);
if (address === undefined) { return [{ label: first }]; }
const project = address.spelled; const [page, third, ...rest] = address.rest;
const subject = Object.entries(SUBJECT_CRUMBS).find( ([name]) => name === page );
if (subject !== undefined && third !== undefined) { return subject[1](project, third, rest.join("/")); }
const tab = sectionOf(pathname);
return tab === undefined ? [ { label: project, to: `/${project}` }, { label: rest.at(-1) ?? third ?? page ?? project }, ] : projectCrumbs(project, tab, false);};