code.ts
This file
Path
apps/web/src/client/code.ts
134 lines · 4.8 KiB · TypeScript
Last changed by
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 1–5why, one step back1/**2 * The code browser's reads (the data pattern in `src/surfaces.ts`): one3 * atom per code contract over the browser's `/rpc`, each taking the4 * contract's own payload and keyed by the project's code (`keys.code`),5 * which a landing changes. The code pages read on the server first (theirMerge build/1005oscabrielbefore adoptionb124cbb
lines 6–8why, one step back6 * loaders), so the browser calls only the reads a page draws there (every7 * path for the file tree, `pathsAtom`) or keeps live: the project page's8 * root folder and README (`projectCodeAtom`), which its loader also read.Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 9–10why, one step back9 */10import type { Contract } from "@repo/protocol/contracts";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 11–13why, one step back11import type { CommitView } from "@repo/protocol/domain/code";12import type { ProjectName, RepoPath } from "@repo/protocol/ids";13import { Array, Effect, Option } from "effect";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 14–56why, one step back14import { Atom } from "effect/reactivity";15import type { Rpc } from "effect/rpc";1617import { ContractsClient } from "../surfaces.ts";18import { keys } from "./keys.ts";1920/** What the contract tagged `Tag` takes. */21type PayloadOf<Tag extends string> = Rpc.Payload<Rpc.ExtractTag<Contract, Tag>>;2223const options = (project: ProjectName) => ({24 reactivityKeys: keys.code(project),25});2627/** Main, the tags and the forks. */28export const refsAtom = Atom.family((project: ProjectName) =>29 Atom.withServerValueInitial(30 ContractsClient.query("listRefs", { project }, options(project))31 )32);3334/** One folder; the root at main when the payload names neither. */35export const treeAtom = (payload: PayloadOf<"readTree">) =>36 ContractsClient.query("readTree", payload, options(payload.project));3738/** Every file path at a revision, for the file tree. */39export const pathsAtom = (payload: PayloadOf<"listPaths">) =>40 ContractsClient.query("listPaths", payload, options(payload.project));4142/** One file at a revision. */43export const fileAtom = (payload: PayloadOf<"readFile">) =>44 ContractsClient.query("readFile", payload, options(payload.project));4546/** A page of the log. */47export const logAtom = (payload: PayloadOf<"log">) =>48 ContractsClient.query("log", payload, options(payload.project));4950/** One commit and what it changed. */51export const commitAtom = (payload: PayloadOf<"showCommit">) =>52 ContractsClient.query("showCommit", payload, options(payload.project));5354/**55 * The project page's code: the root folder at main and, when it has one,56 * its README, read after it. A project with nothing landed yet refusesMerge build/1005oscabrielbefore adoptionb124cbb
lines 57–60why, one step back57 * the read, and its page shows no code. It renders as waiting on the58 * server, where the page draws its loader's read of the same code59 * (`server/folder.ts`); in the browser it takes over once it has a value,60 * and reads again on each landing.Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 61–84why, one step back61 */62export const projectCodeAtom = Atom.family((project: ProjectName) =>63 Atom.withServerValueInitial(64 ContractsClient.runtime.factory.withReactivity(keys.code(project))(65 ContractsClient.runtime.atom(66 Effect.gen(function* () {67 const client = yield* ContractsClient;68 const root = yield* client("readTree", { at: "main", project });6970 if (root.readme === undefined) {71 return { root };72 }7374 const readme = yield* client("readFile", {75 at: root.at,76 path: root.readme.path,77 project,78 });7980 return { readme, root };81 })82 )83 )84 )Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 85–133why, one step back85);8687/** At most this many of a folder's entries have what last changed them read. */88export const LAST_CHANGES_CAP = 30;8990/**91 * What last changed each of a folder's entries at a commit, by path: one92 * `log` of one commit per entry (the platform keeps a path's history once93 * walked), three at a time, read after the page and only in the browser.94 * An entry whose read fails is left out; a folder of more than95 * `LAST_CHANGES_CAP` entries reads none, to spare the platform the walks.96 */97export const lastChangesAtom = Atom.family(98 (key: {99 readonly at: string;100 readonly paths: readonly RepoPath[];101 readonly project: ProjectName;102 }) =>103 Atom.withServerValueInitial(104 ContractsClient.runtime.atom(105 Effect.gen(function* () {106 const client = yield* ContractsClient;107108 if (key.paths.length > LAST_CHANGES_CAP) {109 return new Map<string, CommitView>();110 }111112 const found = yield* Effect.forEach(113 key.paths,114 (path) =>115 client("log", {116 at: key.at,117 limit: 1,118 path,119 project: key.project,120 }).pipe(121 Effect.map(({ commits }) => Option.fromUndefinedOr(commits[0])),122 Effect.orElseSucceed(() => Option.none<CommitView>()),123 Effect.map((commit) =>124 Option.map(commit, (last) => [path, last] as const)125 )126 ),127 { concurrency: 3 }128 );129130 return new Map<string, CommitView>(Array.getSomes(found));131 })132 )133 )Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 134why, one step back134);