Skip to content

Commit 996b43f

Browse files
aleo113Lukas Tomondyjuliusmarminge
authored
fix(web): retain diff selection across navigation (pingdotgg#22347)
Co-authored-by: Lukas Tomondy <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent e2a7814 commit 996b43f

8 files changed

Lines changed: 73 additions & 69 deletions

‎apps/web/src/components/ChatView.browser.test.tsx‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createMemoryHistory } from "@tanstack/react-router";
2-
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2+
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
33
import { ThreadId } from "@t3tools/contracts";
44
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
55
import type * as Context from "effect/Context";
@@ -11,7 +11,9 @@ import { AppRoot } from "~/AppRoot";
1111
// takes 6-11s; importing it here keeps that out of the test's timeout.
1212
import "~/components/ChatView";
1313
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
14+
import { selectThreadDiffPanelSelection, useDiffPanelStore } from "~/diffPanelStore";
1415
import { __setClientSettingsForTests } from "~/hooks/useSettings";
16+
import { useRightPanelStore } from "~/rightPanelStore";
1517
import { getRouter } from "~/router";
1618
import {
1719
ENVIRONMENT_ID,
@@ -20,7 +22,7 @@ import {
2022
threadWithHistory,
2123
type FakeDriver,
2224
} from "../../test/fakeEnvironment";
23-
import { render } from "../../test/render";
25+
import { act, render } from "../../test/render";
2426

2527
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
2628
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
9193
.element(page.getByRole("menuitem", { name: "Rename thread" }), { timeout: 5_000 })
9294
.toBeVisible();
9395
});
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‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,6 @@ export function resolveProactiveTurnDiffAction(input: {
230230
return totals.count >= 3 || totals.additions + totals.deletions >= 50 ? "open" : "ignore";
231231
}
232232

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-
243233
export function codexArtifactTemplatePromptToAppend(
244234
currentDraft: string,
245235
template: CodexArtifactTemplate,

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ import {
133133
} from "@t3tools/client-runtime/state/runtime";
134134
import { AsyncResult } from "effect/reactivity";
135135
import { isElectron } from "../env";
136-
import { useDiffPanelStore } from "../diffPanelStore";
137136
import { useActiveThreadRef } from "../hooks/useActiveThreadRef";
138137
import { composerRequiresModifier } from "../composer-logic";
139138
import {
@@ -1031,20 +1030,6 @@ export default function ChatView(props: ChatViewProps) {
10311030
selectActiveRightPanel(state.byThreadKey, activeThreadRef),
10321031
);
10331032
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]);
10481033
const rightPanelState = useRightPanelStore((state) =>
10491034
selectThreadRightPanelState(state.byThreadKey, activeThreadRef),
10501035
);
@@ -2374,7 +2359,6 @@ export default function ChatView(props: ChatViewProps) {
23742359
pullRequestsSurfaceAvailable,
23752360
gitCwd,
23762361
activeProjectCwd,
2377-
explicitDiffOpenRef,
23782362
onDiffPanelOpen,
23792363
closeRightPanelSurface,
23802364
});

‎apps/web/src/components/DiffPanel.browser.test.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ function setup(failedBaseRef: string | null) {
121121
useDiffPanelStore.setState({
122122
byThreadKey: {},
123123
branchBaseRefByThreadKey: {},
124-
keepSelectionOnOpenThreadKey: null,
125124
});
126125
useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });
127126
};

‎apps/web/src/components/chat/useRightPanelActions.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
33
import type { ThreadPreviewState } from "../../previewStateStore";
44
import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
55
import { resolvePathLinkTarget } from "@t3tools/shared/fileLinks";
6-
import { resolveExplicitDiffOpenOnActivate } from "../ChatView.logic";
76
import { useStableCallback } from "../../hooks/useStableCallback";
87
import { type ScopedThreadRef, type RunId } from "@t3tools/contracts";
98
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
@@ -15,7 +14,6 @@ import {
1514
import { useDiffPanelStore } from "../../diffPanelStore";
1615
import { type ThreadPanelPresentation } from "../../rightPanelLayout";
1716
import {
18-
selectActiveRightPanel,
1917
selectThreadPanelOpen,
2018
type RightPanelSurface,
2119
useRightPanelStore,
@@ -59,7 +57,6 @@ interface RightPanelActionsInput {
5957
readonly pullRequestsSurfaceAvailable: boolean;
6058
readonly gitCwd: string | null;
6159
readonly activeProjectCwd: string | null;
62-
readonly explicitDiffOpenRef: { current: ScopedThreadRef | null };
6360
readonly onDiffPanelOpen: (() => void) | undefined;
6461
readonly closeRightPanelSurface: (surface: RightPanelSurface) => void;
6562
}
@@ -93,7 +90,6 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
9390
pullRequestsSurfaceAvailable,
9491
gitCwd,
9592
activeProjectCwd,
96-
explicitDiffOpenRef,
9793
onDiffPanelOpen,
9894
} = input;
9995

@@ -201,11 +197,6 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
201197
const togglePreviewPanelStable = useStableCallback(togglePreviewPanel);
202198
const activateRightPanelSurface = (surface: RightPanelSurface) => {
203199
if (!activeThreadRef) return;
204-
explicitDiffOpenRef.current = resolveExplicitDiffOpenOnActivate({
205-
surfaceKind: surface.kind,
206-
diffOpen,
207-
activeThreadRef,
208-
});
209200
useRightPanelStore.getState().activateSurface(activeThreadRef, surface.id);
210201
if (surface.kind === "preview" && surface.resourceId) {
211202
setActivePreviewTab(activeThreadRef, surface.resourceId);
@@ -299,18 +290,12 @@ export function useRightPanelActions(input: RightPanelActionsInput) {
299290
const addPullRequestSurfaceStable = useStableCallback(addPullRequestSurface);
300291
const onOpenTurnDiff = (runId: RunId, filePath?: string) => {
301292
if (!isServerThread || !activeThreadRef) return;
302-
const diffOpen =
303-
selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
304-
explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
305293
useDiffPanelStore.getState().selectTurn(activeThreadRef, runId, filePath);
306294
useRightPanelStore.getState().open(activeThreadRef, "diff");
307295
onDiffPanelOpen?.();
308296
};
309297
const onOpenToolDiffStable = useStableCallback((target: ToolCallDiffTarget) => {
310298
if (!isServerThread || !activeThreadRef) return;
311-
const diffOpen =
312-
selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, activeThreadRef) === "diff";
313-
explicitDiffOpenRef.current = diffOpen ? null : activeThreadRef;
314299
useDiffPanelStore.getState().selectToolCall(activeThreadRef, target);
315300
useRightPanelStore.getState().open(activeThreadRef, "diff");
316301
onDiffPanelOpen?.();

‎apps/web/src/diffPanelStore.ts‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
2525
interface DiffPanelStoreState {
2626
byThreadKey: Record<string, DiffPanelSelection>;
2727
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;
3028
selectGitScope: (ref: ScopedThreadRef, scope: "branch" | "unstaged") => void;
3129
selectBranchBaseRef: (ref: ScopedThreadRef, baseRef: string | null) => void;
3230
selectToolCall: (ref: ScopedThreadRef, target: ToolCallDiffTarget) => void;
3331
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;
3633
reconcileTurnSelection: (ref: ScopedThreadRef, availableTurnIds: ReadonlyArray<RunId>) => void;
3734
removeThread: (ref: ScopedThreadRef) => void;
3835
}
@@ -54,10 +51,9 @@ function branchBaseRef(
5451

5552
export const useDiffPanelStore = create<DiffPanelStoreState>()(
5653
persist(
57-
(set, get) => ({
54+
(set) => ({
5855
byThreadKey: {},
5956
branchBaseRefByThreadKey: {},
60-
keepSelectionOnOpenThreadKey: null,
6157
selectGitScope: (ref, scope) =>
6258
set((state) => {
6359
const threadKey = scopedThreadKey(ref);
@@ -115,7 +111,7 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
115111
},
116112
};
117113
}),
118-
selectCommit: (ref, sha, keepOnOpen = false) =>
114+
selectCommit: (ref, sha) =>
119115
set((state) => {
120116
const threadKey = scopedThreadKey(ref);
121117
const baseRef = branchBaseRef(state, threadKey);
@@ -125,16 +121,8 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
125121
[threadKey]: { kind: "commit", sha, baseRef },
126122
},
127123
branchBaseRefByThreadKey: { ...state.branchBaseRefByThreadKey, [threadKey]: baseRef },
128-
keepSelectionOnOpenThreadKey: keepOnOpen
129-
? threadKey
130-
: state.keepSelectionOnOpenThreadKey,
131124
};
132125
}),
133-
consumeKeepSelectionOnOpen: (ref) => {
134-
if (get().keepSelectionOnOpenThreadKey !== scopedThreadKey(ref)) return false;
135-
set({ keepSelectionOnOpenThreadKey: null });
136-
return true;
137-
},
138126
reconcileTurnSelection: (ref, availableTurnIds) =>
139127
set((state) => {
140128
const threadKey = scopedThreadKey(ref);

‎apps/web/src/lib/openPullRequestLink.browser.test.tsx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function setup() {
7676
useDiffPanelStore.setState({
7777
byThreadKey: {},
7878
branchBaseRefByThreadKey: {},
79-
keepSelectionOnOpenThreadKey: null,
8079
});
8180
useRightPanelStore.setState({ byThreadKey: {}, userActionRevisionByThreadKey: {} });
8281
}
@@ -157,6 +156,4 @@ it("opens an available commit in the closed Diff pane without the pane resetting
157156
[scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: null },
158157
}),
159158
);
160-
// ChatView resets a pane opened without an explicit request to Changes unless this is set.
161-
expect(useDiffPanelStore.getState().consumeKeepSelectionOnOpen(threadRef)).toBe(true);
162159
});

‎apps/web/src/lib/openPullRequestLink.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { canOpenLinksInApp } from "../browser/browserLinkTarget";
1515
import { useOpenLink } from "../browser/useOpenLink";
1616
import { useClientSettings } from "../hooks/useSettings";
1717
import { stackedThreadToast, toastManager } from "../components/ui/toast";
18-
import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
18+
import { useRightPanelStore } from "../rightPanelStore";
1919
import {
2020
selectThreadBranchBaseRef,
2121
selectThreadDiffPanelSelection,
@@ -199,12 +199,7 @@ export function useOpenChangeRequestLink(
199199
(source) => source.kind === "commit" && source.headRef === commit.sha,
200200
)
201201
) {
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);
208203
useRightPanelStore.getState().open(resolvedThreadRef, "diff");
209204
return;
210205
}

0 commit comments

Comments
 (0)