Skip to content
TypeScript

fix(web): returning to a thread no longer jumps to its pull request tab (#13608)

Sairajcefe45b7dee7
diff --git a/apps/web/src/hooks/useProactivePanels.ts b/apps/web/src/hooks/useProactivePanels.ts
index aaec22ce6ee..e423afca171 100644
--- a/apps/web/src/hooks/useProactivePanels.ts
+++ b/apps/web/src/hooks/useProactivePanels.ts
@@ -16,6 +16,9 @@ import {
shouldRetargetThreadPullRequestPanel,
} from "../components/ChatView.logic";
+// Settings and drafts unmount ChatView; remember links until the page reloads.
+const observedThreadPullRequests = new Map<string, ThreadLinkedPullRequest | null>();
+
/**
* Opens the right panel on its own when there is something new to look at: a
* change request linked to the thread, or the diff of a turn that just
@@ -65,16 +68,11 @@ export function useProactivePanels(input: {
const proactivePanelObservationRef = useRef<ReturnType<
typeof observeProactivePanelUserChoice
> | null>(null);
- const observedThreadPullRequestRef = useRef<{
- readonly threadKey: string;
- readonly reference: ThreadLinkedPullRequest | null;
- } | null>(null);
const threadKey = threadRef ? scopedThreadKey(threadRef) : null;
useEffect(() => {
if (threadKey === null || threadRef === null) {
proactivePanelObservationRef.current = null;
- observedThreadPullRequestRef.current = null;
return;
}
const panels = useRightPanelStore.getState();
@@ -90,18 +88,16 @@ export function useProactivePanels(input: {
userActionRevision,
} = observation;
const openSurface = selectActiveRightPanelSurface(panels.byThreadKey, threadRef);
- const previousPullRequest = observedThreadPullRequestRef.current;
- observedThreadPullRequestRef.current = {
- threadKey: threadKey,
- reference: linkedThreadPullRequest,
- };
- const followSelectedPullRequest =
- previousPullRequest?.threadKey === threadKey &&
- shouldRetargetThreadPullRequestPanel(
- previousPullRequest.reference,
- linkedThreadPullRequest,
- openSurface,
- );
+ const previousPullRequest = observedThreadPullRequests.get(threadKey) ?? null;
+ // A missing link during loading must not erase the last observed one.
+ if (linkedThreadPullRequest !== null || !threadDetailLoading) {
+ observedThreadPullRequests.set(threadKey, linkedThreadPullRequest);
+ }
+ const followSelectedPullRequest = shouldRetargetThreadPullRequestPanel(
+ previousPullRequest,
+ linkedThreadPullRequest,
+ openSurface,
+ );
// Following the selected linked PR does not open an unrelated panel, so it
// remains available with proactive panels off. It still respects a later choice.
if (followSelectedPullRequest && linkedThreadPullRequest !== null) {
@@ -109,6 +105,7 @@ export function useProactivePanels(input: {
threadRef,
pullRequestSurface(linkedThreadPullRequest),
userActionRevision,
+ true,
);
}
if (!clientSettingsHydrated) return;
@@ -124,6 +121,7 @@ export function useProactivePanels(input: {
};
if (eligibleLink && pullRequestsCapabilityKnown) {
if (
+ !followSelectedPullRequest &&
pullRequestsSurfaceAvailable &&
(visiblePullRequestCount > 1 || !hasLinkedPullRequestDetail || !supportsPullRequests)
) {
diff --git a/apps/web/src/rightPanelStore.test.ts b/apps/web/src/rightPanelStore.test.ts
index 9bcddc3fab2..300f0e4f252 100644
--- a/apps/web/src/rightPanelStore.test.ts
+++ b/apps/web/src/rightPanelStore.test.ts
@@ -302,8 +302,63 @@ describe("rightPanelStore", () => {
});
});
- it("allows automatic panels for a later turn after a manual choice", () => {
+ it.each([
+ {
+ surface: linkedPullRequest,
+ choose: () => useRightPanelStore.getState().openFile(refA, "notes.md"),
+ },
+ {
+ surface: { id: "pull-requests", kind: "pull-requests" } as const,
+ choose: () => useRightPanelStore.getState().openBrowser(refA, "tab-a"),
+ },
+ ])(
+ "preserves the selected tab and hidden panel when $surface.kind already exists",
+ ({ surface, choose }) => {
+ const store = useRightPanelStore.getState();
+ store.openProactive(refA, surface, store.getUserActionRevision(refA));
+ choose();
+ const chosen = selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA);
+
+ // Re-entry captures a fresh revision; the existing tab must still preserve the choice.
+ expect(store.openProactive(refA, surface, store.getUserActionRevision(refA))).toBe(false);
+ expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual(
+ chosen,
+ );
+
+ store.close(refA);
+ expect(store.openProactive(refA, surface, store.getUserActionRevision(refA))).toBe(false);
+ expect(selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA)).toEqual({
+ ...chosen,
+ isOpen: false,
+ });
+ },
+ );
+
+ it("retargets an existing PR tab but respects a later manual choice", () => {
+ const store = useRightPanelStore.getState();
+ const relinkedPullRequest = pullRequestSurface({ ...linkedPullRequest, number: 43 });
+ store.openPullRequest(refA, relinkedPullRequest);
+ store.openPullRequest(refA, linkedPullRequest);
+ const revision = store.getUserActionRevision(refA);
+
+ expect(store.openProactive(refA, relinkedPullRequest, revision, true)).toBe(true);
+ expect(selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, refA)).toEqual(
+ relinkedPullRequest,
+ );
+
+ store.close(refA);
+ expect(store.openProactive(refA, linkedPullRequest, revision, true)).toBe(false);
+ expect(
+ selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, refA),
+ ).toMatchObject({
+ activeSurfaceId: relinkedPullRequest.id,
+ isOpen: false,
+ });
+ });
+
+ it("refocuses an existing diff for a later turn after a manual choice", () => {
const store = useRightPanelStore.getState();
+ store.openProactive(refA, completedDiff, store.getUserActionRevision(refA));
const firstTurnRevision = store.getUserActionRevision(refA);
store.openFile(refA, "src/app.ts");
expect(store.openProactive(refA, completedDiff, firstTurnRevision)).toBe(false);
diff --git a/apps/web/src/rightPanelStore.ts b/apps/web/src/rightPanelStore.ts
index 38251f409e6..8fceb2f4da9 100644
--- a/apps/web/src/rightPanelStore.ts
+++ b/apps/web/src/rightPanelStore.ts
@@ -122,6 +122,7 @@ interface RightPanelStoreState {
| Extract<RightPanelSurface, { kind: "pull-request" | "pull-requests" }>
| { kind: "diff"; turnId: RunId },
expectedUserActionRevision: number,
+ activateExisting?: boolean,
) => boolean;
open: (
ref: ScopedThreadRef,
@@ -600,7 +601,7 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
closeRevisionByThreadKey: {},
getUserActionRevision: (ref) =>
get().userActionRevisionByThreadKey[scopedThreadKey(ref)] ?? 0,
- openProactive: (ref, request, expectedUserActionRevision) => {
+ openProactive: (ref, request, expectedUserActionRevision, activateExisting = false) => {
let opened = false;
set((state) => {
const threadKey = scopedThreadKey(ref);
@@ -626,6 +627,15 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
) {
return state;
}
+ // Keep an existing PR tab where the user left it when revisiting a thread.
+ if (
+ !activateExisting &&
+ selectThreadRightPanelState(state.byThreadKey, ref).surfaces.some(
+ (entry) => entry.id === request.id,
+ )
+ ) {
+ return state;
+ }
opened = true;
return automaticUpdate(state, threadKey, (current) => upsertSurface(current, request));
});