Skip to content
Myr

commit-view.tsx

This file

Path

apps/web/src/features/code/commit-view.tsx

341 lines · 10.5 KiB · TypeScript

Last changed by

Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726

History of this file

Take it

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 1–18why, one step back
1/**
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 back
19import { Match, Option, Schema } from "effect";

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 20–29why, one step back
20import type { DateTime } from "effect";
21import type { ReactNode } from "react";2223import { answerPath } from "../ask/ask-page.tsx";24import { askPath } from "../project/asks.tsx";25import { ProjectNav } from "../project/project-nav.tsx";26import { HandleLink } from "../shared/handle.tsx";27import { PageShell } from "../shared/layout.tsx";28import { OutcomeView } from "../shared/outcome.tsx";29import { 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 back
30import { askOf } from "../why/rows.ts";

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 31–179why, one step back
31import { blobHref, commitHref, shortSha, treeHref } from "./paths.ts";
3233const LINK = "decoration-rule underline hover:decoration-current";3435const ID = "font-mono text-[0.8125rem]";3637const LABEL = "text-muted-foreground text-xs font-medium";3839const isRole = Schema.is(CreditRole);4041const isHandle = Schema.is(Handle);4243/** Each change as its mark says it. */44const statusWords = Match.type<ChangedFile["status"]>().pipe(45 Match.when("added", () => ({ mark: "A", words: "added" })),46 Match.when("modified", () => ({ mark: "M", words: "modified" })),47 Match.when("deleted", () => ({ mark: "D", words: "deleted" })),48 Match.exhaustive49);5051/** The body without the trailer lines shown apart below it. */52const bodyWithoutTrailers = (commit: Commit) => {53 const trailers = new Set(54 commit.trailers.map(({ key, value }) => `${key}: ${value}`)55 );5657 return commit.body58 .split("\n")59 .filter((line) => !trailers.has(line.trim()))60 .join("\n")61 .trim();62};6364const Person = ({65 label,66 now,67 signature,68}: {69 readonly label: string;70 readonly now: DateTime.Utc;71 readonly signature: Signature;72}) => (73 <div className="flex min-w-0 flex-col">74 <dt className={LABEL}>{label}</dt>75 <dd className="[overflow-wrap:anywhere]">76 {signature.name}{" "}77 <span className="text-muted-foreground">78 <When at={signature.at} now={now} />79 </span>80 </dd>81 </div>82);8384/**85 * The person a credit trailer names on a landing: its value's handle,86 * before any ` <email>` (`Asked-by: ines-v <ines-v@users.…>`). Only the87 * platform's own landings name its people; an imported commit's88 * `Signed-off-by:` names someone elsewhere, never a page here.89 */90const personIn = (landed: boolean, name: string, value: string) => {91 const handle = value.replace(/\s*<[^>]*>$/u, "");9293 return landed && isRole(name) && isHandle(handle) ? handle : undefined;94};9596/** A trailer: a landing's credit links the person; any other reads as written. */97const Trailer = ({98 landed,99 name,100 value,101}: {102 readonly landed: boolean;103 readonly name: string;104 readonly value: string;105}) => {106 const person = personIn(landed, name, value);107108 return (109 <li className="[overflow-wrap:anywhere]">110 <span className="text-muted-foreground">{name}:</span>{" "}111 {person === undefined ? value : <HandleLink handle={person} />}112 </li>113 );114};115116/** The changed files by path, each linking its new version (or its old one, deleted). */117export const ChangedList = ({118 commit,119 files,120 project,121}: {122 readonly commit: Commit;123 readonly files: readonly ChangedFile[];124 readonly project: ProjectName;125}) => (126 <ul className={`flex flex-col gap-1 ${ID}`}>127 {files.map((file) => {128 const { mark, words } = statusWords(file.status);129 const at = file.status === "deleted" ? commit.parents[0] : commit.sha;130131 return (132 <li133 className="flex min-w-0 items-baseline gap-2"134 data-file={file.path}135 key={file.path}136 >137 <span138 aria-label={words}139 className="text-muted-foreground w-3 shrink-0"140 title={words}141 >142 {mark}143 </span>144 {at === undefined ? (145 <span className="[overflow-wrap:anywhere]">{file.path}</span>146 ) : (147 <a148 className={`${LINK} [overflow-wrap:anywhere]`}149 href={blobHref(project, at, file.path)}150 >151 {file.path}152 </a>153 )}154 </li>155 );156 })}157 </ul>158);159160export const CommitView = ({161 diff,162 drawing,163 now,164 project,165 split = false,166}: {167 readonly diff: CommitDiff;168 /**169 * The browser's file tree and diffs, holding `ChangedList` as their170 * fallback; the list alone when absent.171 */172 readonly drawing?: ReactNode;173 readonly now: DateTime.Utc;174 readonly project: ProjectName;175 /** `?style=split`: old and new side by side. */176 readonly split?: boolean;177}) => {178 const { commit, files, truncated } = diff;179 const { landing } = commit;

Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726

lines 180–183why, one step back
180
181 const linked =182 landing === undefined ? undefined : Option.getOrUndefined(askOf(landing));183

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 184–257why, one step back
184 const body = bodyWithoutTrailers(commit);
185 const here = commitHref(project, commit.sha);186187 return (188 <PageShell189 title={190 commit.subject === ""191 ? `Commit ${shortSha(commit.sha)}`192 : commit.subject193 }194 >195 <ProjectNav current="landings" project={project} />196 <section aria-label="Commit" className="flex flex-col gap-4">197 <p className={`${ID} text-muted-foreground [overflow-wrap:anywhere]`}>198 commit {commit.sha} ·{" "}199 <a className={LINK} href={treeHref(project, commit.sha)}>200 Browse files201 </a>202 </p>203 {body === "" ? null : (204 <pre className="max-w-[80ch] font-mono text-[0.8125rem] leading-6 [overflow-wrap:anywhere] whitespace-pre-wrap">205 {body}206 </pre>207 )}208 <dl className="grid gap-x-8 gap-y-2 text-sm sm:grid-cols-3">209 <Person label="Author" now={now} signature={commit.author} />210 <Person label="Committer" now={now} signature={commit.committer} />211 <div className="flex min-w-0 flex-col">212 <dt className={LABEL}>213 {commit.parents.length === 1 ? "Parent" : "Parents"}214 </dt>215 <dd className={`flex flex-wrap gap-2 ${ID}`}>216 {commit.parents.length === 0217 ? "none: the first commit"218 : commit.parents.map((parent) => (219 <a220 className={LINK}221 href={commitHref(project, parent)}222 key={parent}223 >224 {shortSha(parent)}225 </a>226 ))}227 </dd>228 </div>229 </dl>230 {commit.trailers.length === 0 ? null : (231 <section aria-label="Trailers" className="flex flex-col gap-1.5">232 <h2 className={LABEL}>Trailers</h2>233 <ul className={`flex flex-col gap-1 ${ID}`}>234 {commit.trailers.map(({ key, value }) => (235 <Trailer236 key={`${key}:${value}`}237 landed={landing !== undefined}238 name={key}239 value={value}240 />241 ))}242 </ul>243 </section>244 )}245 </section>246 {landing === undefined ? (247 <p className="text-muted-foreground text-sm">248 The platform did not land this commit: it came in with the history the249 project adopted (before adoption).250 </p>251 ) : (252 <section253 aria-label="Landing"254 className="border-rule flex flex-col gap-3 border-t pt-4"255 >256 <h2 className="text-lg font-semibold">Landing</h2>257 <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 258why, one step back
258 {linked === undefined ? null : (

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 259–260why, one step back
259 <>
260 <div>

Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726

lines 261–263why, one step back
261 <dt className={LABEL}>
262 {linked.kind === "opened" ? "Opened" : "Ask"}263 </dt>

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 264–266why, one step back
264 <dd>
265 <a266 className={`${ID} ${LINK}`}

Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726

line 267why, one step back
267 href={askPath(project, linked.ask)}

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

line 268why, one step back
268 >

Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726

line 269why, one step back
269 {linked.ask}

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 270–272why, one step back
270 </a>
271 </dd>272 </div>

Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726

lines 273–285why, one step back
273 {linked.kind === "opened" ? null : (
274 <div>275 <dt className={LABEL}>Answer</dt>276 <dd>277 <a278 className={LINK}279 href={answerPath(project, linked.ask)}280 >281 The answer and its checks282 </a>283 </dd>284 </div>285 )}

Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229

lines 286–341why, one step back
286 </>
287 )}288 <div>289 <dt className={LABEL}>Author</dt>290 <dd>291 <HandleLink handle={landing.author} />292 </dd>293 </div>294 <div>295 <dt className={LABEL}>Landed</dt>296 <dd>297 <When at={landing.landedAt} now={now} />298 </dd>299 </div>300 </dl>301 {landing.verdict === undefined ? null : (302 <OutcomeView outcome={landing.verdict.outcome} />303 )}304 </section>305 )}306 <section307 aria-label="Changes"308 className="flex min-w-0 flex-col gap-3"309 data-files=""310 >311 <div className="flex flex-wrap items-baseline justify-between gap-2">312 <h2 className="text-lg font-semibold">313 {files.length} {files.length === 1 ? "file" : "files"} changed314 </h2>315 <p className="text-sm">316 {split ? (317 <a className={LINK} href={here}>318 Unified319 </a>320 ) : (321 <a className={LINK} href={`${here}?style=split`}>322 Split323 </a>324 )}325 </p>326 </div>327 {truncated ? (328 <p className="border-rule border border-dashed px-4 py-3 text-sm">329 This commit is too large to show whole: its files are listed by330 path, without their changes.331 </p>332 ) : null}333 {drawing === undefined || truncated ? (334 <ChangedList commit={commit} files={files} project={project} />335 ) : (336 drawing337 )}338 </section>339 </PageShell>340 );341};