Skip to content
Myr

code-address.ts

This file

Path

apps/web/src/features/code/code-address.ts

59 lines · 1.7 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 1–59why, one step back
1/**
2 * Where a page of the Code tab is, read from its path alone, so the code bar3 * reads the same address on the server, in the browser and under any route.4 */5import { Rev } from "@repo/protocol/domain/code";6import { projectAddressOf } from "@repo/protocol/ids";7import { Option, Schema } from "effect";89import { segmentsOf } from "../shared/paths.ts";1011/** The Code tab's pages: Files are `tree`, `blob` and `why`; Commits are `commits` and `commit`. */12export type CodePage = "blob" | "commit" | "commits" | "refs" | "tree" | "why";1314export interface CodeAddress {15 readonly page: CodePage;16 readonly rev: string;17 readonly path?: string | undefined;18}1920const PAGES: readonly CodePage[] = [21 "blob",22 "commit",23 "commits",24 "refs",25 "tree",26 "why",27];2829const asRev = Schema.decodeUnknownOption(Rev);3031/** Each segment is decoded once, so a tag's encoded `/` stays inside its own segment. */32export const codeAddressOf = (pathname: string): Option.Option<CodeAddress> =>33 Option.flatMap(projectAddressOf(segmentsOf(pathname)), ({ rest }) => {34 const [first, second, ...more] = rest;35 const page = PAGES.find((each) => each === first);3637 if (page === undefined) {38 return Option.none();39 }4041 if (page === "refs") {42 return Option.some({ page, rev: "main" });43 }4445 return Option.map(asRev(second), (rev) => ({46 page,47 path: more.length === 0 ? undefined : more.join("/"),48 rev,49 }));50 });5152/** The pages under Files, and the rest under Commits; the refs list is neither. */53export const codeViewOf = (page: CodePage): "commits" | "files" | undefined => {54 if (page === "refs") {55 return undefined;56 }5758 return page === "commit" || page === "commits" ? "commits" : "files";59};