commit-view.tsx
This file
Path
apps/web/src/features/code/commit-view.tsx
335 lines · 10.4 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
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 1–18why, one step back1/**2 * One commit: its message, who wrote and committed it, its parents, the3 * people its trailers name (each a link to their page), and, when the4 * platform landed it, the landing's ask, answer and verdict. Below, the5 * files it changed against its first parent: a plain list from the6 * server, which the browser replaces with the file tree and the diffs7 * (`drawing`, from `client-code.tsx`). Unified by default; `?style=split`8 * sets the sides apart.9 */10import type {11 ChangedFile,12 CommitDiff,13 CommitView as Commit,14 Signature,15} from "@repo/protocol/domain/code";16import { CreditRole } from "@repo/protocol/domain/credit";17import { Handle } from "@repo/protocol/ids";18import type { ProjectName } from "@repo/protocol/ids";Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 19why, one step back19import { Match, Option, Schema } from "effect";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 20–23why, one step back20import type { DateTime } from "effect";21import type { ReactNode } from "react";2223import { ProjectNav } from "../project/project-nav.tsx";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 24why, one step back24import { ID, LABEL, LINK } from "../shared/classes.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 25–27why, one step back25import { HandleLink } from "../shared/handle.tsx";26import { PageShell } from "../shared/layout.tsx";27import { OutcomeView } from "../shared/outcome.tsx";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 28why, one step back28import { answerPath, askPath } from "../shared/paths.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 29why, one step back29import { When } from "../shared/time.tsx";Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 30why, one step back30import { askOf } from "../why/rows.ts";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 31–173why, one step back31import { blobHref, commitHref, shortSha, treeHref } from "./paths.ts";3233const isRole = Schema.is(CreditRole);3435const isHandle = Schema.is(Handle);3637/** Each change as its mark says it. */38const statusWords = Match.type<ChangedFile["status"]>().pipe(39 Match.when("added", () => ({ mark: "A", words: "added" })),40 Match.when("modified", () => ({ mark: "M", words: "modified" })),41 Match.when("deleted", () => ({ mark: "D", words: "deleted" })),42 Match.exhaustive43);4445/** The body without the trailer lines shown apart below it. */46const bodyWithoutTrailers = (commit: Commit) => {47 const trailers = new Set(48 commit.trailers.map(({ key, value }) => `${key}: ${value}`)49 );5051 return commit.body52 .split("\n")53 .filter((line) => !trailers.has(line.trim()))54 .join("\n")55 .trim();56};5758const Person = ({59 label,60 now,61 signature,62}: {63 readonly label: string;64 readonly now: DateTime.Utc;65 readonly signature: Signature;66}) => (67 <div className="flex min-w-0 flex-col">68 <dt className={LABEL}>{label}</dt>69 <dd className="[overflow-wrap:anywhere]">70 {signature.name}{" "}71 <span className="text-muted-foreground">72 <When at={signature.at} now={now} />73 </span>74 </dd>75 </div>76);7778/**79 * The person a credit trailer names on a landing: its value's handle,80 * before any ` <email>` (`Asked-by: ines-v <ines-v@users.…>`). Only the81 * platform's own landings name its people; an imported commit's82 * `Signed-off-by:` names someone elsewhere, never a page here.83 */84const personIn = (landed: boolean, name: string, value: string) => {85 const handle = value.replace(/\s*<[^>]*>$/u, "");8687 return landed && isRole(name) && isHandle(handle) ? handle : undefined;88};8990/** A trailer: a landing's credit links the person; any other reads as written. */91const Trailer = ({92 landed,93 name,94 value,95}: {96 readonly landed: boolean;97 readonly name: string;98 readonly value: string;99}) => {100 const person = personIn(landed, name, value);101102 return (103 <li className="[overflow-wrap:anywhere]">104 <span className="text-muted-foreground">{name}:</span>{" "}105 {person === undefined ? value : <HandleLink handle={person} />}106 </li>107 );108};109110/** The changed files by path, each linking its new version (or its old one, deleted). */111export const ChangedList = ({112 commit,113 files,114 project,115}: {116 readonly commit: Commit;117 readonly files: readonly ChangedFile[];118 readonly project: ProjectName;119}) => (120 <ul className={`flex flex-col gap-1 ${ID}`}>121 {files.map((file) => {122 const { mark, words } = statusWords(file.status);123 const at = file.status === "deleted" ? commit.parents[0] : commit.sha;124125 return (126 <li127 className="flex min-w-0 items-baseline gap-2"128 data-file={file.path}129 key={file.path}130 >131 <span132 aria-label={words}133 className="text-muted-foreground w-3 shrink-0"134 title={words}135 >136 {mark}137 </span>138 {at === undefined ? (139 <span className="[overflow-wrap:anywhere]">{file.path}</span>140 ) : (141 <a142 className={`${LINK} [overflow-wrap:anywhere]`}143 href={blobHref(project, at, file.path)}144 >145 {file.path}146 </a>147 )}148 </li>149 );150 })}151 </ul>152);153154export const CommitView = ({155 diff,156 drawing,157 now,158 project,159 split = false,160}: {161 readonly diff: CommitDiff;162 /**163 * The browser's file tree and diffs, holding `ChangedList` as their164 * fallback; the list alone when absent.165 */166 readonly drawing?: ReactNode;167 readonly now: DateTime.Utc;168 readonly project: ProjectName;169 /** `?style=split`: old and new side by side. */170 readonly split?: boolean;171}) => {172 const { commit, files, truncated } = diff;173 const { landing } = commit;Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
lines 174–177why, one step back174175 const linked =176 landing === undefined ? undefined : Option.getOrUndefined(askOf(landing));177Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 178–251why, one step back178 const body = bodyWithoutTrailers(commit);179 const here = commitHref(project, commit.sha);180181 return (182 <PageShell183 title={184 commit.subject === ""185 ? `Commit ${shortSha(commit.sha)}`186 : commit.subject187 }188 >189 <ProjectNav current="landings" project={project} />190 <section aria-label="Commit" className="flex flex-col gap-4">191 <p className={`${ID} text-muted-foreground [overflow-wrap:anywhere]`}>192 commit {commit.sha} ·{" "}193 <a className={LINK} href={treeHref(project, commit.sha)}>194 Browse files195 </a>196 </p>197 {body === "" ? null : (198 <pre className="max-w-[80ch] font-mono text-[0.8125rem] leading-6 [overflow-wrap:anywhere] whitespace-pre-wrap">199 {body}200 </pre>201 )}202 <dl className="grid gap-x-8 gap-y-2 text-sm sm:grid-cols-3">203 <Person label="Author" now={now} signature={commit.author} />204 <Person label="Committer" now={now} signature={commit.committer} />205 <div className="flex min-w-0 flex-col">206 <dt className={LABEL}>207 {commit.parents.length === 1 ? "Parent" : "Parents"}208 </dt>209 <dd className={`flex flex-wrap gap-2 ${ID}`}>210 {commit.parents.length === 0211 ? "none: the first commit"212 : commit.parents.map((parent) => (213 <a214 className={LINK}215 href={commitHref(project, parent)}216 key={parent}217 >218 {shortSha(parent)}219 </a>220 ))}221 </dd>222 </div>223 </dl>224 {commit.trailers.length === 0 ? null : (225 <section aria-label="Trailers" className="flex flex-col gap-1.5">226 <h2 className={LABEL}>Trailers</h2>227 <ul className={`flex flex-col gap-1 ${ID}`}>228 {commit.trailers.map(({ key, value }) => (229 <Trailer230 key={`${key}:${value}`}231 landed={landing !== undefined}232 name={key}233 value={value}234 />235 ))}236 </ul>237 </section>238 )}239 </section>240 {landing === undefined ? (241 <p className="text-muted-foreground text-sm">242 The platform did not land this commit: it came in with the history the243 project adopted (before adoption).244 </p>245 ) : (246 <section247 aria-label="Landing"248 className="border-rule flex flex-col gap-3 border-t pt-4"249 >250 <h2 className="text-lg font-semibold">Landing</h2>251 <dl className="flex flex-wrap gap-x-8 gap-y-2 text-sm">Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 252why, one step back252 {linked === undefined ? null : (Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 253–254why, one step back253 <>254 <div>Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
lines 255–257why, one step back255 <dt className={LABEL}>256 {linked.kind === "opened" ? "Opened" : "Ask"}257 </dt>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 258–260why, one step back258 <dd>259 <a260 className={`${ID} ${LINK}`}Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 261why, one step back261 href={askPath(project, linked.ask)}Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 262why, one step back262 >Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 263why, one step back263 {linked.ask}Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 264–266why, one step back264 </a>265 </dd>266 </div>Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
lines 267–279why, one step back267 {linked.kind === "opened" ? null : (268 <div>269 <dt className={LABEL}>Answer</dt>270 <dd>271 <a272 className={LINK}273 href={answerPath(project, linked.ask)}274 >275 The answer and its checks276 </a>277 </dd>278 </div>279 )}Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 280–335why, one step back280 </>281 )}282 <div>283 <dt className={LABEL}>Author</dt>284 <dd>285 <HandleLink handle={landing.author} />286 </dd>287 </div>288 <div>289 <dt className={LABEL}>Landed</dt>290 <dd>291 <When at={landing.landedAt} now={now} />292 </dd>293 </div>294 </dl>295 {landing.verdict === undefined ? null : (296 <OutcomeView outcome={landing.verdict.outcome} />297 )}298 </section>299 )}300 <section301 aria-label="Changes"302 className="flex min-w-0 flex-col gap-3"303 data-files=""304 >305 <div className="flex flex-wrap items-baseline justify-between gap-2">306 <h2 className="text-lg font-semibold">307 {files.length} {files.length === 1 ? "file" : "files"} changed308 </h2>309 <p className="text-sm">310 {split ? (311 <a className={LINK} href={here}>312 Unified313 </a>314 ) : (315 <a className={LINK} href={`${here}?style=split`}>316 Split317 </a>318 )}319 </p>320 </div>321 {truncated ? (322 <p className="border-rule border border-dashed px-4 py-3 text-sm">323 This commit is too large to show whole: its files are listed by324 path, without their changes.325 </p>326 ) : null}327 {drawing === undefined || truncated ? (328 <ChangedList commit={commit} files={files} project={project} />329 ) : (330 drawing331 )}332 </section>333 </PageShell>334 );335};