folder.ts
This file
Path
apps/web/src/server/folder.ts
75 lines · 2.1 KiB · TypeScript
Last changed by
Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
History of this fileTake it
Merge build/1005oscabrielbefore adoptionb124cbb
lines 1–2why, one step back1import { Client } from "@repo/protocol/contracts";2import type { FileView, Rev, TreeListing } from "@repo/protocol/domain/code";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 3–4why, one step back3import type { ProjectName } from "@repo/protocol/ids";4import { RepoPath } from "@repo/protocol/ids";Merge build/1005oscabrielbefore adoptionb124cbb
lines 5–13why, one step back5import type { Unauthenticated } from "@repo/protocol/principal";6import type {7 UnknownPath,8 UnknownProject,9 UnknownRevision,10} from "@repo/protocol/refusal";11import { Effect, Option } from "effect";12import type { RpcClientError } from "effect/rpc";13Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 14–15why, one step back14const GUESS = "README.md";15Merge build/comments-web-rest into mainoscabrielbefore adoption0eb1248
lines 16–17why, one step back16// The folder and a `README.md` in it are read at once. The guess stands only when it is the README the folder17// names at the folder's commit (`main` may move between the two reads).Merge build/1005oscabrielbefore adoptionb124cbb
lines 18–34why, one step back18export const readFolderWithReadme = (input: {19 readonly path: Option.Option<RepoPath>;20 readonly project: ProjectName;21 readonly rev: Rev;22}): Effect.Effect<23 { readonly listing: TreeListing; readonly readme: Option.Option<FileView> },24 | UnknownProject25 | UnknownRevision26 | UnknownPath27 | RpcClientError.RpcClientError28 | Unauthenticated,29 Client30> =>31 Effect.gen(function* () {32 const { path, project, rev } = input;33 const client = yield* Client;34Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 35–40why, one step back35 const guessed = RepoPath.make(36 Option.match(path, {37 onNone: () => GUESS,38 onSome: (folder) => `${folder}/${GUESS}`,39 })40 );Merge build/1005oscabrielbefore adoptionb124cbb
line 41why, one step back41Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 42–64why, one step back42 const [listing, guess] = yield* Effect.all(43 [44 client.readTree({45 at: rev,46 project,47 ...Option.match(path, {48 onNone: () => ({}),49 onSome: (at) => ({ path: at }),50 }),51 }),52 client53 .readFile({ at: rev, path: guessed, project })54 .pipe(Effect.option),55 ],56 { concurrency: 2 }57 );5859 const named = listing.readme;6061 if (named === undefined) {62 return { listing, readme: Option.none() };63 }64Merge build/1005oscabrielbefore adoptionb124cbb
line 65why, one step back65 const readme =Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
lines 66–69why, one step back66 Option.isSome(guess) &&67 guess.value.path === named.path &&68 guess.value.at === listing.at69 ? guessMerge build/1005oscabrielbefore adoptionb124cbb
line 70why, one step back70 : yield* clientFold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 71why, one step back71 .readFile({ at: listing.at, path: named.path, project })Merge build/1005oscabrielbefore adoptionb124cbb
lines 72–75why, one step back72 .pipe(Effect.option);7374 return { listing, readme };75 });