/** * A commit's diff as plain text: what the server sends, and what a reader * sees until the browser has drawn the highlighted diff over it. It holds * the hunks the drawing holds (`diff-model.ts` parses both texts with the * drawing's own parser) and takes Pierre's measures: a 44 px file header, * 13 px code on 20 px lines, line numbers in a gutter of at least three * figures with two before and one after, a sign in the first of two * leading columns, unchanged runs as 32 px "N unmodified lines" bars. So * the highlighting arrives in place and nothing moves. The first files the * server highlights (`prerenderedPaths`, ADR 0027) are its drawing instead * of plain lines, under the same header. * * Each file is a disclosure whose summary is its header: folded, only the * header shows (an added or deleted file past 80 lines starts folded). * Additions and deletions take the diff's own tints, never the landed or * refused colour; a unified diff marks each line `+` or `-`, a split one * marks the side with a bar. */ import type { ChangedFile } from "@repo/protocol/domain/code"; import { Option } from "effect"; import { ChevronDown, SquareDot, SquareMinus, SquarePlus } from "lucide-react"; import type { LucideIcon } from "lucide-react"; import { useMemo } from "react"; import type { ReactNode } from "react"; import { drawnFiles, splitRows, unifiedRows } from "./diff-model.ts"; import type { DrawnFile, PlainRow, SplitRow } from "./diff-model.ts"; import { diffAnchor } from "./paths.ts"; import { diffDrawing } from "./prerender.ts"; import { ServerCode } from "./server-code.tsx"; /** One numbered line: its text, and which side it is on. */ type Line = Extract; /** A line's ground and its number's colour, by what it is. */ const TONES = { addition: { line: "bg-diff-add-bg", number: "bg-diff-add-bg text-diff-add-sign", }, context: { line: "", number: "text-muted-foreground" }, deletion: { line: "bg-diff-del-bg", number: "bg-diff-del-bg text-diff-del-sign", }, } as const satisfies Record< Line["kind"], { readonly line: string; readonly number: string } >; /** Each line's sign in a unified diff, the mark that reads without colour. */ const SIGNS = { addition: "+", context: " ", deletion: "-" } as const; /** Pierre's gutter: at least three figures, two before them and one after, then a 2 px gap. */ const GUTTER = "border-background min-w-[calc(6ch+2px)] border-r-2 pr-[1ch] pl-[2ch] text-right tabular-nums select-none"; /** An unchanged run between hunks: a 32 px bar saying how many lines it skips. */ const Separator = ({ lines, span, }: { readonly lines: number; readonly span: number; }) => (
{lines} unmodified {lines === 1 ? "line" : "lines"}
); /** One line of a unified diff: its number, then its sign and text. */ const UnifiedLine = ({ line }: { readonly line: Line }) => ( <> {line.number} {line.text} ); /** One side of a split diff's row: its number with the side's bar, then its text; empty when the side has none. */ const SplitSide = ({ line }: { readonly line: Line | undefined }) => line === undefined ? ( <> ) : ( <> {line.number} {line.text} ); /** A file's lines, unified: one grid of gutter and code, scrolling sideways in itself. */ const UnifiedBody = ({ rows }: { readonly rows: readonly PlainRow[] }) => (
{rows.map((row, index) => row.kind === "separator" ? ( ) : ( ) )}
); /** A file's lines, split: old on the left, new on the right, row beside row. */ const SplitBody = ({ rows }: { readonly rows: readonly SplitRow[] }) => (
{rows.map((row, index) => row.kind === "separator" ? ( ) : ( ) )}
); /** What a file changed, in counts: `+n −m`, each in its tint, with words for a screen reader. */ export const ChangeCounts = ({ additions, deletions, header = false, }: { readonly additions: number; readonly deletions: number; /** As a diff's file header prints them: a hyphen, and no count of nothing. */ readonly header?: boolean; }) => ( {header && additions === 0 ? null : ( +{additions} added )} {header && deletions === 0 ? null : ( {header ? "-" : "−"} {deletions} removed )} ); /** Why a file has no lines to show: binary, or its text not sent. */ const Unshown = ({ file }: { readonly file: ChangedFile }) => (

{file.binary ? "A binary file" : "Its text is not shown"}, {file.status}.

); /** * A file's lines, unified or split; why it has none when it has none. A * file the server highlighted (unified, `prerenderedPaths`) is its drawing, * and the plain lines only where the document did not bring it. */ const FileBody = ({ file, prerendered, split, }: { readonly file: DrawnFile; readonly prerendered: boolean; readonly split: boolean; }) => { const diff = Option.getOrUndefined(file.diff); if (diff === undefined) { return ; } if (split) { return ; } const plain = ; return prerendered ? (
diffDrawing(file)} plain={plain} />
) : ( plain ); }; /** Each kind of change as the drawing's header marks it, in the diff's tints. */ const CHANGE_MARKS = { added: { icon: SquarePlus, tone: "text-diff-add-sign" }, deleted: { icon: SquareMinus, tone: "text-diff-del-sign" }, modified: { icon: SquareDot, tone: "text-muted-foreground" }, } as const satisfies Record< ChangedFile["status"], { readonly icon: LucideIcon; readonly tone: string } >; /** * A file's header as the drawing draws it: the fold's chevron, the mark of * its change (with its word for a screen reader), its path, then its * counts and anything the page adds. */ export const FileHeaderLine = ({ end, file, }: { readonly file: DrawnFile; /** Anything a page puts at the header's end (a "Mark read" button). */ readonly end?: ReactNode; }) => { const { icon: Icon, tone } = CHANGE_MARKS[file.file.status]; return ( <>