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 21, 23:30 UTC
fix(web): retain diff selection across navigation (#22347)
Co-authored-by: Lukas Tomondy <lukas.tomondy@moonpyre.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Alessandro Sola committed
Nov 11, 2026 at 10:27 UTC
996b43f01b2bfe3720e2a974940ed963ff144f25
8 files changed
+73
-69
apps/web/src/components/ChatView.browser.test.tsx
+68
-2
@@ -1,5 +1,5 @@
1
import { createMemoryHistory } from "@tanstack/react-router";
2
-import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2
+import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
3
import { ThreadId } from "@t3tools/contracts";
4
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
5
import type * as Context from "effect/Context";
@@ -11,7 +11,9 @@ import { AppRoot } from "~/AppRoot";
11
// takes 6-11s; importing it here keeps that out of the test's timeout.
12
import "~/components/ChatView";
13
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
14
+import { selectThreadDiffPanelSelection, useDiffPanelStore } from "~/diffPanelStore";
15
import { __setClientSettingsForTests } from "~/hooks/useSettings";
16
+import { useRightPanelStore } from "~/rightPanelStore";
17
import { getRouter } from "~/router";
18
import {
19
ENVIRONMENT_ID,
@@ -20,7 +22,7 @@ import {
22
threadWithHistory,
23
type FakeDriver,
24
} from "../../test/fakeEnvironment";
23
-import { render } from "../../test/render";
25
+import { act, render } from "../../test/render";
26
27
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
28
vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
@@ -91,3 +93,67 @@ it("opens the thread actions menu for an older settled thread the shell leaves o
93
.element(page.getByRole("menuitem", { name: "Rename thread" }), { timeout: 5_000 })
94
.toBeVisible();
95
});
96
+
97
+it("retains each thread's diff selection when returning from another thread or Settings", async () => {
98
+ const a = threadWithHistory("diff-selection-a", 1);
99
+ const b = threadWithHistory("diff-selection-b", 1);
100
+ using _environment = installFakeEnvironment([a, b]);
101
+ __setClientSettingsForTests({
102
+ ...DEFAULT_CLIENT_SETTINGS,
103
+ onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
104
+ });
105
+ const aRef = scopeThreadRef(ENVIRONMENT_ID, a.id);
106
+ const bRef = scopeThreadRef(ENVIRONMENT_ID, b.id);
107
+ useRightPanelStore.getState().open(aRef, "diff");
108
+ useRightPanelStore.getState().open(bRef, "diff");
109
+ useDiffPanelStore.getState().selectBranchBaseRef(bRef, "origin/release");
110
+ const router = getRouter(createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${a.id}`] }));
111
+ await router.load();
112
+ await using _app = await render(<AppRoot router={router} />, {
113
+ // AppRoot brings its own router.
114
+ router: false,
115
+ });
116
+ await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
117
+ await act(() => useDiffPanelStore.getState().selectGitScope(aRef, "unstaged"));
118
+
119
+ await act(() =>
120
+ router.navigate({
121
+ to: "/$environmentId/$threadId",
122
+ params: { environmentId: ENVIRONMENT_ID, threadId: b.id },
123
+ }),
124
+ );
125
+ await expect.element(page.getByText(`Answer 0 for ${b.id}.`)).toBeVisible();
126
+ expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, bRef)).toEqual({
127
+ kind: "branch",
128
+ baseRef: "origin/release",
129
+ });
130
+ await act(() =>
131
+ router.navigate({
132
+ to: "/$environmentId/$threadId",
133
+ params: { environmentId: ENVIRONMENT_ID, threadId: a.id },
134
+ }),
135
+ );
136
+ await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
137
+ expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, aRef)).toEqual({
138
+ kind: "unstaged",
139
+ });
140
+
141
+ await act(() => useDiffPanelStore.getState().selectTurn(aRef, a.runs[0]!.id));
142
+ await act(() => router.navigate({ to: "/settings/general" }));
143
+ await expect
144
+ .element(page.getByRole("heading", { name: "Time format", exact: true }))
145
+ .toBeVisible();
146
+ await act(() =>
147
+ router.navigate({
148
+ to: "/$environmentId/$threadId",
149
+ params: { environmentId: ENVIRONMENT_ID, threadId: a.id },
150
+ }),
151
+ );
152
+ await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
153
+ expect(
154
+ selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, aRef),
155
+ ).toMatchObject({
156
+ kind: "turn",
157
+ turnId: a.runs[0]!.id,
158
+ });
159
+});
apps/web/src/components/ChatView.logic.ts
-10
@@ -230,16 +230,6 @@ export function resolveProactiveTurnDiffAction(input: {
230
return totals.count >= 3 || totals.additions + totals.deletions >= 50 ? "open" : "ignore";
231
}
232
233
-/** Mark reactivated Diff tabs to preserve review scope. Already-active tabs must not leave a marker for
234
- * the next open. */
235
-export function resolveExplicitDiffOpenOnActivate(input: {
236
- readonly surfaceKind: RightPanelSurface["kind"];
237
- readonly diffOpen: boolean;
238
- readonly activeThreadRef: ScopedThreadRef;
239
-}): ScopedThreadRef | null {
240
- return input.surfaceKind === "diff" && !input.diffOpen ? input.activeThreadRef : null;
241
-}
242
-
233
export function codexArtifactTemplatePromptToAppend(
234
currentDraft: string,
235
template: CodexArtifactTemplate,
apps/web/src/components/ChatView.tsx
-16
@@ -133,7 +133,6 @@ import {
133
} from "@t3tools/client-runtime/state/runtime";
134
import { AsyncResult } from "effect/reactivity";
135
import { isElectron } from "../env";
136
-import { useDiffPanelStore } from "../diffPanelStore";
136
import { useActiveThreadRef } from "../hooks/useActiveThreadRef";
137
import { composerRequiresModifier } from "../composer-logic";
138
import {
@@ -1031,20 +1030,6 @@ export default function ChatView(props: ChatViewProps) {
1030
selectActiveRightPanel(state.byThreadKey, activeThreadRef),
1031
);
1032
const diffOpen = activeRightPanelKind === "diff";
1034
- const explicitDiffOpenRef = useRef<ScopedThreadRef | null>(null);
1035
- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- resets the diff panel store to branch scope when the diff opens without an explicit request
1036
- useLayoutEffect(() => {
1037
- const explicitThreadRef = explicitDiffOpenRef.current;
1038
- explicitDiffOpenRef.current = null;
1039
- if (
1040
- diffOpen &&
1041
- activeThreadRef &&
1042
- explicitThreadRef !== activeThreadRef &&
1043
- !useDiffPanelStore.getState().consumeKeepSelectionOnOpen(activeThreadRef)
1044
- ) {
1045
- useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");
1046
- }
1047
- }, [activeThreadRef, diffOpen]);
1033
const rightPanelState = useRightPanelStore((state) =>
1034
selectThreadRightPanelState(state.byThreadKey, activeThreadRef),
1035
);
@@ -2374,7 +2359,6 @@ export default function ChatView(props: ChatViewProps) {
2359
pullRequestsSurfaceAvailable,
2360
gitCwd,
2361
activeProjectCwd,
2377
- explicitDiffOpenRef,
2362
onDiffPanelOpen,
2363
closeRightPanelSurface,
2364
});
apps/web/src/components/DiffPanel.browser.test.tsx
-1
@@ -121,7 +121,6 @@ function setup(failedBaseRef: string | null) {
121
useDiffPanelStore.setState({
122
byThreadKey: {},
123
branchBaseRefByThreadKey: {},
124
- keepSelectionOnOpenThreadKey: null,
124
});
125
useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });
126
};
apps/web/src/components/chat/useRightPanelActions.ts
-15
@@ -3,7 +3,6 @@ import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
3
import type { ThreadPreviewState } from "../../previewStateStore";
4
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
5
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
6
-import { resolveExplicitDiffOpenOnActivate } from "../ChatView.logic";
6
import { useStableCallback } from "../../hooks/useStableCallback";
7
import { type ScopedThreadRef, type RunId } from "@t3tools/contracts";
8
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
@@ -15,7 +14,6 @@ import {
14
import { useDiffPanelStore } from "../../diffPanelStore";
15
import { type ThreadPanelPresentation } from "../../rightPanelLayout";
16
import {
18
- selectActiveRightPanel,
17
selectThreadPanelOpen,
18
type RightPanelSurface,
19
useRightPanelStore,
@@ -59,7 +57,6 @@ interface RightPanelActionsInput {
57
readonly pullRequestsSurfaceAvailable: boolean;
58
readonly gitCwd: string | null;
59
readonly activeProjectCwd: string | null;
62
- readonly explicitDiffOpenRef: { current: ScopedThreadRef | null };
60
readonly onDiffPanelOpen: (() => void) | undefined;
61
readonly closeRightPanelSurface: (surface: RightPanelSurface) => void;
62
}
@@ -93,7 +90,6 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
90
pullRequestsSurfaceAvailable,
91
gitCwd,
92
activeProjectCwd,
96
- explicitDiffOpenRef,
93
onDiffPanelOpen,
94
} = input;
95
@@ -201,11 +197,6 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
197
const togglePreviewPanelStable = useStableCallback(togglePreviewPanel);
198
const activateRightPanelSurface = (surface: RightPanelSurface) => {
199
if (!activeThreadRef) return;
204
- explicitDiffOpenRef.current = resolveExplicitDiffOpenOnActivate({
205
- surfaceKind: surface.kind,
206
- diffOpen,
207
- activeThreadRef,
208
- });
200
useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
201
if (surface.kind === "preview" && surface.resourceId) {
202
setActivePreviewTab(activeThreadRef, surface.resourceId);
@@ -299,18 +290,12 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
290
const addPullRequestSurfaceStable = useStableCallback(addPullRequestSurface);
291
const onOpenTurnDiff = (runId: RunId, filePath?: string) => {
292
if (!isServerThread || !activeThreadRef) return;
302
- const diffOpen =
303
- selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
304
- explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
293
useDiffPanelStore.getState().selectTurn(activeThreadRef, runId, filePath);
294
useRightPanelStore.getState().open(activeThreadRef, "diff");
295
onDiffPanelOpen?.();
296
};
297
const onOpenToolDiffStable = useStableCallback((target: ToolCallDiffTarget) => {
298
if (!isServerThread || !activeThreadRef) return;
311
- const diffOpen =
312
- selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
313
- explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
299
useDiffPanelStore.getState().selectToolCall(activeThreadRef, target);
300
useRightPanelStore.getState().open(activeThreadRef, "diff");
301
onDiffPanelOpen?.();
apps/web/src/diffPanelStore.ts
+3
-15
@@ -25,14 +25,11 @@ const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
25
interface DiffPanelStoreState {
26
byThreadKey: Record<string, DiffPanelSelection>;
27
branchBaseRefByThreadKey: Record<string, string | null>;
28
- /** A link that selected a commit before opening the closed pane; opening keeps that selection. */
29
- keepSelectionOnOpenThreadKey: string | null;
28
selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
29
selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
30
selectToolCall: (ref: ScopedThreadRef, target: ToolCallDiffTarget) => void;
31
selectTurn: (ref: ScopedThreadRef, turnId: RunId, filePath?: string) => void;
34
- selectCommit: (ref: ScopedThreadRef, sha: string, keepOnOpen?: boolean) => void;
35
- consumeKeepSelectionOnOpen: (ref: ScopedThreadRef) => boolean;
32
+ selectCommit: (ref: ScopedThreadRef, sha: string) => void;
33
reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
34
removeThread: (ref: ScopedThreadRef) => void;
35
}
@@ -54,10 +51,9 @@ function branchBaseRef(
51
52
export const useDiffPanelStore = create<DiffPanelStoreState>()(
53
persist(
57
- (set, get) => ({
54
+ (set) => ({
55
byThreadKey: {},
56
branchBaseRefByThreadKey: {},
60
- keepSelectionOnOpenThreadKey: null,
57
selectGitScope: (ref, scope) =>
58
set((state) => {
59
const threadKey = scopedThreadKey(ref);
@@ -115,7 +111,7 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
111
},
112
};
113
}),
118
- selectCommit: (ref, sha, keepOnOpen = false) =>
114
+ selectCommit: (ref, sha) =>
115
set((state) => {
116
const threadKey = scopedThreadKey(ref);
117
const baseRef = branchBaseRef(state, threadKey);
@@ -125,16 +121,8 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
121
[threadKey]: { kind: "commit", sha, baseRef },
122
},
123
branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [threadKey]: baseRef },
128
- keepSelectionOnOpenThreadKey: keepOnOpen
129
- ? threadKey
130
- : state.keepSelectionOnOpenThreadKey,
124
};
125
}),
133
- consumeKeepSelectionOnOpen: (ref) => {
134
- if (get().keepSelectionOnOpenThreadKey !== scopedThreadKey(ref)) return false;
135
- set({ keepSelectionOnOpenThreadKey: null });
136
- return true;
137
- },
126
reconcileTurnSelection: (ref, availableTurnIds) =>
127
set((state) => {
128
const threadKey = scopedThreadKey(ref);
apps/web/src/lib/openPullRequestLink.browser.test.tsx
-3
@@ -76,7 +76,6 @@ function setup() {
76
useDiffPanelStore.setState({
77
byThreadKey: {},
78
branchBaseRefByThreadKey: {},
79
- keepSelectionOnOpenThreadKey: null,
79
});
80
useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
81
}
@@ -157,6 +156,4 @@ it("opens an available commit in the closed Diff pane without the pane resetting
156
[scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: null },
157
}),
158
);
160
- // ChatView resets a pane opened without an explicit request to Changes unless this is set.
161
- expect(useDiffPanelStore.getState().consumeKeepSelectionOnOpen(threadRef)).toBe(true);
159
});
apps/web/src/lib/openPullRequestLink.ts
+2
-7
@@ -15,7 +15,7 @@ import { canOpenLinksInApp } from "../browser/browserLinkTarget";
15
import { useOpenLink } from "../browser/useOpenLink";
16
import { useClientSettings } from "../hooks/useSettings";
17
import { stackedThreadToast, toastManager } from "../components/ui/toast";
18
-import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
18
+import { useRightPanelStore } from "../rightPanelStore";
19
import {
20
selectThreadBranchBaseRef,
21
selectThreadDiffPanelSelection,
@@ -199,12 +199,7 @@ export function useOpenChangeRequestLink(
199
(source) => source.kind === "commit" && source.headRef === commit.sha,
200
)
201
) {
202
- const diffOpen =
203
- selectActiveRightPanel(
204
- useRightPanelStore.getState().byThreadKey,
205
- resolvedThreadRef,
206
- ) === "diff";
207
- useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha, !diffOpen);
202
+ useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha);
203
useRightPanelStore.getState().open(resolvedThreadRef, "diff");
204
return;
205
}