Skip to content
Myr

commit-view.tsx

This file

Path

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

329 lines · 10.2 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

lines 1–329why, 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";19import { Match, Schema } from "effect";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";30import { blobHref, commitHref, shortSha, treeHref } from "./paths.ts";3132const LINK = "decoration-rule underline hover:decoration-current";3334const ID = "font-mono text-[0.8125rem]";3536const LABEL = "text-muted-foreground text-xs font-medium";3738const isRole = Schema.is(CreditRole);3940const isHandle = Schema.is(Handle);4142/** Each change as its mark says it. */43const statusWords = Match.type<ChangedFile["status"]>().pipe(44 Match.when("added", () => ({ mark: "A", words: "added" })),45 Match.when("modified", () => ({ mark: "M", words: "modified" })),46 Match.when("deleted", () => ({ mark: "D", words: "deleted" })),47 Match.exhaustive48);4950/** The body without the trailer lines shown apart below it. */51const bodyWithoutTrailers = (commit: Commit) => {52 const trailers = new Set(53 commit.trailers.map(({ key, value }) => `${key}: ${value}`)54 );5556 return commit.body57 .split("\n")58 .filter((line) => !trailers.has(line.trim()))59 .join("\n")60 .trim();61};6263const Person = ({64 label,65 now,66 signature,67}: {68 readonly label: string;69 readonly now: DateTime.Utc;70 readonly signature: Signature;71}) => (72 <div className="flex min-w-0 flex-col">73 <dt className={LABEL}>{label}</dt>74 <dd className="[overflow-wrap:anywhere]">75 {signature.name}{" "}76 <span className="text-muted-foreground">77 <When at={signature.at} now={now} />78 </span>79 </dd>80 </div>81);8283/**84 * The person a credit trailer names on a landing: its value's handle,85 * before any ` <email>` (`Asked-by: ines-v <ines-v@users.…>`). Only the86 * platform's own landings name its people; an imported commit's87 * `Signed-off-by:` names someone elsewhere, never a page here.88 */89const personIn = (landed: boolean, name: string, value: string) => {90 const handle = value.replace(/\s*<[^>]*>$/u, "");9192 return landed && isRole(name) && isHandle(handle) ? handle : undefined;93};9495/** A trailer: a landing's credit links the person; any other reads as written. */96const Trailer = ({97 landed,98 name,99 value,100}: {101 readonly landed: boolean;102 readonly name: string;103 readonly value: string;104}) => {105 const person = personIn(landed, name, value);106107 return (108 <li className="[overflow-wrap:anywhere]">109 <span className="text-muted-foreground">{name}:</span>{" "}110 {person === undefined ? value : <HandleLink handle={person} />}111 </li>112 );113};114115/** The changed files by path, each linking its new version (or its old one, deleted). */116export const ChangedList = ({117 commit,118 files,119 project,120}: {121 readonly commit: Commit;122 readonly files: readonly ChangedFile[];123 readonly project: ProjectName;124}) => (125 <ul className={`flex flex-col gap-1 ${ID}`}>126 {files.map((file) => {127 const { mark, words } = statusWords(file.status);128 const at = file.status === "deleted" ? commit.parents[0] : commit.sha;129130 return (131 <li132 className="flex min-w-0 items-baseline gap-2"133 data-file={file.path}134 key={file.path}135 >136 <span137 aria-label={words}138 className="text-muted-foreground w-3 shrink-0"139 title={words}140 >141 {mark}142 </span>143 {at === undefined ? (144 <span className="[overflow-wrap:anywhere]">{file.path}</span>145 ) : (146 <a147 className={`${LINK} [overflow-wrap:anywhere]`}148 href={blobHref(project, at, file.path)}149 >150 {file.path}151 </a>152 )}153 </li>154 );155 })}156 </ul>157);158159export const CommitView = ({160 diff,161 drawing,162 now,163 project,164 split = false,165}: {166 readonly diff: CommitDiff;167 /**168 * The browser's file tree and diffs, holding `ChangedList` as their169 * fallback; the list alone when absent.170 */171 readonly drawing?: ReactNode;172 readonly now: DateTime.Utc;173 readonly project: ProjectName;174 /** `?style=split`: old and new side by side. */175 readonly split?: boolean;176}) => {177 const { commit, files, truncated } = diff;178 const { landing } = commit;179 const body = bodyWithoutTrailers(commit);180 const here = commitHref(project, commit.sha);181182 return (183 <PageShell184 title={185 commit.subject === ""186 ? `Commit ${shortSha(commit.sha)}`187 : commit.subject188 }189 >190 <ProjectNav current="landings" project={project} />191 <section aria-label="Commit" className="flex flex-col gap-4">192 <p className={`${ID} text-muted-foreground [overflow-wrap:anywhere]`}>193 commit {commit.sha} ·{" "}194 <a className={LINK} href={treeHref(project, commit.sha)}>195 Browse files196 </a>197 </p>198 {body === "" ? null : (199 <pre className="max-w-[80ch] font-mono text-[0.8125rem] leading-6 [overflow-wrap:anywhere] whitespace-pre-wrap">200 {body}201 </pre>202 )}203 <dl className="grid gap-x-8 gap-y-2 text-sm sm:grid-cols-3">204 <Person label="Author" now={now} signature={commit.author} />205 <Person label="Committer" now={now} signature={commit.committer} />206 <div className="flex min-w-0 flex-col">207 <dt className={LABEL}>208 {commit.parents.length === 1 ? "Parent" : "Parents"}209 </dt>210 <dd className={`flex flex-wrap gap-2 ${ID}`}>211 {commit.parents.length === 0212 ? "none: the first commit"213 : commit.parents.map((parent) => (214 <a215 className={LINK}216 href={commitHref(project, parent)}217 key={parent}218 >219 {shortSha(parent)}220 </a>221 ))}222 </dd>223 </div>224 </dl>225 {commit.trailers.length === 0 ? null : (226 <section aria-label="Trailers" className="flex flex-col gap-1.5">227 <h2 className={LABEL}>Trailers</h2>228 <ul className={`flex flex-col gap-1 ${ID}`}>229 {commit.trailers.map(({ key, value }) => (230 <Trailer231 key={`${key}:${value}`}232 landed={landing !== undefined}233 name={key}234 value={value}235 />236 ))}237 </ul>238 </section>239 )}240 </section>241 {landing === undefined ? (242 <p className="text-muted-foreground text-sm">243 The platform did not land this commit: it came in with the history the244 project adopted (before adoption).245 </p>246 ) : (247 <section248 aria-label="Landing"249 className="border-rule flex flex-col gap-3 border-t pt-4"250 >251 <h2 className="text-lg font-semibold">Landing</h2>252 <dl className="flex flex-wrap gap-x-8 gap-y-2 text-sm">253 {landing.ask === undefined ? null : (254 <>255 <div>256 <dt className={LABEL}>Ask</dt>257 <dd>258 <a259 className={`${ID} ${LINK}`}260 href={askPath(project, landing.ask)}261 >262 {landing.ask}263 </a>264 </dd>265 </div>266 <div>267 <dt className={LABEL}>Answer</dt>268 <dd>269 <a className={LINK} href={answerPath(project, landing.ask)}>270 The answer and its checks271 </a>272 </dd>273 </div>274 </>275 )}276 <div>277 <dt className={LABEL}>Author</dt>278 <dd>279 <HandleLink handle={landing.author} />280 </dd>281 </div>282 <div>283 <dt className={LABEL}>Landed</dt>284 <dd>285 <When at={landing.landedAt} now={now} />286 </dd>287 </div>288 </dl>289 {landing.verdict === undefined ? null : (290 <OutcomeView outcome={landing.verdict.outcome} />291 )}292 </section>293 )}294 <section295 aria-label="Changes"296 className="flex min-w-0 flex-col gap-3"297 data-files=""298 >299 <div className="flex flex-wrap items-baseline justify-between gap-2">300 <h2 className="text-lg font-semibold">301 {files.length} {files.length === 1 ? "file" : "files"} changed302 </h2>303 <p className="text-sm">304 {split ? (305 <a className={LINK} href={here}>306 Unified307 </a>308 ) : (309 <a className={LINK} href={`${here}?style=split`}>310 Split311 </a>312 )}313 </p>314 </div>315 {truncated ? (316 <p className="border-rule border border-dashed px-4 py-3 text-sm">317 This commit is too large to show whole: its files are listed by318 path, without their changes.319 </p>320 ) : null}321 {drawing === undefined || truncated ? (322 <ChangedList commit={commit} files={files} project={project} />323 ) : (324 drawing325 )}326 </section>327 </PageShell>328 );329};