Skip to content

Commit 1ff1ebf

Browse files
refactor(web): pull request views record answers and refresh by revision
Pull request pages and panels record shared summaries, snapshots and guide watches from query answers, refetch when a host revision changes, and keep files-viewed writes in a scope-keyed store instead of chaining effects on tokens and counters. Effect categories removed: - shared summary, list, detail snapshot and running-guide mirrors moved into query answer handlers - window and document key listeners moved to dispatcher scopes (detail, page, guide tour, list search, close-sweep Escape) - sentinels, overflow, condense and stats visibility observers moved to ref callbacks - values reported back to parents derived in render (selected commit, stats batches, tab fallback) - refresh-token and revalidation-counter effects replaced by reads keyed on the pull request's host revision - per-watch guide run components replaced by an app-lifetime poller - files-viewed mirror and scope-leave effects moved into a scoped store - code tab file reveals became requests that pend until the file loads Bugs fixed: - A review guide seen as Running reported the previous run's failure as a toast or desktop notification. - A selected commit missing from a still-loading list was reset to "all commits" permanently. - Comment overflow was measured after paint, so the fade and button popped in. - A guide open request with review guides disabled looped between tabs. - A files-viewed answer for a scope that was left and re-entered was applied to the new visit. Kept effects: - Linked-selection panel open: the URL changes through reload and back/forward and resolves against late project data. - Guide tab maximize on show: needs the page's maximized state in a store. - Close-sweep sensor cancel. Temporary compat shims (removed in a later layer of this stack): - components/pullRequest/guide/PullRequestGuideRunCoordinator.tsx: final startGuideRunNotifications plus a compat PullRequestGuideRunCoordinator component that the base __root mounts until appInit starts it in the web-chat-view layer; guideRunStore's final shape removes the state the base coordinator used (removed in layer 10) - hooks/useSettings.ts: starts model-preference migration at module load until appInit starts it in the app-bootstrap layer (the final module drops useMigrateModelPreferences) (removed in layer 10) - components/diffs/useCodeViewFileReveal.ts: final hook plus a retry effect for the base DiffPanel's (viewer, scope, readyFileKeys) call, so DiffPanel's reveal still waits for the file to render until DiffPanel lands in the web-files layer; the PR code tab needs the final hook (removed in layer 07) Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9648c2d commit 1ff1ebf

34 files changed

Lines changed: 2436 additions & 1217 deletions

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

Lines changed: 88 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ import {
4242
type MouseEvent as ReactMouseEvent,
4343
type ReactElement,
4444
type ReactNode,
45-
useEffect,
46-
useEffectEvent,
4745
useRef,
4846
useState,
4947
} from "react";
@@ -53,6 +51,7 @@ import type { DesktopPreviewOverlay } from "~/previewStateStore";
5351
import type { RightPanelSurface } from "~/rightPanelStore";
5452
import { cn } from "~/lib/utils";
5553
import { resolveShortcutCommand, type ShortcutMatchContext } from "~/keybindings";
54+
import { registerCommands } from "~/shortcuts/dispatcher";
5655
import { readLocalApi } from "~/localApi";
5756
import { useStableCallback } from "~/hooks/useStableCallback";
5857
import { Button } from "~/components/ui/button";
@@ -204,6 +203,13 @@ function tabScrollViewport(root: HTMLDivElement | null): HTMLDivElement | null {
204203
return root?.querySelector<HTMLDivElement>('[data-slot="scroll-area-viewport"]') ?? null;
205204
}
206205

206+
function scrollActiveTabIntoView(within: HTMLElement): void {
207+
const activeTab = within.matches("[data-active-tab='true']")
208+
? within
209+
: within.querySelector<HTMLElement>("[data-active-tab='true']");
210+
activeTab?.scrollIntoView({ block: "nearest", inline: "nearest" });
211+
}
212+
207213
function previewTabIdOf(
208214
surface: RightPanelSurface,
209215
sessions: Readonly<Record<string, PreviewSessionSnapshot>>,
@@ -379,21 +385,27 @@ function RightPanelEmptyState(props: {
379385
const highlightIndex =
380386
availableActions.length === 0 ? -1 : Math.min(highlight, availableActions.length - 1);
381387

382-
const handleShortcut = useEffectEvent((event: KeyboardEvent) => {
383-
const action = surfaceShortcutActionForKey(availableActions, event);
384-
if (!action) return;
385-
if (document.querySelector(LAUNCHER_SHORTCUT_BLOCKING_LAYERS)) return;
386-
const target = event.composedPath()[0] ?? event.target;
387-
if (target instanceof Element && surfaceShortcutTargetsTypingContext(target)) return;
388-
event.preventDefault();
389-
event.stopPropagation();
390-
action.onClick();
391-
});
392-
useEffect(() => {
393-
const handler = (event: KeyboardEvent) => handleShortcut(event);
394-
window.addEventListener("keydown", handler, true);
395-
return () => window.removeEventListener("keydown", handler, true);
396-
}, []);
388+
// Capture phase, so a launcher letter wins over the chat's type-to-focus.
389+
const registerLetterShortcuts = (node: HTMLDivElement | null) => {
390+
if (!node) return;
391+
return registerCommands(
392+
{},
393+
{
394+
capture: true,
395+
onKeyDown: (event) => {
396+
const action = surfaceShortcutActionForKey(availableActions, event);
397+
if (!action) return false;
398+
if (document.querySelector(LAUNCHER_SHORTCUT_BLOCKING_LAYERS)) return false;
399+
const target = event.composedPath()[0] ?? event.target;
400+
if (target instanceof Element && surfaceShortcutTargetsTypingContext(target))
401+
return false;
402+
event.preventDefault();
403+
event.stopPropagation();
404+
action.onClick();
405+
},
406+
},
407+
);
408+
};
397409

398410
const handleKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
399411
if (event.defaultPrevented || event.metaKey || event.ctrlKey || event.altKey) return;
@@ -445,7 +457,7 @@ function RightPanelEmptyState(props: {
445457
"pb-(--workspace-topbar-height)",
446458
)}
447459
>
448-
<div className="w-full max-w-xs py-6">
460+
<div ref={registerLetterShortcuts} className="w-full max-w-xs py-6">
449461
<h3 className="mb-3 text-center font-medium text-foreground text-sm">Open a surface</h3>
450462
<div className="flex flex-col gap-0.5">
451463
{actions.map((action) =>
@@ -734,7 +746,7 @@ function PullRequestSurfaceIcon({
734746
repository: surface.repository,
735747
number: surface.number,
736748
};
737-
const sharedSummary = useSharedPullRequestSummary(resolvedEnvironmentId, reference, null);
749+
const sharedSummary = useSharedPullRequestSummary(resolvedEnvironmentId, reference);
738750
const status = linkedSnapshot ?? newestPullRequestSummary(detail, sharedSummary) ?? seed ?? null;
739751
if (status === null) {
740752
return <PullRequestGlyph.pullRequest className="size-3 shrink-0 text-muted-foreground" />;
@@ -926,7 +938,10 @@ function RightPanelTab(
926938
onAuxClick={(event) => onAuxClick(event, surface)}
927939
onContextMenu={(event) => onContextMenu(event, surface)}
928940
{...listeners}
929-
ref={setNodeRef}
941+
ref={(node: HTMLDivElement | null) => {
942+
setNodeRef(node);
943+
if (node && props.active) scrollActiveTabIntoView(node);
944+
}}
930945
style={{ transform: CSS.Translate.toString(transform), transition }}
931946
className={cn(
932947
"cursor-pointer group/tab flex h-6 max-w-36 shrink-0 items-center gap-0.5 rounded-md pr-2 pl-1.5 text-xs",
@@ -958,32 +973,35 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
958973

959974
if (props.open === false && addSurfaceMenuOpen) setAddSurfaceMenuOpen(false);
960975

961-
const onNewSurfaceKeyDown = useEffectEvent((event: KeyboardEvent) => {
962-
if (event.defaultPrevented || event.isComposing) return;
963-
if (
964-
resolveShortcutCommand(event, props.keybindings, {
965-
context: { ...props.getShortcutContext(), rightPanelOpen: true },
966-
}) !== "rightPanel.new"
967-
)
968-
return;
969-
if (!addSurfaceMenuOpen && document.querySelector(LAUNCHER_SHORTCUT_BLOCKING_LAYERS)) return;
970-
event.preventDefault();
971-
event.stopPropagation();
972-
if (!event.repeat) {
973-
addSurfaceTriggerRef.current?.focus();
974-
setAddSurfaceMenuOpen(true);
975-
}
976-
});
977-
useEffect(() => {
978-
if (props.open === false) return;
979-
document.addEventListener("keydown", onNewSurfaceKeyDown, true);
980-
return () => document.removeEventListener("keydown", onNewSurfaceKeyDown, true);
981-
}, [props.open]);
982-
983-
const updateTabScrollState = () => {
984-
const viewport = tabScrollViewport(tabListRef.current);
985-
if (!viewport) return;
976+
// Capture phase, ahead of focused widgets (the terminal) that consume the chord themselves.
977+
const registerNewSurfaceShortcut = (node: HTMLDivElement | null) => {
978+
if (!node || props.open === false) return;
979+
return registerCommands(
980+
{},
981+
{
982+
capture: true,
983+
onKeyDown: (event) => {
984+
if (event.isComposing) return false;
985+
if (
986+
resolveShortcutCommand(event, props.keybindings, {
987+
context: { ...props.getShortcutContext(), rightPanelOpen: true },
988+
}) !== "rightPanel.new"
989+
)
990+
return false;
991+
if (!addSurfaceMenuOpen && document.querySelector(LAUNCHER_SHORTCUT_BLOCKING_LAYERS))
992+
return false;
993+
event.preventDefault();
994+
event.stopPropagation();
995+
if (!event.repeat) {
996+
addSurfaceTriggerRef.current?.focus();
997+
setAddSurfaceMenuOpen(true);
998+
}
999+
},
1000+
},
1001+
);
1002+
};
9861003

1004+
const updateTabScrollState = (viewport: HTMLDivElement) => {
9871005
const hasOverflow = viewport.scrollWidth - viewport.clientWidth > TAB_SCROLL_EDGE_TOLERANCE;
9881006
const canScrollLeft = hasOverflow && viewport.scrollLeft > TAB_SCROLL_EDGE_TOLERANCE;
9891007
const canScrollRight =
@@ -999,6 +1017,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
9991017
}
10001018
return { hasOverflow, canScrollLeft, canScrollRight };
10011019
});
1020+
return hasOverflow;
10021021
};
10031022

10041023
const scrollTabs = (direction: -1 | 1) => {
@@ -1182,34 +1201,18 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
11821201
setRenamingDevice(null);
11831202
});
11841203

1185-
useEffect(() => {
1186-
if (!props.activeSurfaceId || !tabScrollState.hasOverflow) return;
1187-
const activeTab = tabListRef.current?.querySelector<HTMLElement>("[data-active-tab='true']");
1188-
activeTab?.scrollIntoView({ block: "nearest", inline: "nearest" });
1189-
}, [props.activeSurfaceId, tabScrollState.hasOverflow]);
1190-
1191-
useEffect(() => {
1192-
const viewport = tabScrollViewport(tabListRef.current);
1204+
const attachTabList = (root: HTMLDivElement | null) => {
1205+
tabListRef.current = root;
1206+
const viewport = tabScrollViewport(root);
11931207
if (!viewport) return;
11941208

1195-
const content = viewport.firstElementChild;
1196-
const stopObserving = observeResize(
1197-
content ? [viewport, content] : viewport,
1198-
updateTabScrollState,
1199-
);
1200-
viewport.addEventListener("scroll", updateTabScrollState, { passive: true });
1201-
updateTabScrollState();
1202-
1203-
return () => {
1204-
stopObserving();
1205-
viewport.removeEventListener("scroll", updateTabScrollState);
1209+
let hadOverflow = false;
1210+
const update = () => {
1211+
const hasOverflow = updateTabScrollState(viewport);
1212+
// Tabs that start overflowing may have pushed the active one out of view.
1213+
if (hasOverflow && !hadOverflow) scrollActiveTabIntoView(viewport);
1214+
hadOverflow = hasOverflow;
12061215
};
1207-
}, [updateTabScrollState]);
1208-
1209-
useEffect(() => {
1210-
const viewport = tabScrollViewport(tabListRef.current);
1211-
if (!viewport) return;
1212-
12131216
const handleWheel = (event: WheelEvent) => {
12141217
if (event.ctrlKey) return;
12151218
let delta = Math.abs(event.deltaX) > Math.abs(event.deltaY) ? event.deltaX : event.deltaY;
@@ -1221,12 +1224,22 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
12211224
viewport.scrollLeft += delta;
12221225
if (viewport.scrollLeft === previousScrollLeft) return;
12231226
event.preventDefault();
1224-
updateTabScrollState();
1227+
update();
12251228
};
12261229

1230+
const content = viewport.firstElementChild;
1231+
const stopObserving = observeResize(content ? [viewport, content] : viewport, update);
1232+
viewport.addEventListener("scroll", update, { passive: true });
12271233
viewport.addEventListener("wheel", handleWheel, { passive: false });
1228-
return () => viewport.removeEventListener("wheel", handleWheel);
1229-
}, [updateTabScrollState]);
1234+
update();
1235+
1236+
return () => {
1237+
tabListRef.current = null;
1238+
stopObserving();
1239+
viewport.removeEventListener("scroll", update);
1240+
viewport.removeEventListener("wheel", handleWheel);
1241+
};
1242+
};
12301243

12311244
return (
12321245
<PreviewPanelShell
@@ -1238,6 +1251,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
12381251
{...(props.inlineSize ? { inlineSize: props.inlineSize } : {})}
12391252
>
12401253
<div
1254+
ref={registerNewSurfaceShortcut}
12411255
className={cn(
12421256
"flex h-[var(--workspace-topbar-height)] min-h-[var(--workspace-topbar-height)] shrink-0 items-center gap-1 pl-2",
12431257
props.mode === "inline" && !props.layoutControls ? "pr-28" : "pr-3",
@@ -1249,7 +1263,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
12491263
>
12501264
<ScrollArea
12511265
radius="none"
1252-
ref={tabListRef}
1266+
ref={attachTabList}
12531267
hideScrollbars
12541268
scrollFade
12551269
className="min-w-0 flex-1"

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,14 @@ export function useLinkedThreadPullRequest(
9393
const currentStatus = current === null ? null : linkedPullRequestSnapshotStatus(current);
9494
const fallback =
9595
current === null ? ((!supportsLinks ? linkedPullRequest : null) ?? branchPullRequest) : null;
96-
// Preserve reference identity so sidebar renders do not touch the summary cache.
9796
const reference = fallback == null ? null : linkedPullRequestReference(fallback);
98-
const queried = useEnvironmentQuery(
97+
// Mounted for its answers, which land in the shared summary read below.
98+
useEnvironmentQuery(
9999
!enabled || environmentId === null || reference === null
100100
? null
101101
: linkedPullRequestDetailAtom({ environmentId, input: reference }),
102102
);
103-
const detail = useSharedPullRequestSummary(
104-
environmentId,
105-
reference,
106-
queried.data,
107-
queried.dataUpdatedAt,
108-
);
103+
const detail = useSharedPullRequestSummary(environmentId, reference);
109104

110105
if (current !== null) return currentStatus;
111106
return detail === null
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { CodeViewScrollTarget } from "@pierre/diffs";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { createCodeViewFileReveal } from "./useCodeViewFileReveal";
5+
6+
function viewer(initialItems: ReadonlyArray<string>) {
7+
const items = new Set(initialItems);
8+
const scrolls: CodeViewScrollTarget[] = [];
9+
return {
10+
items,
11+
scrolls,
12+
getInstance: () => ({}),
13+
getItem: (id: string) => (items.has(id) ? {} : undefined),
14+
scrollTo: (target: CodeViewScrollTarget) => scrolls.push(target),
15+
};
16+
}
17+
18+
describe("createCodeViewFileReveal", () => {
19+
it("scrolls at once to a file the viewer holds", () => {
20+
const reveal = createCodeViewFileReveal();
21+
const view = viewer(["a"]);
22+
23+
reveal.request(view, "a");
24+
25+
expect(view.scrolls).toEqual([{ type: "item", id: "a", align: "start" }]);
26+
});
27+
28+
it("keeps a missing file pending until a flush finds it, then drops it", () => {
29+
const reveal = createCodeViewFileReveal();
30+
const view = viewer([]);
31+
32+
reveal.request(view, "a");
33+
reveal.flush(view);
34+
expect(view.scrolls).toEqual([]);
35+
36+
view.items.add("a");
37+
reveal.flush(view);
38+
reveal.flush(view);
39+
expect(view.scrolls).toHaveLength(1);
40+
});
41+
42+
it("drops a pending reveal when another viewer replaces the one it was asked of", () => {
43+
const reveal = createCodeViewFileReveal();
44+
const first = viewer([]);
45+
const second = viewer(["a"]);
46+
47+
reveal.request(first, "a");
48+
reveal.flush(second);
49+
50+
expect(second.scrolls).toEqual([]);
51+
});
52+
});

0 commit comments

Comments
 (0)