Skip to content

Commit a3abf64

Browse files
committed
fix(web): offer each turn's diff once so a closed diff stays closed
Proactive panels reopened a completed turn's diff on every thread visit because the user-choice revision is re-read on entry and never persisted. Remember the last offered turn in the persisted panel state, keep it when other panel actions rebuild the state, and refuse a turn already offered.
1 parent 6a6ebb2 commit a3abf64

3 files changed

Lines changed: 117 additions & 19 deletions

File tree

‎apps/web/src/hooks/useProactivePanels.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ export function useProactivePanels(input: {
179179
diffAction === "defer" || shouldDeferLink ? previousRunningTurnId : activeRunningTurnId,
180180
};
181181
if (diffAction !== "open" || newlyCompletedTurnId === null) return;
182-
if (!panels.openProactive(threadRef, { id: "diff", kind: "diff" }, userActionRevision)) {
182+
if (
183+
!panels.openProactive(
184+
threadRef,
185+
{ kind: "diff", turnId: newlyCompletedTurnId },
186+
userActionRevision,
187+
)
188+
) {
183189
return;
184190
}
185191
useDiffPanelStore.getState().selectGitScope(threadRef, "branch");

‎apps/web/src/rightPanelStore.test.ts‎

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2-
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { type EnvironmentId, RunId, ThreadId } from "@t3tools/contracts";
33
import { beforeEach, describe, expect, it } from "vite-plus/test";
44

55
import { useClosedViewStore } from "./closedViewStore";
@@ -208,7 +208,7 @@ describe("rightPanelStore", () => {
208208
},
209209
);
210210

211-
const completedDiff = { id: "diff", kind: "diff" } as const;
211+
const completedDiff = { kind: "diff", turnId: RunId.make("turn-1") } as const;
212212
const linkedPullRequest = pullRequestSurface({
213213
projectId: "project-a",
214214
repository: "pingdotgg/t3code",
@@ -281,9 +281,15 @@ describe("rightPanelStore", () => {
281281
expect(
282282
store.openProactive(refA, { id: "pull-requests", kind: "pull-requests" }, revision),
283283
).toBe(false);
284-
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
285-
chosen,
284+
const { isOpen, activeSurfaceId, surfaces } = selectThreadRightPanelState(
285+
useRightPanelStore.getState().byThreadKey,
286+
refA,
286287
);
288+
expect({ isOpen, activeSurfaceId, surfaces }).toEqual({
289+
isOpen: chosen.isOpen,
290+
activeSurfaceId: chosen.activeSurfaceId,
291+
surfaces: chosen.surfaces,
292+
});
287293
});
288294

289295
it("allows automatic panels for a later turn after a manual choice", () => {
@@ -293,10 +299,73 @@ describe("rightPanelStore", () => {
293299
expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
294300

295301
const nextTurnRevision = store.getUserActionRevision(refA);
296-
expect(store.openProactive(refA, completedDiff, nextTurnRevision)).toBe(true);
302+
expect(
303+
store.openProactive(
304+
refA,
305+
{ ...completedDiff, turnId: RunId.make("turn-2") },
306+
nextTurnRevision,
307+
),
308+
).toBe(true);
297309
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
298310
});
299311

312+
it.each([
313+
{ dismissal: "hide", dismiss: () => useRightPanelStore.getState().close(refA) },
314+
{
315+
dismissal: "close tab",
316+
dismiss: () => useRightPanelStore.getState().closeSurface(refA, "diff"),
317+
},
318+
])("offers each turn's diff once after $dismissal and reload", ({ dismiss }) => {
319+
const store = useRightPanelStore.getState();
320+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
321+
dismiss();
322+
const persisted = JSON.parse(
323+
JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
324+
);
325+
useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
326+
327+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
328+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBeNull();
329+
330+
const nextTurn = { ...completedDiff, turnId: RunId.make("turn-2") };
331+
expect(store.openProactive(refA, nextTurn, store.getUserActionRevision(refA))).toBe(true);
332+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("diff");
333+
});
334+
335+
it.each([
336+
{ action: "open file", act: () => useRightPanelStore.getState().openFile(refA, "src/app.ts") },
337+
{ action: "open files", act: () => useRightPanelStore.getState().open(refA, "files") },
338+
{
339+
action: "open pull request",
340+
act: () => useRightPanelStore.getState().openPullRequest(refA, linkedPullRequest),
341+
},
342+
])("keeps the offered turn after dismissing, then $action, then reload", ({ act }) => {
343+
const store = useRightPanelStore.getState();
344+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(true);
345+
store.close(refA);
346+
act();
347+
const persisted = JSON.parse(
348+
JSON.stringify({ byThreadKey: useRightPanelStore.getState().byThreadKey }),
349+
);
350+
useRightPanelStore.setState(migratePersistedRightPanelState(persisted));
351+
const before = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
352+
353+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
354+
expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toBe(
355+
before,
356+
);
357+
});
358+
359+
it("does not retry a diff offer the user already declined", () => {
360+
const store = useRightPanelStore.getState();
361+
const revision = store.getUserActionRevision(refA);
362+
store.openFile(refA, "src/app.ts");
363+
expect(store.openProactive(refA, completedDiff, revision)).toBe(false);
364+
365+
expect(store.openProactive(refA, completedDiff, store.getUserActionRevision(refA))).toBe(false);
366+
expect(selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, refA)).toBe("file");
367+
});
368+
300369
it("keeps manual choices scoped to their thread and environment", () => {
301370
const otherEnvironment = scopeThreadRef("env-2" as EnvironmentId, refA.threadId);
302371
const store = useRightPanelStore.getState();

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
scopedThreadKey,
44
scopeThreadRef,
55
} from "@t3tools/client-runtime/environment";
6-
import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
6+
import { EnvironmentId, RunId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts";
77
import { create } from "zustand";
88
import { createJSONStorage, persist } from "zustand/middleware";
99

@@ -95,13 +95,19 @@ export interface ThreadRightPanelState {
9595
surfaces: RightPanelSurface[];
9696
dismissedDeviceSurfaceIds?: string[];
9797
maximized?: true;
98+
/** The last turn whose diff was offered automatically. Each turn is offered once. */
99+
proactiveDiffTurnId?: RunId;
98100
}
99101

100102
export interface ThreadPanelVisibility {
101103
inlineOpen: boolean;
102104
popoverOpen: boolean;
103105
}
104106

107+
export type ProactivePanelRequest =
108+
| Extract<RightPanelSurface, { kind: "pull-request" | "pull-requests" }>
109+
| { kind: "diff"; turnId: RunId };
110+
105111
interface RightPanelStoreState {
106112
byThreadKey: Record<string, ThreadRightPanelState>;
107113
pendingMaximizeThreadKey: string | null;
@@ -117,7 +123,7 @@ interface RightPanelStoreState {
117123
*/
118124
openProactive: (
119125
ref: ScopedThreadRef,
120-
surface: Extract<RightPanelSurface, { kind: "diff" | "pull-request" | "pull-requests" }>,
126+
request: ProactivePanelRequest,
121127
expectedUserActionRevision: number,
122128
) => boolean;
123129
open: (
@@ -305,12 +311,18 @@ const updateThreadStateMap = (
305311
updater: (current: ThreadRightPanelState) => ThreadRightPanelState,
306312
): Record<string, ThreadRightPanelState> => {
307313
const current = byThreadKey[threadKey] ?? EMPTY_THREAD_STATE;
308-
const next = updater(current);
314+
const updated = updater(current);
315+
// Many actions rebuild the thread state from scratch. Only `openProactive` replaces the offer.
316+
const next =
317+
updated.proactiveDiffTurnId === undefined && current.proactiveDiffTurnId !== undefined
318+
? { ...updated, proactiveDiffTurnId: current.proactiveDiffTurnId }
319+
: updated;
309320
if (
310321
!next.isOpen &&
311322
next.activeSurfaceId === null &&
312323
next.surfaces.length === 0 &&
313-
!next.dismissedDeviceSurfaceIds?.length
324+
!next.dismissedDeviceSurfaceIds?.length &&
325+
next.proactiveDiffTurnId === undefined
314326
) {
315327
if (!(threadKey in byThreadKey)) return byThreadKey;
316328
const { [threadKey]: _removed, ...rest } = byThreadKey;
@@ -545,6 +557,9 @@ export function migratePersistedRightPanelState(persistedState: unknown): {
545557
isOpen,
546558
surfaces,
547559
activeSurfaceId,
560+
...(typeof validThreadState?.proactiveDiffTurnId === "string"
561+
? { proactiveDiffTurnId: RunId.make(validThreadState.proactiveDiffTurnId) }
562+
: {}),
548563
...(Array.isArray(validThreadState?.dismissedDeviceSurfaceIds)
549564
? {
550565
dismissedDeviceSurfaceIds:
@@ -593,24 +608,32 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
593608
closeRevisionByThreadKey: {},
594609
getUserActionRevision: (ref) =>
595610
get().userActionRevisionByThreadKey[scopedThreadKey(ref)] ?? 0,
596-
openProactive: (ref, surface, expectedUserActionRevision) => {
611+
openProactive: (ref, request, expectedUserActionRevision) => {
597612
let opened = false;
598613
set((state) => {
599614
const threadKey = scopedThreadKey(ref);
600-
if (
601-
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
602-
) {
603-
return state;
615+
if (request.kind === "diff") {
616+
const current = selectThreadRightPanelState(state.byThreadKey, ref);
617+
if (current.proactiveDiffTurnId === request.turnId) return state;
618+
const pullRequestActive =
619+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
620+
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests";
621+
opened =
622+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) ===
623+
expectedUserActionRevision && !pullRequestActive;
624+
// Record the offer even when refused, so a revisit does not retry this turn.
625+
return automaticUpdate(state, threadKey, (thread) => ({
626+
...(opened ? upsertSurface(thread, singletonSurface("diff")) : thread),
627+
proactiveDiffTurnId: request.turnId,
628+
}));
604629
}
605630
if (
606-
surface.kind === "diff" &&
607-
(selectActiveRightPanel(state.byThreadKey, ref) === "pull-request" ||
608-
selectActiveRightPanel(state.byThreadKey, ref) === "pull-requests")
631+
(state.userActionRevisionByThreadKey[threadKey] ?? 0) !== expectedUserActionRevision
609632
) {
610633
return state;
611634
}
612635
opened = true;
613-
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, surface));
636+
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, request));
614637
});
615638
return opened;
616639
},

0 commit comments

Comments
 (0)