Skip to content
TypeScript

feat(clients): every PR +/- shows its code, tests, and docs split, without the hover wait (#22255)

Theo Browne962428df104d
diff --git a/apps/mobile/src/features/pull-requests/PullRequestFiles.tsx b/apps/mobile/src/features/pull-requests/PullRequestFiles.tsx
index dc9c765bf50..f9d262e0f2d 100644
--- a/apps/mobile/src/features/pull-requests/PullRequestFiles.tsx
+++ b/apps/mobile/src/features/pull-requests/PullRequestFiles.tsx
@@ -29,6 +29,7 @@ const BREAKDOWN_LABELS = {
tests: "Tests",
mocks: "Mocks",
generated: "Generated",
+ docs: "Docs",
} satisfies Record<keyof NonNullable<PullRequestChangeBreakdown>, string>;
function PullRequestChangeBreakdownLine(props: {
diff --git a/apps/server/src/pullRequest/pullRequestChangeBreakdown.test.ts b/apps/server/src/pullRequest/pullRequestChangeBreakdown.test.ts
index 68321f39eb2..ecb347d77a8 100644
--- a/apps/server/src/pullRequest/pullRequestChangeBreakdown.test.ts
+++ b/apps/server/src/pullRequest/pullRequestChangeBreakdown.test.ts
@@ -12,6 +12,7 @@ describe("toChangeBreakdown", () => {
{ path: "src/b.ts", additions: 5, deletions: 1 },
{ path: "src/a.test.ts", additions: 40, deletions: 0 },
{ path: "pnpm-lock.yaml", additions: 300, deletions: 120 },
+ { path: "README.md", additions: 7, deletions: 3 },
],
}),
).toEqual({
@@ -19,6 +20,7 @@ describe("toChangeBreakdown", () => {
tests: { additions: 40, deletions: 0 },
mocks: { additions: 0, deletions: 0 },
generated: { additions: 300, deletions: 120 },
+ docs: { additions: 7, deletions: 3 },
});
});
diff --git a/apps/server/src/pullRequest/pullRequestChangeBreakdown.ts b/apps/server/src/pullRequest/pullRequestChangeBreakdown.ts
index 77d8162e895..eaaf50c0d86 100644
--- a/apps/server/src/pullRequest/pullRequestChangeBreakdown.ts
+++ b/apps/server/src/pullRequest/pullRequestChangeBreakdown.ts
@@ -5,7 +5,13 @@ import type { ProviderFileStats } from "@t3tools/source-control-core/server/Pull
export function toChangeBreakdown(stats: ProviderFileStats): PullRequestChangeBreakdown {
if (stats.truncated) return null;
const empty = () => ({ additions: 0, deletions: 0 });
- const breakdown = { code: empty(), tests: empty(), mocks: empty(), generated: empty() };
+ const breakdown = {
+ code: empty(),
+ tests: empty(),
+ mocks: empty(),
+ generated: empty(),
+ docs: empty(),
+ };
for (const file of stats.files) {
const bucket = breakdown[classifyChangedFile(file.path)];
bucket.additions += file.additions;
diff --git a/apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx b/apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx
index 10ad05b12e3..48fef8ee4a8 100644
--- a/apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx
+++ b/apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx
@@ -12,6 +12,7 @@ import { AsyncResult } from "effect/reactivity";
import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
const state = vi.hoisted(() => ({
status: "success" as PullRequestCheck["status"],
@@ -26,7 +27,14 @@ const state = vi.hoisted(() => ({
vi.mock(import("~/state/entities"), async (importOriginal) => ({
...(await importOriginal()),
- useServerConfigs: (() => new Map()) as never,
+ // Rows render a pull request only with a project, so this does not reach tests that pass none.
+ useServerConfigs: (() =>
+ new Map([
+ [
+ EnvironmentId.make("environment"),
+ { environment: { capabilities: { pullRequests: true } } },
+ ],
+ ])) as never,
}));
const queryData = () => ({
projectId: ProjectId.make("project"),
@@ -58,6 +66,16 @@ vi.mock(import("~/state/query"), async (importOriginal) => ({
select(AsyncResult.success(queryData()))) as never,
useEnvironmentQueryIsNewer: () => true,
useEnvironmentQueryRefresh: () => ({ refresh: vi.fn(), isPending: () => false }),
+ useEnvironmentQuery: (() => ({
+ data: {
+ code: { additions: 1, deletions: 0 },
+ tests: { additions: 0, deletions: 0 },
+ mocks: { additions: 0, deletions: 0 },
+ generated: { additions: 0, deletions: 0 },
+ docs: { additions: 7, deletions: 3 },
+ },
+ isPending: false,
+ })) as never,
}));
vi.mock(import("../pullRequest/usePullRequestActions"), async (importOriginal) => ({
...(await importOriginal()),
@@ -231,3 +249,25 @@ it("shows the current checkout recovery detail when fixing checks fails and is r
second.detail,
]);
});
+
+it("splits the change by file kind in the hover card", async () => {
+ act(() =>
+ root.render(
+ <ThreadDetailsPrRow
+ environmentId={EnvironmentId.make("environment")}
+ pr={null}
+ number={1}
+ reference={{ repository: "owner/repo", number: 1 }}
+ status={null}
+ project={{ id: ProjectId.make("project"), repositoryIdentity: null } as never}
+ label="Test PR"
+ openAriaLabel="Open PR"
+ onOpen={vi.fn()}
+ />,
+ ),
+ );
+ await act(() => userEvent.hover(container.querySelector('button[aria-label="Open PR"]')!));
+ await expect
+ .poll(() => document.querySelector('[data-slot="tooltip-popup"][data-open]')?.textContent)
+ .toMatch(/Docs\+7-3/);
+});
diff --git a/apps/web/src/components/chat/ThreadDetailsPrRow.tsx b/apps/web/src/components/chat/ThreadDetailsPrRow.tsx
index 5bf544f1416..c419beb89d3 100644
--- a/apps/web/src/components/chat/ThreadDetailsPrRow.tsx
+++ b/apps/web/src/components/chat/ThreadDetailsPrRow.tsx
@@ -1,3 +1,4 @@
+import { PullRequestChangeBreakdownRows } from "../pullRequest/PullRequestChangeBreakdownStat";
import { ThreadDetailsControl } from "./ThreadDetailsControl";
import { PullRequestGlyph } from "../pullRequest/pullRequestIcons";
import { selectEnvironmentQueryData } from "@t3tools/client-runtime/state/runtime";
@@ -292,6 +293,13 @@ export function ThreadDetailsPrRow({
<PullRequestDiffStat additions={detail.additions} deletions={detail.deletions} />
</div>
</div>
+ {reference !== null && (detail.additions > 0 || detail.deletions > 0) ? (
+ <PullRequestChangeBreakdownRows
+ environmentId={environmentId}
+ reference={reference}
+ className="pl-5 text-foreground/75"
+ />
+ ) : null}
</div>
</div>
</TooltipPopup>
diff --git a/apps/web/src/components/pullRequest/PullRequestChangeBreakdownStat.tsx b/apps/web/src/components/pullRequest/PullRequestChangeBreakdownStat.tsx
index cba047e2a41..e4d6765fd20 100644
--- a/apps/web/src/components/pullRequest/PullRequestChangeBreakdownStat.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestChangeBreakdownStat.tsx
@@ -13,12 +13,15 @@ const KIND_LABELS = {
tests: "Tests",
mocks: "Mocks",
generated: "Generated",
+ docs: "Docs",
} satisfies Record<keyof NonNullable<PullRequestChangeBreakdown>, string>;
+type DiffTotals = { additions: number; deletions: number };
+
/**
* The pull request's diff stat, with a hover split into code, tests, mocks, and generated files.
- * The split is read on first pointer or focus intent, so a page nobody hovers costs the host
- * nothing.
+ * In lists the split is read on first pointer or focus intent, so rows nobody hovers cost the
+ * host nothing. A view of one pull request passes `eager` so the split is ready before the hover.
*/
export function PullRequestChangeBreakdownStat({
environmentId,
@@ -26,14 +29,16 @@ export function PullRequestChangeBreakdownStat({
additions,
deletions,
className,
+ eager = false,
}: {
environmentId: EnvironmentId;
reference: PullRequestRef;
additions: number;
deletions: number;
className: string;
+ eager?: boolean;
}) {
- const [requested, setRequested] = useState(false);
+ const [requested, setRequested] = useState(eager);
const query = useEnvironmentQuery(
requested ? pullRequestEnvironment.changeBreakdown({ environmentId, input: reference }) : null,
);
@@ -56,21 +61,7 @@ export function PullRequestChangeBreakdownStat({
/>
{breakdown ? (
<TooltipPopup side="bottom">
- <div className="grid grid-cols-[auto_auto_auto] gap-x-4 gap-y-1 py-0.5 font-mono tabular-nums">
- {(Object.keys(KIND_LABELS) as Array<keyof typeof KIND_LABELS>).map((kind) => {
- const { additions, deletions } = breakdown[kind];
- return additions === 0 && deletions === 0 ? null : (
- <BreakdownRow
- key={kind}
- label={KIND_LABELS[kind]}
- additions={additions}
- deletions={deletions}
- />
- );
- })}
- <div className="col-span-3 border-t border-border" />
- <BreakdownRow label="Total" additions={additions} deletions={deletions} />
- </div>
+ <ChangeBreakdownGrid breakdown={breakdown} total={{ additions, deletions }} />
</TooltipPopup>
) : query.isPending ? (
<TooltipPopup side="bottom">Loading breakdown</TooltipPopup>
@@ -79,15 +70,61 @@ export function PullRequestChangeBreakdownStat({
);
}
-function BreakdownRow({
- label,
- additions,
- deletions,
+/**
+ * The split shown inline, for surfaces that already sit behind a hover, such as the thread
+ * details card. It reads on mount, so it costs the host a request only once the card opens.
+ */
+export function PullRequestChangeBreakdownRows({
+ environmentId,
+ reference,
+ className,
}: {
- label: string;
- additions: number;
- deletions: number;
+ environmentId: EnvironmentId;
+ reference: PullRequestRef;
+ className: string;
}) {
+ const query = useEnvironmentQuery(
+ pullRequestEnvironment.changeBreakdown({ environmentId, input: reference }),
+ );
+ if (!query.data) return null;
+ return (
+ <div className={className}>
+ <ChangeBreakdownGrid breakdown={query.data} />
+ </div>
+ );
+}
+
+function ChangeBreakdownGrid({
+ breakdown,
+ total,
+}: {
+ breakdown: NonNullable<PullRequestChangeBreakdown>;
+ total?: DiffTotals;
+}) {
+ return (
+ <div className="grid grid-cols-[auto_auto_auto] justify-start gap-x-4 gap-y-1 py-0.5 font-mono tabular-nums">
+ {(Object.keys(KIND_LABELS) as Array<keyof typeof KIND_LABELS>).map((kind) => {
+ const { additions, deletions } = breakdown[kind];
+ return additions === 0 && deletions === 0 ? null : (
+ <BreakdownRow
+ key={kind}
+ label={KIND_LABELS[kind]}
+ additions={additions}
+ deletions={deletions}
+ />
+ );
+ })}
+ {total ? (
+ <>
+ <div className="col-span-3 border-t border-border" />
+ <BreakdownRow label="Total" additions={total.additions} deletions={total.deletions} />
+ </>
+ ) : null}
+ </div>
+ );
+}
+
+function BreakdownRow({ label, additions, deletions }: DiffTotals & { label: string }) {
return (
<>
<span className="font-sans">{label}</span>
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index 33c87722ff5..d35c85caf30 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -2436,6 +2436,7 @@ export function PullRequestDetailPanel({
additions={detail.additions}
deletions={detail.deletions}
className="shrink-0 font-mono text-2xs"
+ eager
/>
</span>
</div>
@@ -2612,6 +2613,7 @@ export function PullRequestDetailPanel({
additions={detail.additions}
deletions={detail.deletions}
className="shrink-0 font-mono text-xs"
+ eager
/>
}
/>
diff --git a/apps/web/src/components/pullRequest/PullRequestRow.tsx b/apps/web/src/components/pullRequest/PullRequestRow.tsx
index 2bdbc288626..853305d9d27 100644
--- a/apps/web/src/components/pullRequest/PullRequestRow.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestRow.tsx
@@ -1,6 +1,7 @@
import type { PullRequestAction } from "@t3tools/contracts";
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import { SearchIcon } from "lucide-react";
+import { PullRequestChangeBreakdownStat } from "./PullRequestChangeBreakdownStat";
import { PullRequestStackPopover } from "./PullRequestStackPopover";
import {
PullRequestSpeedActions,
@@ -22,11 +23,7 @@ import {
PullRequestRowGlyph,
PullRequestRowLines,
} from "./PullRequestListRow";
-import {
- PullRequestDiffStat,
- PullRequestLabelChip,
- PullRequestReviewDecisionGlyph,
-} from "./pullRequestPresentation";
+import { PullRequestLabelChip, PullRequestReviewDecisionGlyph } from "./pullRequestPresentation";
const LABEL_SLOTS = [
{ overflow: "@xl/pr-row-meta:hidden" },
@@ -186,7 +183,14 @@ function PullRequestRowImpl({
}
/>
) : null}
- <PullRequestDiffStat
+ <PullRequestChangeBreakdownStat
+ environmentId={entry.environmentId}
+ reference={{
+ projectId: entry.projectId,
+ host: entry.host,
+ repository: entry.repository,
+ number: entry.number,
+ }}
additions={entry.additions}
deletions={entry.deletions}
className="font-mono"
diff --git a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
index 8cbe0c21b50..a650a71a977 100644
--- a/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
+++ b/apps/web/src/components/pullRequest/ThreadPullRequestsPanel.tsx
@@ -29,6 +29,7 @@ import { findProjectForChangeRequest, useOpenPrLink } from "~/lib/openPullReques
import { cn } from "~/lib/utils";
import { useShortcutModifierState } from "~/shortcutModifierState";
import { useProjects, useServerConfigs, useThreadShell } from "~/state/entities";
+import { PullRequestChangeBreakdownStat } from "./PullRequestChangeBreakdownStat";
import { PullRequestsUnavailableState } from "./PullRequestsUnavailableState";
import { threadEnvironment } from "~/state/threads";
import { useAtomCommand } from "~/state/use-atom-command";
@@ -209,11 +210,26 @@ function LinkRowImpl({
) : null
}
status={
- <PullRequestDiffStat
- additions={snapshot?.additions ?? 0}
- deletions={snapshot?.deletions ?? 0}
- className="font-mono"
- />
+ projectId === null ? (
+ <PullRequestDiffStat
+ additions={snapshot?.additions ?? 0}
+ deletions={snapshot?.deletions ?? 0}
+ className="font-mono"
+ />
+ ) : (
+ <PullRequestChangeBreakdownStat
+ environmentId={threadRef.environmentId}
+ reference={{
+ projectId,
+ host: link.host,
+ repository: link.repository,
+ number: link.number,
+ }}
+ additions={snapshot?.additions ?? 0}
+ deletions={snapshot?.deletions ?? 0}
+ className="font-mono"
+ />
+ )
}
meta={
<>
diff --git a/packages/contracts/src/pullRequest.test.ts b/packages/contracts/src/pullRequest.test.ts
index b20f78dd957..941e998763a 100644
--- a/packages/contracts/src/pullRequest.test.ts
+++ b/packages/contracts/src/pullRequest.test.ts
@@ -5,6 +5,7 @@ import { describe, expect, it } from "vite-plus/test";
import {
PullRequestActionInput,
PullRequestCapabilities,
+ PullRequestChangeBreakdown,
PullRequestDetail,
PullRequestFilesViewedResult,
PullRequestListInput,
@@ -18,6 +19,9 @@ import {
const decodeListResult = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestListResult));
const decodeDetail = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestDetail));
+const decodeChangeBreakdown = Schema.decodeUnknownSync(
+ Schema.toCodecJson(PullRequestChangeBreakdown),
+);
const decodeListInput = Schema.decodeUnknownSync(PullRequestListInput);
const decodeReviewerRequest = Schema.decodeUnknownSync(PullRequestReviewerRequestInput);
const decodeAction = Schema.decodeUnknownSync(Schema.toCodecJson(PullRequestActionInput));
@@ -423,3 +427,23 @@ describe("naming the file a tick belongs to", () => {
).toThrow();
});
});
+
+describe("PullRequestChangeBreakdown", () => {
+ it("reads a breakdown from a server that predates the docs kind", () => {
+ const line = { additions: 1, deletions: 0 };
+ expect(
+ decodeChangeBreakdown({
+ code: line,
+ tests: line,
+ mocks: line,
+ generated: line,
+ }),
+ ).toEqual({
+ code: line,
+ tests: line,
+ mocks: line,
+ generated: line,
+ docs: { additions: 0, deletions: 0 },
+ });
+ });
+});
diff --git a/packages/contracts/src/pullRequest.ts b/packages/contracts/src/pullRequest.ts
index 1bdedef2475..5553bb7f176 100644
--- a/packages/contracts/src/pullRequest.ts
+++ b/packages/contracts/src/pullRequest.ts
@@ -1,4 +1,5 @@
import * as Schema from "effect/Schema";
+import * as Effect from "effect/Effect";
import * as HttpServerRespondable from "effect/http/HttpServerRespondable";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
@@ -698,6 +699,10 @@ export const PullRequestChangeBreakdown = Schema.NullOr(
tests: PullRequestLineCount,
mocks: PullRequestLineCount,
generated: PullRequestLineCount,
+ // Servers from before the docs split send no key; their docs lines sit in `code`.
+ docs: PullRequestLineCount.pipe(
+ Schema.withDecodingDefault(Effect.succeed({ additions: 0, deletions: 0 })),
+ ),
}),
);
export type PullRequestChangeBreakdown = typeof PullRequestChangeBreakdown.Type;
diff --git a/packages/shared/src/changedFileKind.test.ts b/packages/shared/src/changedFileKind.test.ts
index a3a8fd62fae..9ff3cea8395 100644
--- a/packages/shared/src/changedFileKind.test.ts
+++ b/packages/shared/src/changedFileKind.test.ts
@@ -36,6 +36,12 @@ describe("classifyChangedFile", () => {
["src/routeTree.gen.ts", "generated"],
["src/__generated__/types.ts", "generated"],
["dist/app.min.js", "generated"],
+ ["README.md", "docs"],
+ ["docs/internals/overview.mdx", "docs"],
+ ["NOTES.txt", "docs"],
+ ["requirements-dev.txt", "code"],
+ ["CMakeLists.txt", "code"],
+ ["tests/README.md", "tests"],
] as const)("%s is %s", (path, kind) => {
expect(classifyChangedFile(path)).toBe(kind);
});
diff --git a/packages/shared/src/changedFileKind.ts b/packages/shared/src/changedFileKind.ts
index e2d6dcbe1d7..9fd5c0c7edc 100644
--- a/packages/shared/src/changedFileKind.ts
+++ b/packages/shared/src/changedFileKind.ts
@@ -58,10 +58,14 @@ const TEST_DIRS = new Set([
"tests",
]);
+const DOCS_NAME = /\.(md|mdx|markdown|rst|adoc|txt)$/i;
+// Plain-text files that tools read as config or input, not prose.
+const NOT_DOCS_NAME = /^(requirements.*|constraints.*|CMakeLists|robots|llms)\.txt$/i;
+
/**
* Sorts a changed file by its path alone, using common naming conventions. Lockfiles and build
* output count as generated, and test support (mocks, fixtures, snapshots) is kept apart from the
- * tests themselves, so a reviewer can see how much of a change is logic.
+ * tests themselves, and prose (Markdown, plain text) is its own kind, so a reviewer can see how much of a change is logic.
*/
export function classifyChangedFile(path: string): ChangedFileKind {
const segments = path.replaceAll("\\", "/").split("/");
@@ -72,5 +76,6 @@ export function classifyChangedFile(path: string): ChangedFileKind {
return "generated";
if (MOCK_NAME.test(name) || inDir(MOCK_DIRS)) return "mocks";
if (TEST_NAME.test(name) || CLASS_TEST_NAME.test(name) || inDir(TEST_DIRS)) return "tests";
+ if (DOCS_NAME.test(name) && !NOT_DOCS_NAME.test(name)) return "docs";
return "code";
}