The best way to control Claude Code, Codex and more from any computer or phone
fix(web): files marked Viewed stay folded when a pull request's Code tab opens again (#34168)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
M.Umais Adeed committed
Nov 11, 2026 at 12:11 UTC
8cbc95923a7168b3ee85c9dbeb224f57ca48f820
4 files changed
+125
-51
apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx
+58
-1
@@ -56,12 +56,43 @@ const { diffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
56
};
57
});
58
59
-const mocks = vi.hoisted(() => ({ paged: false, diffCommits: [] as Array<string | undefined> }));
59
+const mocks = vi.hoisted(() => ({
60
+ paged: false,
61
+ diffCommits: [] as Array<string | undefined>,
62
+ viewedPaths: null as ReadonlySet<string> | null,
63
+ viewedListeners: new Set<() => void>(),
64
+}));
65
+
66
+function deliverViewedPaths(paths: ReadonlySet<string> | null) {
67
+ mocks.viewedPaths = paths;
68
+ for (const listener of mocks.viewedListeners) listener();
69
+}
70
71
vi.mock(import("~/state/use-atom-command"), async (original) => ({
72
...(await original()),
73
useAtomCommand: (() => vi.fn()) as never,
74
}));
75
+vi.mock(import("./usePullRequestFilesViewed"), async () => {
76
+ const { useSyncExternalStore } = await import("react");
77
+ const subscribe = (listener: () => void) => {
78
+ mocks.viewedListeners.add(listener);
79
+ return () => mocks.viewedListeners.delete(listener);
80
+ };
81
+ return {
82
+ usePullRequestFilesViewed: () => {
83
+ const viewedPaths = useSyncExternalStore(subscribe, () => mocks.viewedPaths);
84
+ return {
85
+ enabled: viewedPaths !== null,
86
+ isViewed: (path: string) => viewedPaths?.has(path) === true,
87
+ isStale: () => false,
88
+ setViewed: () => {},
89
+ truncated: false,
90
+ error: null,
91
+ refresh: () => {},
92
+ };
93
+ },
94
+ };
95
+});
96
vi.mock(import("~/state/pullRequests"), async (original) => {
97
const actual = await original();
98
return {
@@ -136,6 +167,8 @@ function setup({ paged }: { paged: boolean }) {
167
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
168
mocks.paged = paged;
169
mocks.diffCommits = [];
170
+ mocks.viewedPaths = null;
171
+ mocks.viewedListeners.clear();
172
}
173
174
const tab = (
@@ -174,6 +207,30 @@ it("keeps the diff after a refresh that brings back the same page", async () =>
207
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
208
});
209
210
+// regression: #13279 a Viewed answer that lands after a link reveals a file must not fold it
211
+it("keeps a revealed file open when its Viewed mark loads afterwards", async () => {
212
+ setup({ paged: false });
213
+ __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false });
214
+ using _preferences = {
215
+ [Symbol.dispose]() {
216
+ __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
217
+ },
218
+ };
219
+ mocks.viewedPaths = new Set();
220
+ const container = document.createElement("div");
221
+ container.className = "flex h-[700px] w-[900px]";
222
+ await using screen = await render(tab(0, { path: "a.ts", nonce: 1 }), { container });
223
+ await expect.element(screen.getByText("three()", { exact: true })).toBeVisible();
224
+
225
+ deliverViewedPaths(new Set(["a.ts"]));
226
+ await expect.element(screen.getByRole("checkbox", { name: "Viewed" })).toBeChecked();
227
+
228
+ await expect
229
+ .element(screen.getByRole("button", { name: "Collapse diff" }))
230
+ .toHaveAttribute("aria-expanded", "true");
231
+ await expect.element(screen.getByText("three()", { exact: true })).toBeVisible();
232
+});
233
+
234
function pagedContainer() {
235
const container = document.createElement("div");
236
container.className = "flex h-[500px] w-[900px]";
apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+30
-27
@@ -123,8 +123,9 @@ import {
123
getFileViewedRange,
124
isFileDiffCollapsed,
125
isLineInFileDiff,
126
- toggleFileDiffFoldForViewed,
126
+ withFileDiffFold,
127
type DiffFoldOverride,
128
+ type FileDiffFoldChoices,
129
} from "./pullRequestDiff.logic";
130
import { PullRequestDiffStat, PullRequestMetaLine } from "./pullRequestPresentation";
131
import { PullRequestDiffUnavailableState } from "./PullRequestDiffUnavailableState";
@@ -385,7 +386,7 @@ function PullRequestCodeTab({
386
diffLayout: settings.diffLayout,
387
wordWrap: settings.wordWrap,
388
}));
388
- const [toggledFiles, setToggledFiles] = useState<ReadonlySet<string>>(() => new Set());
389
+ const [foldChoices, setFoldChoices] = useState<FileDiffFoldChoices>(() => new Map());
390
const [lastViewedFileKey, setLastViewedFileKey] = useState<string | null>(null);
391
const [visibleCommitCount, setVisibleCommitCount] = useState(COMMIT_PAGE_SIZE);
392
const [foldOverride, setFoldOverride] = useState<DiffFoldOverride>(null);
@@ -430,7 +431,7 @@ function PullRequestCodeTab({
431
setLastViewedFileKey(null);
432
setDraft(null);
433
setSelectedLines(null);
433
- setToggledFiles(new Set());
434
+ setFoldChoices(new Map());
435
setFoldOverride(null);
436
setVisibleCommitCount(COMMIT_PAGE_SIZE);
437
setOrphansOpen(false);
@@ -740,9 +741,17 @@ function PullRequestCodeTab({
741
</div>
742
);
743
744
+ // A stale mark means the file changed since it was viewed; Fold all and Expand all outrank marks.
745
+ const foldsAsViewed = (path: string) =>
746
+ foldOverride === null && filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path);
747
const items: CodeViewDiffItem<ReviewAnnotationGroup>[] = annotatedFiles.map(
748
({ fileKey, path, fileDiff, annotations, annotationsVersion }) => {
745
- const collapsed = isFileDiffCollapsed(fileKey, effectiveFoldOverride, toggledFiles);
749
+ const collapsed = isFileDiffCollapsed(
750
+ fileKey,
751
+ effectiveFoldOverride,
752
+ foldChoices,
753
+ foldsAsViewed(path),
754
+ );
755
const viewedMark = filesViewedEnabled
756
? `e${isFileViewed(path) ? "v" : ""}${isFileViewedStale(path) ? "s" : ""}`
757
: "";
@@ -783,13 +792,17 @@ function PullRequestCodeTab({
792
);
793
};
794
786
- const toggleFile = (fileKey: string) =>
787
- setToggledFiles((current) => {
788
- const next = new Set(current);
789
- if (next.has(fileKey)) next.delete(fileKey);
790
- else next.add(fileKey);
791
- return next;
792
- });
795
+ const toggleFile = (fileKey: string) => {
796
+ const path = annotatedFiles.find((file) => file.fileKey === fileKey)?.path;
797
+ const viewed = path !== undefined && foldsAsViewed(path);
798
+ setFoldChoices((current) =>
799
+ withFileDiffFold(
800
+ current,
801
+ fileKey,
802
+ !isFileDiffCollapsed(fileKey, effectiveFoldOverride, current, viewed),
803
+ ),
804
+ );
805
+ };
806
807
const setFileViewed = (fileKey: string, viewed: boolean, extendRange: boolean) => {
808
const range = getFileViewedRange(
@@ -799,12 +812,8 @@ function PullRequestCodeTab({
812
);
813
setLastViewedFileKey(fileKey);
814
for (const file of range) setViewed(file.path, viewed);
802
- setToggledFiles((current) =>
803
- range.reduce(
804
- (next, file) =>
805
- toggleFileDiffFoldForViewed(file.fileKey, viewed, effectiveFoldOverride, next),
806
- current,
807
- ),
815
+ setFoldChoices((current) =>
816
+ range.reduce((next, file) => withFileDiffFold(next, file.fileKey, viewed), current),
817
);
818
};
819
const fileViewedToggle = {
@@ -817,19 +826,15 @@ function PullRequestCodeTab({
826
const revealFile = (path: string, range?: SelectedLineRange) => {
827
const item = items.find((candidate) => resolveFileDiffPath(candidate.fileDiff) === path);
828
if (item === undefined) return;
820
- // Unfold only: a second reveal before the unfold renders must not fold the file again.
821
- if (item.collapsed === true) {
822
- setToggledFiles((current) =>
823
- toggleFileDiffFoldForViewed(item.id, false, effectiveFoldOverride, current),
824
- );
825
- }
829
+ // Record the unfold even when already open, so a Viewed mark that loads later cannot fold it.
830
+ setFoldChoices((current) => withFileDiffFold(current, item.id, false));
831
if (range) setSelectedLines({ id: item.id, range });
832
codeViewReveal.request(item.id, range);
833
};
834
835
const toggleAllFiles = () => {
836
setFoldOverride(areAllDiffFilesCollapsed(fileKeys, collapsedFileKeys) ? "expanded" : "folded");
832
- setToggledFiles(new Set());
837
+ setFoldChoices(new Map());
838
};
839
840
const orderedCommits = // GitLab preserves UTC offsets, so compare instants.
@@ -971,9 +976,7 @@ function PullRequestCodeTab({
976
onPostRender: codeViewReveal.flush,
977
// Find may repeat requests before render; only unfold, never toggle.
978
onRevealSearchMatch: (item: CodeViewItem<ReviewAnnotationGroup>) =>
974
- setToggledFiles((current) =>
975
- toggleFileDiffFoldForViewed(item.id, false, effectiveFoldOverride, current),
976
- ),
979
+ setFoldChoices((current) => withFileDiffFold(current, item.id, false)),
980
};
981
982
const runThreadCommand = async (
apps/web/src/components/pullRequest/pullRequestDiff.logic.test.ts
+21
-8
@@ -5,7 +5,7 @@ import {
5
getFileViewedRange,
6
isFileDiffCollapsed,
7
isLineInFileDiff,
8
- toggleFileDiffFoldForViewed,
8
+ withFileDiffFold,
9
} from "./pullRequestDiff.logic";
10
11
function fileWithHunks(
@@ -43,16 +43,29 @@ describe("isLineInFileDiff", () => {
43
describe("isFileDiffCollapsed", () => {
44
// regression: #9174 diffs now expand by default; a reader's fold must survive new slices
45
it("keeps a file the reader folded closed as the next slice arrives", () => {
46
- const toggled = new Set(["b.ts"]);
47
- expect(isFileDiffCollapsed("b.ts", null, toggled)).toBe(true);
48
- expect(isFileDiffCollapsed("c.ts", null, toggled)).toBe(false);
46
+ const choices = new Map([["b.ts", true]]);
47
+ expect(isFileDiffCollapsed("b.ts", "expanded", choices)).toBe(true);
48
+ expect(isFileDiffCollapsed("c.ts", "expanded", choices)).toBe(false);
49
+ });
50
+
51
+ it("folds a file marked Viewed when the Code tab mounts again with no fold choices", () => {
52
+ expect(isFileDiffCollapsed("a.ts", "expanded", new Map(), true)).toBe(true);
53
+ expect(isFileDiffCollapsed("a.ts", "expanded", new Map(), false)).toBe(false);
54
+ });
55
+
56
+ it("keeps a reader's choice when the file's Viewed mark loads later", () => {
57
+ const choices = new Map([["a.ts", false]]);
58
+ expect(isFileDiffCollapsed("a.ts", "expanded", choices, false)).toBe(false);
59
+ expect(isFileDiffCollapsed("a.ts", "expanded", choices, true)).toBe(false);
60
});
61
});
62
52
-describe("toggleFileDiffFoldForViewed", () => {
53
- it("moves against whatever the toolbar last asked for", () => {
54
- expect([...toggleFileDiffFoldForViewed("a.ts", true, "expanded", new Set())]).toEqual(["a.ts"]);
55
- expect(toggleFileDiffFoldForViewed("a.ts", false, "expanded", new Set()).size).toBe(0);
63
+describe("withFileDiffFold", () => {
64
+ it("records an absolute choice and keeps the same map when nothing changes", () => {
65
+ const folded = withFileDiffFold(new Map(), "a.ts", true);
66
+ expect(folded.get("a.ts")).toBe(true);
67
+ expect(withFileDiffFold(folded, "a.ts", true)).toBe(folded);
68
+ expect(withFileDiffFold(folded, "a.ts", false).get("a.ts")).toBe(false);
69
});
70
});
71
apps/web/src/components/pullRequest/pullRequestDiff.logic.ts
+16
-15
@@ -16,28 +16,29 @@ export function isLineInFileDiff(
16
17
export type DiffFoldOverride = "expanded" | "folded" | null;
18
19
-/** Keep fold choices relative to the toolbar so later pages inherit Fold all. */
19
+/** The reader's own fold choice per file. It is absolute, so a Viewed mark loading later keeps it. */
20
+export type FileDiffFoldChoices = ReadonlyMap<string, boolean>;
21
+
22
+/**
23
+ * A reader's own choice wins. Otherwise the toolbar decides, and a file still marked Viewed
24
+ * starts folded. Fold all and Expand all clear the choices, so later pages follow the toolbar.
25
+ */
26
export function isFileDiffCollapsed(
27
fileKey: string,
28
foldOverride: DiffFoldOverride,
23
- toggledFileKeys: ReadonlySet<string>,
29
+ choices: FileDiffFoldChoices,
30
+ viewed = false,
31
): boolean {
25
- const foldedByDefault = foldOverride === "folded";
26
- return toggledFileKeys.has(fileKey) ? !foldedByDefault : foldedByDefault;
32
+ return choices.get(fileKey) ?? (foldOverride === "folded" || viewed);
33
}
34
29
-export function toggleFileDiffFoldForViewed(
35
+export function withFileDiffFold(
36
+ choices: FileDiffFoldChoices,
37
fileKey: string,
31
- viewed: boolean,
32
- foldOverride: DiffFoldOverride,
33
- toggledFileKeys: ReadonlySet<string>,
34
-): ReadonlySet<string> {
35
- if (isFileDiffCollapsed(fileKey, foldOverride, toggledFileKeys) === viewed)
36
- return toggledFileKeys;
37
- const next = new Set(toggledFileKeys);
38
- if (next.has(fileKey)) next.delete(fileKey);
39
- else next.add(fileKey);
40
- return next;
38
+ collapsed: boolean,
39
+): FileDiffFoldChoices {
40
+ if (choices.get(fileKey) === collapsed) return choices;
41
+ return new Map(choices).set(fileKey, collapsed);
42
}
43
44
export function getFileViewedRange<T extends { readonly fileKey: string }>(