rev.ts
This file
Path
apps/web/src/features/code/rev.ts
29 lines · 1.2 KiB · TypeScript
Last changed by
Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
History of this fileTake it
123456789101112131415161718192021222324252627282930/** * A revision as one segment of a code page's path * (`/$project/tree/$rev/$`): `main`, a 40-hex commit SHA, or a tag name. * A tag name may hold `/` and `@` (`@changesets/cli@2.27.1`), so it is * URL-encoded into one segment; `main` and a SHA encode to themselves. * Whether a revision names anything is the platform's to say (`readTree` * and the other code reads refuse with `UnknownRevision`). */import { Rev } from "@repo/protocol/domain/code";import { Option, Schema } from "effect";
/** `rev` as one path segment. */export const revSegment = (rev: string): string => encodeURIComponent(rev);
const decodeSegment = Option.liftThrowable(decodeURIComponent);
const asRev = Schema.decodeUnknownOption(Rev);
/** * The revision a path segment names: none for a segment that is not a * valid encoding (`%`) or not a revision at all (empty, too long). A * segment the router already decoded reads as itself. */export const revOf = (segment: string): Option.Option<string> => Option.flatMap(decodeSegment(segment), asRev);
/** The revision a page reads, beside its title: a commit by its short id. */export const revLabel = (rev: string) => /^[0-9a-f]{40}$/u.test(rev) ? rev.slice(0, 7) : rev;