The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 22:00 UTC
fix(web): diff panel keeps collapsed files when switching right-panel tabs (#19533)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Jair Meza committed
Oct 20, 2026 at 01:35 UTC
31be0c4d5852406cd55c93232aa54dac96c0ca37
3 files changed
+122
-44
apps/web/src/components/DiffPanel.tsx
+28
-39
@@ -29,7 +29,11 @@ import { type DraftId } from "../composerDraftStore";
29
import { openDiffFilePrimaryAction } from "../diffFileActions";
30
import { useCheckpointDiff } from "~/lib/checkpointDiffState";
31
import { cn } from "~/lib/utils";
32
-import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore";
32
+import {
33
+ selectThreadDiffPanelSelection,
34
+ useDiffPanelSessionStore,
35
+ useDiffPanelStore,
36
+} from "../diffPanelStore";
37
import { useLocalStorage } from "../hooks/useLocalStorage";
38
import { useRenderedAppearance } from "../hooks/useTheme";
39
import { useStableCallback } from "../hooks/useStableCallback";
@@ -111,11 +115,6 @@ function getCachedFileEntry(fileDiff: FileDiffMetadata) {
115
return entry;
116
}
117
114
-interface CollapsedDiffFilesState {
115
- readonly scopeKey: string | null;
116
- readonly fileKeys: ReadonlySet<string>;
117
-}
118
-
118
const EMPTY_COLLAPSED_DIFF_FILE_KEYS: ReadonlySet<string> = new Set();
119
120
function DiffFileCollapseToggle({
@@ -214,10 +213,6 @@ export default function DiffPanel({
213
Schema.Boolean,
214
);
215
const [baseRefQuery, setBaseRefQuery] = useState("");
217
- const [collapsedDiffFiles, setCollapsedDiffFiles] = useState<CollapsedDiffFilesState>(() => ({
218
- scopeKey: null,
219
- fileKeys: EMPTY_COLLAPSED_DIFF_FILE_KEYS,
220
- }));
216
const [codeViewRevision, setCodeViewRevision] = useState(0);
217
const [codeView, setCodeView] = useState<AnnotatableCodeViewHandle | null>(null);
218
@@ -514,10 +509,18 @@ export default function DiffPanel({
509
const defaultCollapsedDiffFileKeys = settings.diffFilesCollapsed
510
? new Set(diffFileKeys)
511
: EMPTY_COLLAPSED_DIFF_FILE_KEYS;
517
- const collapsedDiffFileKeys =
518
- collapsedDiffFiles.scopeKey === collapseScopeKey
519
- ? collapsedDiffFiles.fileKeys
520
- : defaultCollapsedDiffFileKeys;
512
+ const storedCollapsedDiffFileKeys = useDiffPanelSessionStore((state) =>
513
+ collapseScopeKey ? state.collapsedFileKeysByScopeKey[collapseScopeKey] : undefined,
514
+ );
515
+ const collapsedDiffFileKeys = storedCollapsedDiffFileKeys ?? defaultCollapsedDiffFileKeys;
516
+ const updateCollapsedDiffFiles = (
517
+ update: (current: ReadonlySet<string>) => ReadonlySet<string>,
518
+ ) => {
519
+ if (!collapseScopeKey) return;
520
+ useDiffPanelSessionStore
521
+ .getState()
522
+ .updateCollapsedFileKeys(collapseScopeKey, defaultCollapsedDiffFileKeys, update);
523
+ };
524
const renderLoadingBoundary = () =>
525
settledFileCount < renderableFiles.length ? (
526
<DiffFileLoadingBoundary
@@ -550,12 +553,10 @@ export default function DiffPanel({
553
);
554
const file = renderableFileEntries[index];
555
if (!file) return false;
553
- setCollapsedDiffFiles((current) => {
554
- const next = new Set(
555
- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
556
- );
556
+ updateCollapsedDiffFiles((current) => {
557
+ const next = new Set(current);
558
next.delete(file.fileKey);
558
- return { scopeKey: collapseScopeKey, fileKeys: next };
559
+ return next;
560
});
561
if (lazySource && index >= settledFileCount) {
562
requestFile(index);
@@ -604,41 +605,29 @@ export default function DiffPanel({
605
};
606
// Every file header holds these; a new identity per collapse change would redraw them all.
607
const toggleDiffFileCollapsed = useStableCallback((fileKey: string) => {
607
- setCollapsedDiffFiles((current) => {
608
- const next = new Set(
609
- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
610
- );
608
+ updateCollapsedDiffFiles((current) => {
609
+ const next = new Set(current);
610
if (next.has(fileKey)) {
611
next.delete(fileKey);
612
} else {
613
next.add(fileKey);
614
}
616
- return { scopeKey: collapseScopeKey, fileKeys: next };
615
+ return next;
616
});
617
});
618
// Find can ask again before the unfolded file reaches the viewer, so this must never fold.
619
const unfoldDiffFile = useStableCallback((fileKey: string) => {
621
- setCollapsedDiffFiles((current) => {
622
- const fileKeys =
623
- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys;
624
- if (!fileKeys.has(fileKey)) return current;
625
- const next = new Set(fileKeys);
620
+ updateCollapsedDiffFiles((current) => {
621
+ if (!current.has(fileKey)) return current;
622
+ const next = new Set(current);
623
next.delete(fileKey);
627
- return { scopeKey: collapseScopeKey, fileKeys: next };
624
+ return next;
625
});
626
});
627
628
const toggleDiffFileCollapse = () => {
629
setCodeViewRevision((current) => current + 1);
633
- setCollapsedDiffFiles((current) => {
634
- const currentKeys =
635
- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys;
636
-
637
- return {
638
- scopeKey: collapseScopeKey,
639
- fileKeys: toggleAllDiffFiles(diffFileKeys, currentKeys),
640
- };
641
- });
630
+ updateCollapsedDiffFiles((current) => toggleAllDiffFiles(diffFileKeys, current));
631
};
632
633
const selectTurn = (runId: RunId) => {
apps/web/src/diffPanelStore.test.ts
+71
-5
@@ -1,18 +1,84 @@
1
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2
import { EnvironmentId, ThreadId, RunId } from "@t3tools/contracts";
3
-import { beforeEach, describe, expect, it } from "vite-plus/test";
3
+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5
-import { selectThreadDiffPanelSelection, useDiffPanelStore } from "./diffPanelStore";
5
+import {
6
+ selectThreadDiffPanelSelection,
7
+ useDiffPanelSessionStore,
8
+ useDiffPanelStore,
9
+} from "./diffPanelStore";
10
11
const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));
12
13
describe("diffPanelStore", () => {
10
- beforeEach(() =>
14
+ beforeEach(() => {
15
useDiffPanelStore.setState({
16
byThreadKey: {},
17
branchBaseRefByThreadKey: {},
14
- }),
15
- );
18
+ });
19
+ useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });
20
+ });
21
+
22
+ it("retains fold choices when a panel consumer returns with fresh defaults", () => {
23
+ const scopeKey = "environment-1:thread-1:branch";
24
+ const defaults = new Set(["src/app.ts", "src/index.ts"]);
25
+ useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, defaults, (current) => {
26
+ const next = new Set(current);
27
+ next.delete("src/app.ts");
28
+ return next;
29
+ });
30
+
31
+ expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
32
+ new Set(["src/index.ts"]),
33
+ );
34
+
35
+ useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, new Set(), (current) => {
36
+ const next = new Set(current);
37
+ next.delete("src/index.ts");
38
+ return next;
39
+ });
40
+ expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
41
+ new Set(),
42
+ );
43
+
44
+ useDiffPanelSessionStore
45
+ .getState()
46
+ .updateCollapsedFileKeys(scopeKey, defaults, (current) => current);
47
+ expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
48
+ new Set(),
49
+ );
50
+ expect(defaults).toEqual(new Set(["src/app.ts", "src/index.ts"]));
51
+ });
52
+
53
+ it("persists selection while fold changes and no-op unfolds never write to storage", async () => {
54
+ const { storage, name } = useDiffPanelStore.persist.getOptions();
55
+ if (!storage || !name) throw new Error("Diff panel persistence storage is unavailable.");
56
+ const setItem = vi.spyOn(storage, "setItem");
57
+ try {
58
+ useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/release");
59
+ expect(setItem).toHaveBeenCalledOnce();
60
+ const beforeFolding = await storage.getItem(name);
61
+ expect(beforeFolding).toMatchObject({
62
+ state: { byThreadKey: useDiffPanelStore.getState().byThreadKey },
63
+ });
64
+ setItem.mockClear();
65
+
66
+ const scopeKey = "environment-1:thread-1:branch";
67
+ useDiffPanelSessionStore
68
+ .getState()
69
+ .updateCollapsedFileKeys(scopeKey, new Set(), () => new Set(["src/app.ts"]));
70
+ useDiffPanelSessionStore
71
+ .getState()
72
+ .updateCollapsedFileKeys(scopeKey, new Set(), (current) => current);
73
+
74
+ expect(setItem).not.toHaveBeenCalled();
75
+ const persisted = await storage.getItem(name);
76
+ expect(persisted).toEqual(beforeFolding);
77
+ expect(persisted?.state).not.toHaveProperty("collapsedFileKeysByScopeKey");
78
+ } finally {
79
+ setItem.mockRestore();
80
+ }
81
+ });
82
83
it("defaults each thread to Changes without requiring git status", () => {
84
expect(
apps/web/src/diffPanelStore.ts
+23
@@ -130,6 +130,29 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
130
),
131
);
132
133
+interface DiffPanelSessionStoreState {
134
+ collapsedFileKeysByScopeKey: Record<string, ReadonlySet<string>>;
135
+ updateCollapsedFileKeys: (
136
+ scopeKey: string,
137
+ defaults: ReadonlySet<string>,
138
+ update: (current: ReadonlySet<string>) => ReadonlySet<string>,
139
+ ) => void;
140
+}
141
+
142
+// Session-only so folds survive right-panel switches without changing reload defaults.
143
+export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({
144
+ collapsedFileKeysByScopeKey: {},
145
+ updateCollapsedFileKeys: (scopeKey, defaults, update) =>
146
+ set((state) => {
147
+ const current = state.collapsedFileKeysByScopeKey[scopeKey] ?? defaults;
148
+ const next = update(current);
149
+ if (next === current) return state;
150
+ return {
151
+ collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },
152
+ };
153
+ }),
154
+}));
155
+
156
export function selectThreadDiffPanelSelection(
157
byThreadKey: Record<string, DiffPanelSelection>,
158
ref: ScopedThreadRef | null | undefined,