Skip to content

Commit 85989a2

Browse files
author
Lukas Tomondy
committed
fix(web): retain diff selection across chat navigation
Adapt the approach from aleo113 in pingdotgg#22347 to main 47f2c8d. Remove the activation reset and the exceptions it required after the right-panel action extraction and commit-link support. Add a regression that mounts the app and navigates between chats and Settings.
1 parent 47f2c8d commit 85989a2

8 files changed

Lines changed: 74 additions & 69 deletions

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

Lines changed: 69 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,15 +11,18 @@ 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,
1820
PROJECT_ID,
1921
installFakeEnvironment,
22+
threadWithHistory,
2023
type FakeDriver,
2124
} from "../../test/fakeEnvironment";
22-
import { render } from "../../test/render";
25+
import { act, render } from "../../test/render";
2326

2427
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
2528
vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
@@ -65,3 +68,67 @@ it("a new draft does not load its reserved thread before the server lists it", a
6568
// A 404 here marks the thread deleted, and the view never loads it once the send creates it.
6669
expect(threadRequests).toEqual([]);
6770
});
71+
72+
it("retains each thread's diff selection when returning from another thread or Settings", async () => {
73+
const a = threadWithHistory("diff-selection-a", 1);
74+
const b = threadWithHistory("diff-selection-b", 1);
75+
using _environment = installFakeEnvironment([a, b]);
76+
__setClientSettingsForTests({
77+
...DEFAULT_CLIENT_SETTINGS,
78+
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
79+
});
80+
const aRef = scopeThreadRef(ENVIRONMENT_ID, a.id);
81+
const bRef = scopeThreadRef(ENVIRONMENT_ID, b.id);
82+
useRightPanelStore.getState().open(aRef, "diff");
83+
useRightPanelStore.getState().open(bRef, "diff");
84+
useDiffPanelStore.getState().selectBranchBaseRef(bRef, "origin/release");
85+
const router = getRouter(createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${a.id}`] }));
86+
await router.load();
87+
await using _app = await render(<AppRoot router={router} />, {
88+
// AppRoot brings its own router.
89+
router: false,
90+
});
91+
await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
92+
await act(() => useDiffPanelStore.getState().selectGitScope(aRef, "unstaged"));
93+
94+
await act(() =>
95+
router.navigate({
96+
to: "/$environmentId/$threadId",
97+
params: { environmentId: ENVIRONMENT_ID, threadId: b.id },
98+
}),
99+
);
100+
await expect.element(page.getByText(`Answer 0 for ${b.id}.`)).toBeVisible();
101+
expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, bRef)).toEqual({
102+
kind: "branch",
103+
baseRef: "origin/release",
104+
});
105+
await act(() =>
106+
router.navigate({
107+
to: "/$environmentId/$threadId",
108+
params: { environmentId: ENVIRONMENT_ID, threadId: a.id },
109+
}),
110+
);
111+
await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
112+
expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, aRef)).toEqual({
113+
kind: "unstaged",
114+
});
115+
116+
await act(() => useDiffPanelStore.getState().selectTurn(aRef, a.runs[0]!.id));
117+
await act(() => router.navigate({ to: "/settings/general" }));
118+
await expect
119+
.element(page.getByRole("heading", { name: "Time format", exact: true }))
120+
.toBeVisible();
121+
await act(() =>
122+
router.navigate({
123+
to: "/$environmentId/$threadId",
124+
params: { environmentId: ENVIRONMENT_ID, threadId: a.id },
125+
}),
126+
);
127+
await expect.element(page.getByText(`Answer 0 for ${a.id}.`)).toBeVisible();
128+
expect(
129+
selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, aRef),
130+
).toMatchObject({
131+
kind: "turn",
132+
turnId: a.runs[0]!.id,
133+
});
134+
});

‎apps/web/src/components/ChatView.logic.ts‎

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

238-
/** Mark reactivated Diff tabs to preserve review scope. Already-active tabs must not leave a marker for
239-
* the next open. */
240-
export function resolveExplicitDiffOpenOnActivate(input: {
241-
readonly surfaceKind: RightPanelSurface["kind"];
242-
readonly diffOpen: boolean;
243-
readonly activeThreadRef: ScopedThreadRef;
244-
}): ScopedThreadRef | null {
245-
return input.surfaceKind === "diff" && !input.diffOpen ? input.activeThreadRef : null;
246-
}
247-
248238
export function codexArtifactTemplatePromptToAppend(
249239
currentDraft: string,
250240
template: CodexArtifactTemplate,

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ import {
125125
} from "@t3tools/client-runtime/state/runtime";
126126
import { AsyncResult } from "effect/reactivity";
127127
import { isElectron } from "../env";
128-
import { useDiffPanelStore } from "../diffPanelStore";
129128
import { useActiveThreadRef } from "../hooks/useActiveThreadRef";
130129
import { composerRequiresModifier } from "../composer-logic";
131130
import {
@@ -1020,20 +1019,6 @@ export default function ChatView(props: ChatViewProps) {
10201019
selectActiveRightPanel(state.byThreadKey, activeThreadRef),
10211020
);
10221021
const diffOpen = activeRightPanelKind === "diff";
1023-
const explicitDiffOpenRef = useRef<ScopedThreadRef | null>(null);
1024-
// 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
1025-
useLayoutEffect(() => {
1026-
const explicitThreadRef = explicitDiffOpenRef.current;
1027-
explicitDiffOpenRef.current = null;
1028-
if (
1029-
diffOpen &&
1030-
activeThreadRef &&
1031-
explicitThreadRef !== activeThreadRef &&
1032-
!useDiffPanelStore.getState().consumeKeepSelectionOnOpen(activeThreadRef)
1033-
) {
1034-
useDiffPanelStore.getState().selectGitScope(activeThreadRef, "branch");
1035-
}
1036-
}, [activeThreadRef, diffOpen]);
10371022
const rightPanelState = useRightPanelStore((state) =>
10381023
selectThreadRightPanelState(state.byThreadKey, activeThreadRef),
10391024
);
@@ -2381,7 +2366,6 @@ export default function ChatView(props: ChatViewProps) {
23812366
pullRequestsSurfaceAvailable,
23822367
gitCwd,
23832368
activeProjectCwd,
2384-
explicitDiffOpenRef,
23852369
onDiffPanelOpen,
23862370
closeRightPanelSurface,
23872371
});

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ function setup(failedBaseRef: string | null) {
111111
useDiffPanelStore.setState({
112112
byThreadKey: {},
113113
branchBaseRefByThreadKey: {},
114-
keepSelectionOnOpenThreadKey: null,
115114
});
116115
useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });
117116
};

‎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
}
@@ -155,6 +154,4 @@ it("opens an available commit in the closed Diff pane without the pane resetting
155154
[scopedThreadKey(threadRef)]: { kind: "commit", sha, baseRef: null },
156155
}),
157156
);
158-
// ChatView resets a pane opened without an explicit request to Changes unless this is set.
159-
expect(useDiffPanelStore.getState().consumeKeepSelectionOnOpen(threadRef)).toBe(true);
160157
});

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { canOpenLinksInApp } from "../browser/browserLinkTarget";
1414
import { useOpenLink } from "../browser/useOpenLink";
1515
import { useClientSettings } from "../hooks/useSettings";
1616
import { stackedThreadToast, toastManager } from "../components/ui/toast";
17-
import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
17+
import { useRightPanelStore } from "../rightPanelStore";
1818
import {
1919
selectThreadBranchBaseRef,
2020
selectThreadDiffPanelSelection,
@@ -183,12 +183,7 @@ export function useOpenChangeRequestLink(
183183
(source) => source.kind === "commit" && source.headRef === commit.sha,
184184
)
185185
) {
186-
const diffOpen =
187-
selectActiveRightPanel(
188-
useRightPanelStore.getState().byThreadKey,
189-
resolvedThreadRef,
190-
) === "diff";
191-
useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha, !diffOpen);
186+
useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha);
192187
useRightPanelStore.getState().open(resolvedThreadRef, "diff");
193188
return;
194189
}

0 commit comments

Comments
 (0)