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 } from "../shared/shell/gutter.ts";
import { 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";
import { pinnedRev } from "./rev.ts";
const LINK = "decoration-rule underline hover:decoration-current";
const TRAILER = /^[A-Za-z][\w-]*:\s/u;
/**
* 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();
};
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.
);
}
const linked = Option.getOrUndefined(askOf(landing));
return (
Landed as{" "}
{shortSha(commit.sha)}
{linked === undefined ? null : (
<>
{" · "}
{linked.kind === "opened" ? "opened " : "answered "}
{linked.ask}
>
)}
);
};
const GitLine = ({
at,
commit,
now,
project,
stats,
}: {
readonly at: string;
readonly commit: Commit;
readonly now: DateTime.Utc;
readonly project: ProjectName;
readonly stats: DiffStats;
}) => (
authored by {commit.author.name}
{" · "}
committed by{" "}
{commit.landing === undefined ? commit.committer.name : "the platform"}
{" · "}
{commit.parents.length === 0 ? (
"the first commit"
) : (
<>
{commit.parents.length === 1 ? "parent" : "parents"}{" "}
{commit.parents.map((parent, index) => (
{index === 0 ? null : ", "}
{shortSha(parent)}
))}
>
)}
{" · "}
Browse files
{" · "}
{stats.files} {stats.files === 1 ? "file" : "files"}
·
);
const PART_WORDS = {
other: "other",
territory: "territory",
tests: "tests",
} as const;
export const DiffStatsFacts = ({ stats }: { readonly stats: DiffStats }) => (
{stats.files} {stats.files === 1 ? "file" : "files"}
{stats.parts.length === 0 ? null : (
{stats.parts.map((part) => (
-
{PART_WORDS[part.label]}
:
))}
)}
);
const WrittenTrailers = ({ commit }: { readonly commit: Commit }) =>
commit.trailers.length === 0 ? null : (
{commit.trailers.map(({ key, value }) => (
-
{key}: {value}
))}
);
const LandingCredit = ({ commit }: { readonly commit: Commit }) =>
commit.landing === undefined ? (
) : (
);
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 }]),
];
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
>
)}
)}
>
);
};
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";
export const DiffToolbar = ({
allFolded,
files,
onFoldAll,
onSplit,
split,
stats,
}: {
readonly split: boolean;
readonly onSplit?: ((split: boolean) => void) | undefined;
readonly allFolded: boolean;
readonly onFoldAll?: ((folded: boolean) => void) | undefined;
readonly stats: DiffStats;
readonly files?: ReactNode;
}) => (
<>
{files === undefined ? null : (
{files}
)}
{stats.files} {stats.files === 1 ? "file" : "files"}
changed
>
);
export interface CommitViewProps {
/** The fork the commit was read in (`@/`); its links stay inside it. */
readonly at?: string | undefined;
readonly diff: CommitDiff;
readonly now: DateTime.Utc;
readonly project: ProjectName;
readonly territory?: readonly string[] | undefined;
readonly split?: boolean;
readonly onSplit?: ((split: boolean) => void) | undefined;
readonly folded?: ReadonlySet | undefined;
readonly onFold?: ((path: string, folded: boolean) => void) | undefined;
readonly onFoldAll?: ((folded: boolean) => void) | undefined;
readonly drawing?: ReactNode;
readonly drawn?: readonly DrawnFile[] | undefined;
}
export const CommitView = ({
at = "main",
diff,
drawing,
drawn: parsed,
folded,
now,
onFold,
onFoldAll,
onSplit,
project,
split = false,
territory,
}: CommitViewProps) => {
const { commit, files, truncated } = diff;
const body = bodyWithoutTrailers(commit);
const drawn = useMemo(() => parsed ?? drawnFiles(files), [files, parsed]);
const stats = useMemo(() => statsOf(drawn, territory), [drawn, territory]);
const allFolded = drawn.every((file) =>
folded === undefined ? file.folded : folded.has(file.file.path)
);
return (
}
factsHeading={
commit.landing === undefined ? "This commit" : "This landing"
}
meta={
}
state={
commit.landing === undefined ? undefined : (
Landed
)
}
status={
{body === "" ? null : (
{body}
)}
}
title={
commit.subject === ""
? `Commit ${shortSha(commit.sha)}`
: commit.subject
}
toolbar={
}
variant="code"
>
{truncated ? (
This commit is too large to show whole: its files are listed by
path, without their changes.
) : null}
{drawing ?? (
)}
);
};