TypeScript
fix(web): diff panel keeps collapsed files when switching right-panel tabs (#19533)
Jair Meza31be0c4d5852
diff --git a/apps/web/src/components/DiffPanel.tsx b/apps/web/src/components/DiffPanel.tsxindex 5ca69c77352..761cfc187af 100644--- a/apps/web/src/components/DiffPanel.tsx+++ b/apps/web/src/components/DiffPanel.tsx@@ -29,7 +29,11 @@ import { type DraftId } from "../composerDraftStore";import { openDiffFilePrimaryAction } from "../diffFileActions";import { useCheckpointDiff } from "~/lib/checkpointDiffState";import { cn } from "~/lib/utils";-import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore";+import {+ selectThreadDiffPanelSelection,+ useDiffPanelSessionStore,+ useDiffPanelStore,+} from "../diffPanelStore";import { useLocalStorage } from "../hooks/useLocalStorage";import { useRenderedAppearance } from "../hooks/useTheme";import { useStableCallback } from "../hooks/useStableCallback";@@ -111,11 +115,6 @@ function getCachedFileEntry(fileDiff: FileDiffMetadata) {return entry;}-interface CollapsedDiffFilesState {- readonly scopeKey: string | null;- readonly fileKeys: ReadonlySet<string>;-}-const EMPTY_COLLAPSED_DIFF_FILE_KEYS: ReadonlySet<string> = new Set();function DiffFileCollapseToggle({@@ -214,10 +213,6 @@ export default function DiffPanel({Schema.Boolean,);const [baseRefQuery, setBaseRefQuery] = useState("");- const [collapsedDiffFiles, setCollapsedDiffFiles] = useState<CollapsedDiffFilesState>(() => ({- scopeKey: null,- fileKeys: EMPTY_COLLAPSED_DIFF_FILE_KEYS,- }));const [codeViewRevision, setCodeViewRevision] = useState(0);const [codeView, setCodeView] = useState<AnnotatableCodeViewHandle | null>(null);@@ -514,10 +509,18 @@ export default function DiffPanel({const defaultCollapsedDiffFileKeys = settings.diffFilesCollapsed? new Set(diffFileKeys): EMPTY_COLLAPSED_DIFF_FILE_KEYS;- const collapsedDiffFileKeys =- collapsedDiffFiles.scopeKey === collapseScopeKey- ? collapsedDiffFiles.fileKeys- : defaultCollapsedDiffFileKeys;+ const storedCollapsedDiffFileKeys = useDiffPanelSessionStore((state) =>+ collapseScopeKey ? state.collapsedFileKeysByScopeKey[collapseScopeKey] : undefined,+ );+ const collapsedDiffFileKeys = storedCollapsedDiffFileKeys ?? defaultCollapsedDiffFileKeys;+ const updateCollapsedDiffFiles = (+ update: (current: ReadonlySet<string>) => ReadonlySet<string>,+ ) => {+ if (!collapseScopeKey) return;+ useDiffPanelSessionStore+ .getState()+ .updateCollapsedFileKeys(collapseScopeKey, defaultCollapsedDiffFileKeys, update);+ };const renderLoadingBoundary = () =>settledFileCount < renderableFiles.length ? (<DiffFileLoadingBoundary@@ -550,12 +553,10 @@ export default function DiffPanel({);const file = renderableFileEntries[index];if (!file) return false;- setCollapsedDiffFiles((current) => {- const next = new Set(- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,- );+ updateCollapsedDiffFiles((current) => {+ const next = new Set(current);next.delete(file.fileKey);- return { scopeKey: collapseScopeKey, fileKeys: next };+ return next;});if (lazySource && index >= settledFileCount) {requestFile(index);@@ -604,41 +605,29 @@ export default function DiffPanel({};// Every file header holds these; a new identity per collapse change would redraw them all.const toggleDiffFileCollapsed = useStableCallback((fileKey: string) => {- setCollapsedDiffFiles((current) => {- const next = new Set(- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,- );+ updateCollapsedDiffFiles((current) => {+ const next = new Set(current);if (next.has(fileKey)) {next.delete(fileKey);} else {next.add(fileKey);}- return { scopeKey: collapseScopeKey, fileKeys: next };+ return next;});});// Find can ask again before the unfolded file reaches the viewer, so this must never fold.const unfoldDiffFile = useStableCallback((fileKey: string) => {- setCollapsedDiffFiles((current) => {- const fileKeys =- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys;- if (!fileKeys.has(fileKey)) return current;- const next = new Set(fileKeys);+ updateCollapsedDiffFiles((current) => {+ if (!current.has(fileKey)) return current;+ const next = new Set(current);next.delete(fileKey);- return { scopeKey: collapseScopeKey, fileKeys: next };+ return next;});});const toggleDiffFileCollapse = () => {setCodeViewRevision((current) => current + 1);- setCollapsedDiffFiles((current) => {- const currentKeys =- current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys;-- return {- scopeKey: collapseScopeKey,- fileKeys: toggleAllDiffFiles(diffFileKeys, currentKeys),- };- });+ updateCollapsedDiffFiles((current) => toggleAllDiffFiles(diffFileKeys, current));};const selectTurn = (runId: RunId) => {diff --git a/apps/web/src/diffPanelStore.test.ts b/apps/web/src/diffPanelStore.test.tsindex 737f3cc2d0e..b70cf0b4889 100644--- a/apps/web/src/diffPanelStore.test.ts+++ b/apps/web/src/diffPanelStore.test.ts@@ -1,18 +1,84 @@import { scopeThreadRef } from "@t3tools/client-runtime/environment";import { EnvironmentId, ThreadId, RunId } from "@t3tools/contracts";-import { beforeEach, describe, expect, it } from "vite-plus/test";+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";-import { selectThreadDiffPanelSelection, useDiffPanelStore } from "./diffPanelStore";+import {+ selectThreadDiffPanelSelection,+ useDiffPanelSessionStore,+ useDiffPanelStore,+} from "./diffPanelStore";const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));describe("diffPanelStore", () => {- beforeEach(() =>+ beforeEach(() => {useDiffPanelStore.setState({byThreadKey: {},branchBaseRefByThreadKey: {},- }),- );+ });+ useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });+ });++ it("retains fold choices when a panel consumer returns with fresh defaults", () => {+ const scopeKey = "environment-1:thread-1:branch";+ const defaults = new Set(["src/app.ts", "src/index.ts"]);+ useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, defaults, (current) => {+ const next = new Set(current);+ next.delete("src/app.ts");+ return next;+ });++ expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(+ new Set(["src/index.ts"]),+ );++ useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, new Set(), (current) => {+ const next = new Set(current);+ next.delete("src/index.ts");+ return next;+ });+ expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(+ new Set(),+ );++ useDiffPanelSessionStore+ .getState()+ .updateCollapsedFileKeys(scopeKey, defaults, (current) => current);+ expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(+ new Set(),+ );+ expect(defaults).toEqual(new Set(["src/app.ts", "src/index.ts"]));+ });++ it("persists selection while fold changes and no-op unfolds never write to storage", async () => {+ const { storage, name } = useDiffPanelStore.persist.getOptions();+ if (!storage || !name) throw new Error("Diff panel persistence storage is unavailable.");+ const setItem = vi.spyOn(storage, "setItem");+ try {+ useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/release");+ expect(setItem).toHaveBeenCalledOnce();+ const beforeFolding = await storage.getItem(name);+ expect(beforeFolding).toMatchObject({+ state: { byThreadKey: useDiffPanelStore.getState().byThreadKey },+ });+ setItem.mockClear();++ const scopeKey = "environment-1:thread-1:branch";+ useDiffPanelSessionStore+ .getState()+ .updateCollapsedFileKeys(scopeKey, new Set(), () => new Set(["src/app.ts"]));+ useDiffPanelSessionStore+ .getState()+ .updateCollapsedFileKeys(scopeKey, new Set(), (current) => current);++ expect(setItem).not.toHaveBeenCalled();+ const persisted = await storage.getItem(name);+ expect(persisted).toEqual(beforeFolding);+ expect(persisted?.state).not.toHaveProperty("collapsedFileKeysByScopeKey");+ } finally {+ setItem.mockRestore();+ }+ });it("defaults each thread to Changes without requiring git status", () => {expect(diff --git a/apps/web/src/diffPanelStore.ts b/apps/web/src/diffPanelStore.tsindex a4d5bd274f5..eb6f0e9d455 100644--- a/apps/web/src/diffPanelStore.ts+++ b/apps/web/src/diffPanelStore.ts@@ -130,6 +130,29 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(),);+interface DiffPanelSessionStoreState {+ collapsedFileKeysByScopeKey: Record<string, ReadonlySet<string>>;+ updateCollapsedFileKeys: (+ scopeKey: string,+ defaults: ReadonlySet<string>,+ update: (current: ReadonlySet<string>) => ReadonlySet<string>,+ ) => void;+}++// Session-only so folds survive right-panel switches without changing reload defaults.+export const useDiffPanelSessionStore = create<DiffPanelSessionStoreState>()((set) => ({+ collapsedFileKeysByScopeKey: {},+ updateCollapsedFileKeys: (scopeKey, defaults, update) =>+ set((state) => {+ const current = state.collapsedFileKeysByScopeKey[scopeKey] ?? defaults;+ const next = update(current);+ if (next === current) return state;+ return {+ collapsedFileKeysByScopeKey: { ...state.collapsedFileKeysByScopeKey, [scopeKey]: next },+ };+ }),+}));+export function selectThreadDiffPanelSelection(byThreadKey: Record<string, DiffPanelSelection>,ref: ScopedThreadRef | null | undefined,