Open History to see the last commit
1import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
2import type { EnvironmentId, PullRequestChangeBreakdown, PullRequestRef } from "@t3tools/contracts";
3import { useState } from "react";
4
5import { pullRequestEnvironment } from "~/state/pullRequests";
6import { useEnvironmentQuery } from "~/state/query";
7
8import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
9import { PullRequestDiffStat } from "./pullRequestPresentation";
10
11const KIND_LABELS = {
12 code: "Code",
13 tests: "Tests",
14 mocks: "Mocks",
15 generated: "Generated",
16 docs: "Docs",
17} satisfies Record<keyof NonNullable<PullRequestChangeBreakdown>, string>;
18
19type DiffTotals = { additions: number; deletions: number };
20
21/**
22 * The pull request's diff stat, with a hover split into code, tests, mocks, and generated files.
23 * In lists the split is read on first pointer or focus intent, so rows nobody hovers cost the
24 * host nothing. A view of one pull request passes `eager` so the split is ready before the hover.
25 */
26export function PullRequestChangeBreakdownStat({
27 environmentId,
28 reference,
29 additions,
30 deletions,
31 className,
32 eager = false,
33}: {
34 environmentId: EnvironmentId;
35 reference: PullRequestRef;
36 additions: number;
37 deletions: number;
38 className: string;
39 eager?: boolean;
40}) {
41 const [requested, setRequested] = useState(eager);
42 const query = useEnvironmentQuery(
43 requested ? pullRequestEnvironment.changeBreakdown({ environmentId, input: reference }) : null,
44 );
45 const breakdown = query.data;
46 if (additions === 0 && deletions === 0) return null;
47 return (
48 <Tooltip>
49 <TooltipTrigger
50 onPointerEnter={() => setRequested(true)}
51 onFocus={() => setRequested(true)}
52 render={
53 <span className="inline-flex">
54 <PullRequestDiffStat
55 additions={additions}
56 deletions={deletions}
57 className={className}
58 />
59 </span>
60 }
61 />
62 {breakdown ? (
63 <TooltipPopup side="bottom">
64 <ChangeBreakdownGrid breakdown={breakdown} total={{ additions, deletions }} />
65 </TooltipPopup>
66 ) : query.isPending ? (
67 <TooltipPopup side="bottom">Loading breakdown</TooltipPopup>
68 ) : null}
69 </Tooltip>
70 );
71}
72
73/**
74 * The split shown inline, for surfaces that already sit behind a hover, such as the thread
75 * details card. It reads on mount, so it costs the host a request only once the card opens.
76 */
77export function PullRequestChangeBreakdownRows({
78 environmentId,
79 reference,
80 className,
81}: {
82 environmentId: EnvironmentId;
83 reference: PullRequestRef;
84 className: string;
85}) {
86 const query = useEnvironmentQuery(
87 pullRequestEnvironment.changeBreakdown({ environmentId, input: reference }),
88 );
89 if (!query.data) return null;
90 return (
91 <div className={className}>
92 <ChangeBreakdownGrid breakdown={query.data} />
93 </div>
94 );
95}
96
97function ChangeBreakdownGrid({
98 breakdown,
99 total,
100}: {
101 breakdown: NonNullable<PullRequestChangeBreakdown>;
102 total?: DiffTotals;
103}) {
104 return (
105 <div className="grid grid-cols-[auto_auto_auto] justify-start gap-x-4 gap-y-1 py-0.5 font-mono tabular-nums">
106 {(Object.keys(KIND_LABELS) as Array<keyof typeof KIND_LABELS>).map((kind) => {
107 const { additions, deletions } = breakdown[kind];
108 return additions === 0 && deletions === 0 ? null : (
109 <BreakdownRow
110 key={kind}
111 label={KIND_LABELS[kind]}
112 additions={additions}
113 deletions={deletions}
114 />
115 );
116 })}
117 {total ? (
118 <>
119 <div className="col-span-3 border-t border-border" />
120 <BreakdownRow label="Total" additions={total.additions} deletions={total.deletions} />
121 </>
122 ) : null}
123 </div>
124 );
125}
126
127function BreakdownRow({ label, additions, deletions }: DiffTotals & { label: string }) {
128 return (
129 <>
130 <span className="font-sans">{label}</span>
131 <span className="text-right text-diff-addition-foreground">
132 +{formatDiffCount(additions)}
133 </span>
134 <span className="text-right text-diff-deletion">-{formatDiffCount(deletions)}</span>
135 </>
136 );
137}