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
1import type {2 CommitDiff,3 CommitView as Commit,4} from "@repo/protocol/domain/code";5import type { ProjectName } from "@repo/protocol/ids";6import { Option } from "effect";7import type { DateTime } from "effect";8import { Columns2, FoldVertical, Rows3, UnfoldVertical } from "lucide-react";9import { useMemo } from "react";10import type { ReactNode } from "react";1112import { Button } from "@/components/ui/button";1314import { CreditFacts, FactBlock, ProvenanceFacts } from "../shared/facts.tsx";15import type { ProvenanceEntry } from "../shared/facts.tsx";16import { HandleLink } from "../shared/handle.tsx";17import { AppLink } from "../shared/link.tsx";18import { StateMarkIcon } from "../shared/marks.tsx";19import { answerPath, askPath } from "../shared/paths.ts";20import { GUTTER, MEASURE, Workspace } from "../shared/shell/workspace.tsx";21import { When } from "../shared/time.tsx";22import { askOf } from "../why/rows.ts";23import { 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";2829const LINK = "decoration-rule underline hover:decoration-current";3031const TRAILER = /^[A-Za-z][\w-]*:\s/u;3233/**34 * Git's trailer block is the last paragraph when every line of it is a `Key:35 * value`.36 */37export const bodyWithoutTrailers = (commit: Commit) => {38 const paragraphs = commit.body.trim().split(/\n\s*\n/u);39 const last = paragraphs.at(-1) ?? "";4041 const trailers =42 commit.trailers.length > 0 &&43 last.split("\n").every((line) => TRAILER.test(line.trim()));4445 return (trailers ? paragraphs.slice(0, -1) : paragraphs).join("\n\n").trim();46};4748export 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,103 now,104 project,105 stats,106}: {107 readonly commit: Commit;108 readonly now: DateTime.Utc;109 readonly project: ProjectName;110 readonly stats: DiffStats;111}) => (112 <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>140 {" · "}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>151 </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"}168 </span>169 <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>192);193194const 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 );206207const 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 );217218const 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,242}: {243 readonly commit: Commit;244 readonly project: ProjectName;245 readonly stats: DiffStats;246}) => {247 const linked =248 commit.landing === undefined249 ? undefined250 : Option.getOrUndefined(askOf(commit.landing));251252 return (253 <>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 </>274 );275};276277const 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,282 files,283 onFoldAll,284 onSplit,285 split,286 stats,287}: {288 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;294}) => (295 <>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 </>338);339340export 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}354355export const CommitView = ({356 diff,357 drawing,358 drawn: parsed,359 files: filesButton,360 folded,361 now,362 onFold,363 onFoldAll,364 onSplit,365 project,366 split = false,367 territory,368}: CommitViewProps) => {369 const { commit, files, truncated } = diff;370 const body = bodyWithoutTrailers(commit);371 const drawn = useMemo(() => parsed ?? drawnFiles(files), [files, parsed]);372 const stats = useMemo(() => statsOf(drawn, territory), [drawn, territory]);373374 const allFolded = drawn.every((file) =>375 folded === undefined ? file.folded : folded.has(file.file.path)376 );377378 return (379 <Workspace380 facts={<CommitFacts commit={commit} project={project} stats={stats} />}381 factsHeading={382 commit.landing === undefined ? "This commit" : "This landing"383 }384 meta={385 <GitLine commit={commit} now={now} project={project} stats={stats} />386 }387 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=""402 data-said=""403 >404 {body}405 </p>406 )}407 </div>408 }409 title={410 commit.subject === ""411 ? `Commit ${shortSha(commit.sha)}`412 : commit.subject413 }414 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"425 >426 {truncated ? (427 <div className={`${GUTTER} ${MEASURE}`}>428 <p className="border-rule text-ui border border-dashed px-4 py-3">429 This commit is too large to show whole: its files are listed by430 path, without their changes.431 </p>432 </div>433 ) : null}434 <DiffPane id="diff">435 {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>445 )}446 </DiffPane>447 </Workspace>448 );449};