sections.ts
This file
Path
apps/web/src/features/shared/shell/sections.ts
271 lines · 7.4 KiB · TypeScript
Last changed by
Merge project/drop-proposals into mainoscabrielbefore adoptionb4ed08b
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272import { projectAddressOf } from "@repo/protocol/ids";import { Option } from "effect";
import { INBOX_PATH, segmentsOf } from "../paths.ts";
/** A project's seven tabs (ADR 0041, with Discussions), in the header's order. */export type ProjectTab = | "asks" | "code" | "discussions" | "landings" | "overview" | "policy" | "visibility";
export interface Section { readonly label: string; readonly tab: ProjectTab; readonly to: string; /** A tab that opens a page's filtered view carries the filter in the query. */ readonly search?: Readonly<Record<string, string>>;}
/** * Each tab opens a page of its own inside the project's frame: Landings is * the history of main shown as its landings (the Code tab's History, * filtered), and Policy holds who decides as well as the limits. */export const projectSections = (project: string): readonly Section[] => [ { label: "Overview", tab: "overview", to: `/${project}` }, { label: "Code", tab: "code", to: `/${project}/tree/main` }, { label: "Asks", tab: "asks", to: `/${project}/asks` }, { label: "Landings", search: { show: "landings" }, tab: "landings", to: `/${project}/commits/main`, }, { label: "Discussions", tab: "discussions", to: `/${project}/discussions` }, { label: "Policy", tab: "policy", to: `/${project}/policy` }, { label: "Visibility", tab: "visibility", to: `/${project}/visibility` },];
/** The tab each page of a project sits under, by its first segment: the history and a commit are the Landings tab's, as its crumb says. */const TAB_OF = { answers: "landings", asks: "asks", blob: "code", commit: "landings", commits: "landings", decisions: "asks", discussions: "discussions", policy: "policy", 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 = | "asks" | "code" | "discussions" | "landings" | "overview" | "policy" | "refs" | "visibility";
const PAGE_TO = (project: string) => ({ asks: `/${project}/asks`, code: `/${project}/tree/main`, discussions: `/${project}/discussions`, landings: `/${project}/commits/main`, overview: `/${project}`, policy: `/${project}/policy`, refs: `/${project}/refs`, visibility: `/${project}/visibility`, }) as const satisfies Record<PageSection, string>;
const SECTION_OF = { answers: "asks", asks: "asks", blob: "code", commit: "landings", commits: "landings", discussions: "discussions", policy: "policy", 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];};
/** Whether a path is a project's page drawn in the project's frame, under its header. */export const inProjectFrame = (pathname: string): boolean => addressOf(pathname) !== undefined;
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 = { asks: "Asks", code: "Code", discussions: "Discussions", landings: "Landings", overview: "Overview", policy: "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, "asks", true), { label: ask, mono: true, to: `/${project}/asks/${ask}` }, { label: "Answer" }, ], asks: (project: string, ask: string) => [ ...projectCrumbs(project, "asks", 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 }, ], discussions: (project: string, discussion: string) => [ ...projectCrumbs(project, "discussions", true), { label: discussion, mono: true }, ], decisions: (project: string, decision: string) => [ { label: project, to: `/${project}` }, { label: "Inbox", to: INBOX_PATH }, { label: decision, 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);};