/**
* One commit, read as a landing first and a diff second. The head: the
* subject, the landing's status line ("Landed as 9b1c3e5 · answered
* format-ordinal", the SHA and the ask linked), the body as prose, and
* git's own metadata on one muted line. The inspector holds the typed
* facts: this landing's state, its credit (the trailers, once), the diff's
* stats split by path, and its provenance. Then the diff, file by file, in
* a pane of its own: the server's plain text (`PlainDiff`), which the
* browser's drawing (`drawing`, from `client-code.tsx`) replaces in place.
*
* A commit the platform did not land came in with the history the project
* adopted, and says so; its trailers are listed as written, linking no one.
*/
import type {
CommitDiff,
CommitView as Commit,
} from "@repo/protocol/domain/code";
import type { ProjectName } from "@repo/protocol/ids";
import { Option } from "effect";
import type { DateTime } from "effect";
import { Columns2, FoldVertical, Rows3, UnfoldVertical } from "lucide-react";
import { useMemo } from "react";
import type { ReactNode } from "react";
import { Button } from "@/components/ui/button";
import { CreditFacts, FactBlock, ProvenanceFacts } from "../shared/facts.tsx";
import type { ProvenanceEntry } from "../shared/facts.tsx";
import { HandleLink } from "../shared/handle.tsx";
import { AppLink } from "../shared/link.tsx";
import { StateMarkIcon } from "../shared/marks.tsx";
import { answerPath, askPath } from "../shared/paths.ts";
import { GUTTER, MEASURE, Workspace } from "../shared/shell/workspace.tsx";
import { When } from "../shared/time.tsx";
import { askOf } from "../why/rows.ts";
import { drawnFiles, statsOf } from "./diff-model.ts";
import type { DiffStats, DrawnFile } from "./diff-model.ts";
import { DiffPane, PANE_SCROLLER } from "./diff-pane.tsx";
import { commitHref, shortSha, treeHref } from "./paths.ts";
import { ChangeCounts, PlainDiff } from "./plain-diff.tsx";
const LINK = "decoration-rule underline hover:decoration-current";
/** A trailer line: `Key: value`, the key one word of letters and dashes. */
const TRAILER = /^[A-Za-z][\w-]*:\s/u;
/**
* The body without its trailers, which the Credit block shows: git's
* trailer block is the last paragraph when every line of it is a
* `Key: value`.
*/
export const bodyWithoutTrailers = (commit: Commit) => {
const paragraphs = commit.body.trim().split(/\n\s*\n/u);
const last = paragraphs.at(-1) ?? "";
const trailers =
commit.trailers.length > 0 &&
last.split("\n").every((line) => TRAILER.test(line.trim()));
return (trailers ? paragraphs.slice(0, -1) : paragraphs).join("\n\n").trim();
};
/** Where the commit stands, in one line: landed as its SHA, and the ask it answered or opened. */
export const LandingStatus = ({
commit,
project,
}: {
readonly commit: Commit;
readonly project: ProjectName;
}) => {
const { landing } = commit;
if (landing === undefined) {
return (
Before adoption: this commit came in with the history the project
adopted, so no landing records it.
)}
);
/** The trailers of a commit the platform did not land, as written, linking no one. */
const WrittenTrailers = ({ commit }: { readonly commit: Commit }) =>
commit.trailers.length === 0 ? null : (
{commit.trailers.map(({ key, value }) => (
{key}: {value}
))}
);
/** A landing's credit from the record; a commit the platform did not land, its trailers as written. */
const LandingCredit = ({ commit }: { readonly commit: Commit }) =>
commit.landing === undefined ? (
) : (
);
/** Where the commit comes from: its id, its tree, its people and parents, and the landing's request. */
const provenanceOf = (commit: Commit): readonly ProvenanceEntry[] => [
{ label: "Commit", value: commit.sha },
{ label: "Tree", value: commit.tree },
{ label: "Author", value: `${commit.author.name} <${commit.author.email}>` },
{
label: "Committer",
value: `${commit.committer.name} <${commit.committer.email}>`,
},
...commit.parents.map((parent, index) => ({
label: commit.parents.length === 1 ? "Parent" : `Parent ${index + 1}`,
value: parent,
})),
...(commit.landing?.verdict === undefined
? []
: [{ label: "Patch id", value: commit.landing.verdict.patchId }]),
...(commit.landing?.requestId === undefined
? []
: [{ label: "Request-Id", value: commit.landing.requestId }]),
];
/** The inspector's facts for a commit. */
const CommitFacts = ({
commit,
project,
stats,
}: {
readonly commit: Commit;
readonly project: ProjectName;
readonly stats: DiffStats;
}) => {
const linked =
commit.landing === undefined
? undefined
: Option.getOrUndefined(askOf(commit.landing));
return (
<>
{commit.landing === undefined ? null : (
By
{linked === undefined || linked.kind === "opened" ? null : (
<>
{" · "}
the answer and its checks
>
)}
)}
>
);
};
/** A segmented pair of buttons, the pressed one in ink. */
const SEGMENT =
"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";
/** The diff's toolbar: unified or split, fold or unfold every file, and the counts. */
export const DiffToolbar = ({
allFolded,
files,
onFoldAll,
onSplit,
split,
stats,
}: {
readonly split: boolean;
readonly onSplit?: ((split: boolean) => void) | undefined;
/** Whether every file is folded now. */
readonly allFolded: boolean;
readonly onFoldAll?: ((folded: boolean) => void) | undefined;
readonly stats: DiffStats;
/** The Files button, below the shell's width. */
readonly files?: ReactNode;
}) => (
<>
{files === undefined ? null : (