Skip to content
TypeScript

fix(web): PR Code tab keeps its files after a refresh with an unchanged diff (#22917)

Theo Browneb9c1293c0aad
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx
new file mode 100644
index 00000000000..34a1c1ddfd6
--- /dev/null
+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx
@@ -0,0 +1,128 @@
+import {
+ EnvironmentId,
+ ProjectId,
+ SourceControlProviderKind,
+ type PullRequestDetailView,
+ type PullRequestDiffResult,
+} from "@t3tools/contracts";
+import { expect, it, vi } from "vite-plus/test";
+
+import "~/index.css";
+
+const { diffAtom } = await vi.hoisted(async () => {
+ const { AsyncResult, Atom } = await import("effect/reactivity");
+ const firstPage: PullRequestDiffResult = {
+ patch: [
+ "diff --git a/a.ts b/a.ts",
+ "index 1111111..2222222 100644",
+ "--- a/a.ts",
+ "+++ b/a.ts",
+ "@@ -1 +1 @@",
+ "-before()",
+ "+after()",
+ "",
+ ].join("\n"),
+ truncated: false,
+ nextCursor: null,
+ };
+ // An environment query whose refetch returns equal content hands back the previous value
+ // itself, so a refresh publishes a new result around the very same page.
+ return { diffAtom: Atom.make(() => AsyncResult.success(firstPage)) };
+});
+
+vi.mock(import("~/state/use-atom-command"), async (original) => ({
+ ...(await original()),
+ useAtomCommand: (() => vi.fn()) as never,
+}));
+vi.mock(import("~/state/pullRequests"), async (original) => {
+ const actual = await original();
+ return {
+ ...actual,
+ pullRequestEnvironment: {
+ ...actual.pullRequestEnvironment,
+ diff: (() => diffAtom) as never,
+ },
+ };
+});
+
+import { render } from "../../../test/render";
+import PullRequestCodeTab from "./PullRequestCodeTab";
+
+const detail: PullRequestDetailView = {
+ provider: SourceControlProviderKind.make("github"),
+ projectId: ProjectId.make("project"),
+ projectTitle: "Project",
+ workspaceRoot: "/workspace",
+ repository: "owner/repo",
+ number: 1,
+ title: "Test pull request",
+ body: "",
+ url: "https://github.com/owner/repo/pull/1",
+ author: { login: "author", name: null, avatarUrl: null },
+ viewer: "author",
+ state: "open",
+ isDraft: false,
+ mergeability: "mergeable",
+ additions: 1,
+ deletions: 1,
+ changedFiles: 1,
+ headBranch: "feature",
+ baseBranch: "main",
+ createdAt: "2026-09-01T00:00:00Z",
+ updatedAt: "2026-09-01T00:00:00Z",
+ mergedAt: null,
+ closedAt: null,
+ reviewers: [],
+ labels: [],
+ checks: [],
+ comments: [],
+ commentCount: 0,
+ commentsTruncated: false,
+ reviewThreads: [],
+ commits: [],
+ mergeCapabilities: { merge: false, squash: false, rebase: false },
+ capabilities: {
+ diff: true,
+ comment: false,
+ search: true,
+ actions: [],
+ mergeMethods: [],
+ review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
+ reviewers: { request: false, listCandidates: false },
+ edit: { changeRequest: false, comment: false },
+ },
+ viewerPermissions: {
+ actions: [],
+ comment: false,
+ resolve: false,
+ verdicts: [],
+ requestReviewers: false,
+ },
+};
+
+const tab = (refreshToken: number) => (
+ <PullRequestCodeTab
+ scrollerRef={() => {}}
+ environmentId={EnvironmentId.make("environment")}
+ reference={detail}
+ detail={detail}
+ selectedCommitOid={null}
+ onSelectedCommitChange={() => {}}
+ onRefresh={() => {}}
+ refreshToken={refreshToken}
+ />
+);
+
+it("keeps the diff after a refresh that brings back the same page", async () => {
+ const container = document.createElement("div");
+ container.className = "flex h-[700px] w-[900px]";
+ await using screen = await render(tab(0), { container });
+ await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
+
+ await screen.rerender(tab(1));
+
+ await expect
+ .element(screen.getByText("This pull request has no file changes."))
+ .not.toBeInTheDocument();
+ await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
+});
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
index feaa39ce35e..ee152af4581 100644
--- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
@@ -431,6 +431,9 @@ function PullRequestCodeTab({
if (appliedRefreshToken.current === refreshToken) return;
appliedRefreshToken.current = refreshToken;
setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES });
+ // An equal refetch keeps the answer's identity, so the emptied slices refill only if the
+ // applied page is forgotten too.
+ setAppliedPage(null);
refreshFirstDiffPage();
refreshFilesViewed();
}, [refreshToken, scopeKey, refreshFirstDiffPage, refreshFilesViewed]);