Skip to content
TypeScript

fix(web): PR code view keeps its scroll when a turn finishes (#13835)

Sameer Rangwala073a20ad1957
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
index 751b34ecdc0..60cfe7b049d 100644
--- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
@@ -252,6 +252,7 @@ function PullRequestCodeTab({
onAddToAgentSelection,
onRefresh,
refreshToken = 0,
+ backgroundRefreshToken = 0,
revealRequest = null,
}: {
scrollerRef: (node: HTMLDivElement | null) => void;
@@ -266,6 +267,7 @@ function PullRequestCodeTab({
onAddToAgentSelection?: (input: PullRequestAgentSelectionInput) => void;
onRefresh: () => void;
refreshToken?: number;
+ backgroundRefreshToken?: number;
revealRequest?: { readonly path: string; readonly nonce: number } | null;
}) {
const { resolvedTheme } = useTheme();
@@ -308,7 +310,7 @@ function PullRequestCodeTab({
const referenceKey = pullRequestReviewKey(reference);
const commit = selectedCommitOid;
- const scopeKey = commit === null ? referenceKey : `${referenceKey}@${commit}`;
+ const scopeKey = JSON.stringify([environmentId, referenceKey, commit]);
// The panel keeps this mounted across pull requests, so an open composer would otherwise
// survive the switch and attach its comment to whichever one is on screen when it is sent.
const [stateScopeKey, setStateScopeKey] = useState(scopeKey);
@@ -432,6 +434,15 @@ function PullRequestCodeTab({
refreshFirstDiffPage();
refreshFilesViewed();
}, [refreshToken, scopeKey, refreshFirstDiffPage, refreshFilesViewed]);
+ const appliedBackgroundRefreshToken = useRef(backgroundRefreshToken);
+ useEffect(() => {
+ if (appliedBackgroundRefreshToken.current === backgroundRefreshToken) return;
+ appliedBackgroundRefreshToken.current = backgroundRefreshToken;
+ // Keeping slices mounted preserves scroll; the page merge invalidates changed pages.
+ setSliceState((previous) => ({ ...previous, cursor: null }));
+ refreshFirstDiffPage();
+ refreshFilesViewed();
+ }, [backgroundRefreshToken, refreshFirstDiffPage, refreshFilesViewed]);
// The sentinel is held as state rather than a ref because the viewer mounts its own footer:
// an effect reading a ref could run before that node exists and would never arm the observer.
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index 77f92ec836a..50626f0451f 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -831,7 +831,6 @@ export function PullRequestDetailPanel({
nativeStackQuery.refresh();
};
const [refreshToken, setRefreshToken] = useState(0);
- const codeRefreshToken = refreshToken + (turnRefresh ?? 0);
const [activityRevision, setActivityRevision] = useState<{
readonly key: string;
readonly updatedAt: string;
@@ -2878,7 +2877,8 @@ export function PullRequestDetailPanel({
fixFindingLabel={handoffLabels.fixFinding}
{...(canFixFindings ? { onFixFinding: startFixFinding } : {})}
onRefresh={refreshDetail}
- refreshToken={codeRefreshToken}
+ refreshToken={refreshToken}
+ backgroundRefreshToken={turnRefresh ?? 0}
revealRequest={codeRevealRequest}
/>
</Suspense>