commit-view.tsx
This file
Path
apps/web/src/features/code/commit-view.tsx
449 lines · 13.4 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 1–5why, one step back1import type {2 CommitDiff,3 CommitView as Commit,4} from "@repo/protocol/domain/code";5import type { ProjectName } from "@repo/protocol/ids";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 6why, one step back6import { Option } from "effect";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 7why, one step back7import type { DateTime } from "effect";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 8–9why, one step back8import { Columns2, FoldVertical, Rows3, UnfoldVertical } from "lucide-react";9import { useMemo } from "react";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 10–11why, one step back10import type { ReactNode } from "react";11Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 12–15why, one step back12import { Button } from "@/components/ui/button";1314import { CreditFacts, FactBlock, ProvenanceFacts } from "../shared/facts.tsx";15import type { ProvenanceEntry } from "../shared/facts.tsx";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 16why, one step back16import { HandleLink } from "../shared/handle.tsx";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 17–18why, one step back17import { AppLink } from "../shared/link.tsx";18import { StateMarkIcon } from "../shared/marks.tsx";Fold every page's project events once, keep Markdown off the first load, and cache what never changesoscabrielbefore adoption38e5215
line 19why, one step back19import { answerPath, askPath } from "../shared/paths.ts";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 20why, one step back20import { GUTTER, MEASURE, Workspace } from "../shared/shell/workspace.tsx";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 21why, one step back21import { When } from "../shared/time.tsx";Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 22why, one step back22import { askOf } from "../why/rows.ts";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 23–27why, one step back23import { drawnFiles, statsOf } from "./diff-model.ts";24import type { DiffStats, DrawnFile } from "./diff-model.ts";25import { DiffPane, PANE_SCROLLER } from "./diff-pane.tsx";26import { commitHref, shortSha, treeHref } from "./paths.ts";27import { ChangeCounts, PlainDiff } from "./plain-diff.tsx";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 28why, one step back28Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 29why, one step back29const LINK = "decoration-rule underline hover:decoration-current";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 30why, one step back30Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 31why, one step back31const TRAILER = /^[A-Za-z][\w-]*:\s/u;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 32why, one step back32Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 33why, one step back33/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 34–35why, one step back34 * Git's trailer block is the last paragraph when every line of it is a `Key:35 * value`.Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 36–39why, one step back36 */37export const bodyWithoutTrailers = (commit: Commit) => {38 const paragraphs = commit.body.trim().split(/\n\s*\n/u);39 const last = paragraphs.at(-1) ?? "";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 40why, one step back40Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 41–43why, one step back41 const trailers =42 commit.trailers.length > 0 &&43 last.split("\n").every((line) => TRAILER.test(line.trim()));Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 44why, one step back44Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 45why, one step back45 return (trailers ? paragraphs.slice(0, -1) : paragraphs).join("\n\n").trim();Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 46–47why, one step back46};47Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 48–102why, one step back48export const LandingStatus = ({49 commit,50 project,51}: {52 readonly commit: Commit;53 readonly project: ProjectName;54}) => {55 const { landing } = commit;5657 if (landing === undefined) {58 return (59 <p className="text-want text-muted-foreground flex items-start gap-2">60 <StateMarkIcon className="mt-1" mark="notNeeded" />61 <span>62 Before adoption: this commit came in with the history the project63 adopted, so no landing records it.64 </span>65 </p>66 );67 }6869 const linked = Option.getOrUndefined(askOf(landing));7071 return (72 <p className="text-want flex items-start gap-2" data-outcome="Landed">73 <StateMarkIcon className="mt-1" mark="landed" />74 <span className="min-w-0 [overflow-wrap:anywhere]">75 <strong className="text-landed font-semibold">Landed</strong> as{" "}76 <AppLink77 className={`text-mono font-mono ${LINK}`}78 to={commitHref(project, commit.sha)}79 >80 {shortSha(commit.sha)}81 </AppLink>82 {linked === undefined ? null : (83 <>84 <span aria-hidden="true" className="text-muted-foreground">85 {" · "}86 </span>87 {linked.kind === "opened" ? "opened " : "answered "}88 <AppLink89 className={`text-mono font-mono ${LINK}`}90 to={askPath(project, linked.ask)}91 >92 {linked.ask}93 </AppLink>94 </>95 )}96 </span>97 </p>98 );99};100101const GitLine = ({102 commit,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 103why, one step back103 now,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 104why, one step back104 project,Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
line 105why, one step back105 stats,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 106why, one step back106}: {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 107why, one step back107 readonly commit: Commit;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 108why, one step back108 readonly now: DateTime.Utc;Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 109why, one step back109 readonly project: ProjectName;Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
line 110why, one step back110 readonly stats: DiffStats;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 111why, one step back111}) => (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 112–139why, one step back112 <span className="[overflow-wrap:anywhere]">113 authored by {commit.author.name} <When at={commit.author.at} now={now} />114 {" · "}115 committed by{" "}116 {commit.landing === undefined ? commit.committer.name : "the platform"}117 {" · "}118 {commit.parents.length === 0 ? (119 "the first commit"120 ) : (121 <>122 {commit.parents.length === 1 ? "parent" : "parents"}{" "}123 {commit.parents.map((parent, index) => (124 <span key={parent}>125 {index === 0 ? null : ", "}126 <AppLink127 className={`text-mono font-mono ${LINK}`}128 to={commitHref(project, parent)}129 >130 {shortSha(parent)}131 </AppLink>132 </span>133 ))}134 </>135 )}136 {" · "}137 <AppLink className={LINK} to={treeHref(project, commit.sha)}>138 Browse files139 </AppLink>Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
lines 140–150why, one step back140 {" · "}141 <a142 className="inline-flex items-baseline gap-[1ch] no-underline hover:underline"143 href="#diff"144 >145 <span className="tabular-nums">146 {stats.files} {stats.files === 1 ? "file" : "files"}147 </span>148 <span aria-hidden="true">·</span>149 <ChangeCounts additions={stats.additions} deletions={stats.deletions} />150 </a>Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 151–167why, one step back151 </span>152);153154const PART_WORDS = {155 other: "other",156 territory: "territory",157 tests: "tests",158} as const;159160export const DiffStatsFacts = ({ stats }: { readonly stats: DiffStats }) => (161 <FactBlock label="Diff stats">162 <p163 className="text-ui flex flex-wrap items-center gap-x-3 gap-y-1"164 data-stats=""165 >166 <span>167 {stats.files} {stats.files === 1 ? "file" : "files"}Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 168why, one step back168 </span>Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 169–191why, one step back169 <ChangeCounts additions={stats.additions} deletions={stats.deletions} />170 </p>171 {stats.parts.length === 0 ? null : (172 <ul className="text-ui flex flex-col gap-1">173 {stats.parts.map((part) => (174 <li175 className="flex items-center justify-between gap-3"176 data-part={part.label}177 key={part.label}178 >179 <span className="text-muted-foreground">180 {PART_WORDS[part.label]}181 <span className="sr-only">:</span>182 </span>183 <ChangeCounts184 additions={part.additions}185 deletions={part.deletions}186 />187 </li>188 ))}189 </ul>190 )}191 </FactBlock>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 192–193why, one step back192);193Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 194–205why, one step back194const WrittenTrailers = ({ commit }: { readonly commit: Commit }) =>195 commit.trailers.length === 0 ? null : (196 <FactBlock label="Trailers">197 <ul className="text-mono leading-terminal flex flex-col gap-1 font-mono">198 {commit.trailers.map(({ key, value }) => (199 <li className="[overflow-wrap:anywhere]" key={`${key}:${value}`}>200 <span className="text-muted-foreground">{key}:</span> {value}201 </li>202 ))}203 </ul>204 </FactBlock>205 );Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 206why, one step back206Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 207–216why, one step back207const LandingCredit = ({ commit }: { readonly commit: Commit }) =>208 commit.landing === undefined ? (209 <WrittenTrailers commit={commit} />210 ) : (211 <CreditFacts212 assisted={commit.landing.assisted}213 credits={commit.landing.credits}214 heldBack={commit.landing.heldBackTrailer}215 />216 );Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 217why, one step back217Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 218–241why, one step back218const provenanceOf = (commit: Commit): readonly ProvenanceEntry[] => [219 { label: "Commit", value: commit.sha },220 { label: "Tree", value: commit.tree },221 { label: "Author", value: `${commit.author.name} <${commit.author.email}>` },222 {223 label: "Committer",224 value: `${commit.committer.name} <${commit.committer.email}>`,225 },226 ...commit.parents.map((parent, index) => ({227 label: commit.parents.length === 1 ? "Parent" : `Parent ${index + 1}`,228 value: parent,229 })),230 ...(commit.landing?.verdict === undefined231 ? []232 : [{ label: "Patch id", value: commit.landing.verdict.patchId }]),233 ...(commit.landing?.requestId === undefined234 ? []235 : [{ label: "Request-Id", value: commit.landing.requestId }]),236];237238const CommitFacts = ({239 commit,240 project,241 stats,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 242why, one step back242}: {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 243–245why, one step back243 readonly commit: Commit;244 readonly project: ProjectName;245 readonly stats: DiffStats;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 246why, one step back246}) => {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 247–250why, one step back247 const linked =248 commit.landing === undefined249 ? undefined250 : Option.getOrUndefined(askOf(commit.landing));Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 251–252why, one step back251252 return (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 253–273why, one step back253 <>254 <FactBlock label="State">255 <LandingStatus commit={commit} project={project} />256 {commit.landing === undefined ? null : (257 <p className="text-ui text-muted-foreground">258 By <HandleLink handle={commit.landing.author} />259 {linked === undefined || linked.kind === "opened" ? null : (260 <>261 {" · "}262 <AppLink className={LINK} to={answerPath(project, linked.ask)}>263 the answer and its checks264 </AppLink>265 </>266 )}267 </p>268 )}269 </FactBlock>270 <LandingCredit commit={commit} />271 <DiffStatsFacts stats={stats} />272 <ProvenanceFacts entries={provenanceOf(commit)} />273 </>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 274–276why, one step back274 );275};276Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 277–281why, one step back277const SEGMENT =278 "text-ui hover:bg-surface aria-pressed:bg-foreground aria-pressed:text-background aria-pressed:hover:bg-foreground flex h-7 cursor-pointer items-center gap-1.5 px-2.5 font-medium transition-colors duration-150 disabled:cursor-not-allowed disabled:opacity-50 [&_svg]:size-3.5";279280export const DiffToolbar = ({281 allFolded,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 282why, one step back282 files,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 283–286why, one step back283 onFoldAll,284 onSplit,285 split,286 stats,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 287why, one step back287}: {Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 288–293why, one step back288 readonly split: boolean;289 readonly onSplit?: ((split: boolean) => void) | undefined;290 readonly allFolded: boolean;291 readonly onFoldAll?: ((folded: boolean) => void) | undefined;292 readonly stats: DiffStats;293 readonly files?: ReactNode;Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 294why, one step back294}) => (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 295–337why, one step back295 <>296 {files === undefined ? null : (297 <div className="shell:hidden flex shrink-0">{files}</div>298 )}299 <fieldset className="border-input diffsplit:flex m-0 hidden min-w-0 border p-0">300 <legend className="sr-only">Diff layout</legend>301 <button302 aria-pressed={!split}303 className={SEGMENT}304 onClick={() => onSplit?.(false)}305 type="button"306 >307 <Rows3 aria-hidden="true" strokeWidth={1.75} />308 Unified309 </button>310 <button311 aria-pressed={split}312 className={SEGMENT}313 onClick={() => onSplit?.(true)}314 type="button"315 >316 <Columns2 aria-hidden="true" strokeWidth={1.75} />317 Split318 </button>319 </fieldset>320 <span className="text-muted-foreground">321 <Button onClick={() => onFoldAll?.(!allFolded)} size="sm" variant="ghost">322 {allFolded ? (323 <UnfoldVertical aria-hidden="true" strokeWidth={1.75} />324 ) : (325 <FoldVertical aria-hidden="true" strokeWidth={1.75} />326 )}327 {allFolded ? "Unfold all" : "Collapse all"}328 </Button>329 </span>330 <p className="text-ui text-muted-foreground ml-auto flex items-center gap-3 tabular-nums">331 <span>332 {stats.files} {stats.files === 1 ? "file" : "files"}333 <span className="tablet:inline hidden"> changed</span>334 </span>335 <ChangeCounts additions={stats.additions} deletions={stats.deletions} />336 </p>337 </>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 338–339why, one step back338);339Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 340–354why, one step back340export interface CommitViewProps {341 readonly diff: CommitDiff;342 readonly now: DateTime.Utc;343 readonly project: ProjectName;344 readonly territory?: readonly string[] | undefined;345 readonly split?: boolean;346 readonly onSplit?: ((split: boolean) => void) | undefined;347 readonly folded?: ReadonlySet<string> | undefined;348 readonly onFold?: ((path: string, folded: boolean) => void) | undefined;349 readonly onFoldAll?: ((folded: boolean) => void) | undefined;350 readonly drawing?: ReactNode;351 readonly files?: ReactNode;352 readonly drawn?: readonly DrawnFile[] | undefined;353}354Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 355–357why, one step back355export const CommitView = ({356 diff,357 drawing,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 358–360why, one step back358 drawn: parsed,359 files: filesButton,360 folded,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 361why, one step back361 now,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 362–364why, one step back362 onFold,363 onFoldAll,364 onSplit,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 365–366why, one step back365 project,366 split = false,Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 367–368why, one step back367 territory,368}: CommitViewProps) => {Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 369–370why, one step back369 const { commit, files, truncated } = diff;370 const body = bodyWithoutTrailers(commit);Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 371–372why, one step back371 const drawn = useMemo(() => parsed ?? drawnFiles(files), [files, parsed]);372 const stats = useMemo(() => statsOf(drawn, territory), [drawn, territory]);Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 373why, one step back373Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 374–377why, one step back374 const allFolded = drawn.every((file) =>375 folded === undefined ? file.folded : folded.has(file.file.path)376 );377Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 378why, one step back378 return (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 379–383why, one step back379 <Workspace380 facts={<CommitFacts commit={commit} project={project} stats={stats} />}381 factsHeading={382 commit.landing === undefined ? "This commit" : "This landing"383 }Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
lines 384–386why, one step back384 meta={385 <GitLine commit={commit} now={now} project={project} stats={stats} />386 }Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 387–401why, one step back387 state={388 commit.landing === undefined ? undefined : (389 <span className="flex items-center gap-1.5">390 <StateMarkIcon mark="landed" />391 <strong className="text-landed font-semibold">Landed</strong>392 </span>393 )394 }395 status={396 <div className="flex flex-col gap-4">397 <LandingStatus commit={commit} project={project} />398 {body === "" ? null : (399 <p400 className="text-body max-w-[70ch] [overflow-wrap:anywhere] whitespace-pre-wrap"401 data-body=""Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 402why, one step back402 data-said=""Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 403–408why, one step back403 >404 {body}405 </p>406 )}407 </div>408 }Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 409–413why, one step back409 title={410 commit.subject === ""411 ? `Commit ${shortSha(commit.sha)}`412 : commit.subject413 }Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 414–424why, one step back414 toolbar={415 <DiffToolbar416 allFolded={allFolded}417 files={filesButton}418 onFoldAll={onFoldAll}419 onSplit={onSplit}420 split={split}421 stats={stats}422 />423 }424 variant="code"Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 425why, one step back425 >Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 426–428why, one step back426 {truncated ? (427 <div className={`${GUTTER} ${MEASURE}`}>428 <p className="border-rule text-ui border border-dashed px-4 py-3">Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 429–431why, one step back429 This commit is too large to show whole: its files are listed by430 path, without their changes.431 </p>Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 432–433why, one step back432 </div>433 ) : null}Merge build/1500-design-draws into mainoscabrielbefore adoption9bc46b7
line 434why, one step back434 <DiffPane id="diff">Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 435–444why, one step back435 {drawing ?? (436 <div className={PANE_SCROLLER}>437 <PlainDiff438 drawn={drawn}439 files={files}440 folded={folded}441 onFold={onFold}442 split={split}443 />444 </div>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 445why, one step back445 )}Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 446–447why, one step back446 </DiffPane>447 </Workspace>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 448–449why, one step back448 );449};