TypeScript
fix(web): scope PR title collapse to tab scrollers (#14644)
Adamulek12375fca7d77cd4
diff --git a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsxindex d5f054dadab..4b2a878a5ca 100644--- a/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx+++ b/apps/web/src/components/pullRequest/PullRequestCodeTab.tsx@@ -196,6 +196,7 @@ function getReviewPositionAnchor(position: PullRequestReviewPosition): {* drafted rather than being posted as it is typed.*/function PullRequestCodeTab({+ scrollerRef,environmentId,reference,detail,@@ -208,6 +209,7 @@ function PullRequestCodeTab({onRefresh,refreshToken = 0,}: {+ scrollerRef: (node: HTMLDivElement | null) => void;environmentId: EnvironmentId;reference: PullRequestRef;detail: PullRequestDetailView;@@ -1394,7 +1396,9 @@ function PullRequestCodeTab({const withToolbar = (body: ReactNode) => (<div className="flex h-full min-h-0 flex-col">{toolbar}- <div className="min-h-0 flex-1 overflow-auto">{body}</div>+ <div ref={scrollerRef} className="min-h-0 flex-1 overflow-auto">+ {body}+ </div></div>);@@ -1574,6 +1578,7 @@ function PullRequestCodeTab({// interaction, but its native host outline clips and competes with the focus// indicators on its actual controls.className="@container h-full overflow-auto [scrollbar-gutter:stable]"+ containerRef={scrollerRef}viewerRef={setViewer}items={items}selectedLines={selectedLines}diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.test.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.test.tsxindex 3f192f745fa..176009e1a98 100644--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.test.tsx+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.test.tsx@@ -108,43 +108,49 @@ vi.mock("./PullRequestThreadLinks", () => ({ PullRequestThreadLinks: () => nullvi.mock("./PullRequestSummaryTab", () => ({PullRequestSummaryTab: ({onFixFinding,+ scrollerRef,}: ComponentProps<typeof import("./PullRequestSummaryTab").PullRequestSummaryTab>) => (- <button- onClick={() =>- onFixFinding?.({- kind: "check",- check: { name: "Unit tests", status: "failure", description: "Test failed", url: null },- })- }- >- Fix check- </button>+ <div ref={scrollerRef} data-test-scroll="summary">+ <button+ onClick={() =>+ onFixFinding?.({+ kind: "check",+ check: { name: "Unit tests", status: "failure", description: "Test failed", url: null },+ })+ }+ >+ Fix check+ </button>+ </div>),}));vi.mock("./PullRequestCodeTab", () => ({default: ({onAddToAgentSelection,+ scrollerRef,}: ComponentProps<typeof import("./PullRequestCodeTab").default>) => (- <button- onClick={() =>- onAddToAgentSelection?.({- request: "Fix this line",- comment: {- id: "note-1",- sectionId: "file:a.ts",- sectionTitle: "a.ts",- filePath: "a.ts",- startIndex: 0,- endIndex: 0,- rangeLabel: "L1",- text: "Please fix",- diff: "+broken()",- },- })- }- >- Add to agent- </button>+ <div ref={scrollerRef} data-test-scroll="code">+ <button+ onClick={() =>+ onAddToAgentSelection?.({+ request: "Fix this line",+ comment: {+ id: "note-1",+ sectionId: "file:a.ts",+ sectionTitle: "a.ts",+ filePath: "a.ts",+ startIndex: 0,+ endIndex: 0,+ rangeLabel: "L1",+ text: "Please fix",+ diff: "+broken()",+ },+ })+ }+ >+ Add to agent+ </button>+ </div>),}));@@ -343,3 +349,82 @@ describe.each([}});});++it("changes the title only for the active tab's main scroller", async () => {+ const scrollers = new Map<string, { scrollTop: number; scrollHeight: number }>();+ await act(async () => {+ renderer = create(+ <PullRequestDetailPanel+ environmentId={threadRef.environmentId}+ reference={detail}+ context="page"+ shortcutsEnabled={false}+ getShortcutContext={() => ({+ terminalFocus: false,+ terminalOpen: false,+ previewFocus: false,+ previewOpen: false,+ isWeb: true,+ isDesktop: false,+ })}+ />,+ {+ createNodeMock: (element) => {+ const props = element.props as Record<string, unknown>;+ const name = props["data-test-scroll"] as string | undefined;+ if (name) {+ const scroller = { scrollTop: 0, scrollHeight: 300 };+ scrollers.set(name, scroller);+ return scroller;+ }+ return { scrollHeight: props.inert === false ? 100 : 20 };+ },+ },+ );+ });++ const mainScroll = renderer.root.findByProps({+ className: "relative flex min-h-0 flex-1 flex-col overflow-hidden",+ });+ const titleFold = () => {+ let node = renderer.root.findByType("h1");+ while (typeof node.props.inert !== "boolean") node = node.parent!;+ return node;+ };+ const summary = scrollers.get("summary")!;+ const nested = { scrollTop: 200 };+ await act(async () => mainScroll.props.onScrollCapture({ target: nested }));+ expect(titleFold().props.inert).toBe(false);+ summary.scrollTop = 180;+ nested.scrollTop = 0;+ await act(async () => {+ mainScroll.props.onScrollCapture({ target: summary });+ mainScroll.props.onScrollCapture({ target: nested });+ });+ expect(titleFold().props.inert).toBe(true);+ expect(summary.scrollTop).toBe(100);+ expect(nested.scrollTop).toBe(0);++ await act(async () => mainScroll.props.onScrollCapture({ target: { scrollTop: 0 } }));+ expect(titleFold().props.inert).toBe(true);+ summary.scrollTop = 0;+ await act(async () => mainScroll.props.onScrollCapture({ target: summary }));+ expect(titleFold().props.inert).toBe(false);++ await click("Code");+ const code = scrollers.get("code")!;+ summary.scrollTop = 180;+ await act(async () => mainScroll.props.onScrollCapture({ target: summary }));+ expect(titleFold().props.inert).toBe(false);+ code.scrollTop = 180;+ await act(async () => mainScroll.props.onScrollCapture({ target: code }));+ expect(titleFold().props.inert).toBe(true);+ expect(code.scrollTop).toBe(100);+ await click("Summary");+ expect(titleFold().props.inert).toBe(false);+ await click("Code");+ expect(titleFold().props.inert).toBe(true);+ code.scrollTop = 0;+ await act(async () => mainScroll.props.onScrollCapture({ target: code }));+ expect(titleFold().props.inert).toBe(false);+});diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsxindex e977c302be3..526a1ae9bfb 100644--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx@@ -526,18 +526,44 @@ export function PullRequestDetailPanel({setChromeCondensed(chromeStateByTab.current[tab] ?? false);}, [tab]);const condensed = chromeCondensed;- const scrollerRef = useRef<HTMLElement | null>(null);+ const scrollerByTab = useRef<Partial<Record<DetailTab, HTMLDivElement | null>>>({});+ const scrollerRefs = useMemo(+ () => ({+ summary: (node: HTMLDivElement | null) => {+ scrollerByTab.current.summary = node;+ },+ timeline: (node: HTMLDivElement | null) => {+ scrollerByTab.current.timeline = node;+ },+ code: (node: HTMLDivElement | null) => {+ scrollerByTab.current.code = node;+ },+ }),+ [],+ );const foldRef = useRef<HTMLDivElement | null>(null);const condensedRowRef = useRef<HTMLDivElement | null>(null);// Refund after the fold commits so the content under the reader does not jump with its height.- const compensationRef = useRef<number | null>(null);+ const compensationRef = useRef<{+ tab: DetailTab;+ scroller: HTMLDivElement;+ delta: number;+ } | null>(null);useLayoutEffect(() => {- if (compensationRef.current === null) return;- const scroller = scrollerRef.current;- const delta = compensationRef.current;+ const compensation = compensationRef.current;+ if (compensation === null) return;compensationRef.current = null;- if (scroller) scroller.scrollTop = Math.max(0, scroller.scrollTop + delta);- }, [condensed]);+ if (+ !condensed ||+ compensation.tab !== tab ||+ scrollerByTab.current[tab] !== compensation.scroller+ )+ return;+ compensation.scroller.scrollTop = Math.max(+ 0,+ compensation.scroller.scrollTop + compensation.delta,+ );+ }, [condensed, tab]);const lastSelectedMergeMethod = useUiStateStore((state) => state.pullRequestMergeMethod);const setLastSelectedMergeMethod = useUiStateStore((state) => state.setPullRequestMergeMethod);const resolveProjectDefaultMergeMethod = usePullRequestDefaultMergeMethodResolver(@@ -2737,8 +2763,8 @@ export function PullRequestDetailPanel({<divclassName="relative flex min-h-0 flex-1 flex-col overflow-hidden"onScrollCapture={(event) => {- const scroller = event.target as HTMLElement;- scrollerRef.current = scroller;+ const scroller = scrollerByTab.current[tab];+ if (event.target !== scroller || scroller === null || scroller === undefined) return;const top = scroller.scrollTop;setChromeCondensed((previous) => {let next = previous;@@ -2753,7 +2779,7 @@ export function PullRequestDetailPanel({next = false;}} else if (foldHeight > 0 && top > foldHeight + 32) {- compensationRef.current = -chromeDelta;+ compensationRef.current = { tab, scroller, delta: -chromeDelta };next = true;}chromeStateByTab.current[tab] = next;@@ -2779,6 +2805,7 @@ export function PullRequestDetailPanel({{mountedTabs.has("summary") ? (<div className={cn("absolute inset-0", tab !== "summary" && "invisible")}><PullRequestSummaryTab+ scrollerRef={scrollerRefs.summary}environmentId={environmentId}threadRef={threadRef}reference={reference}@@ -2806,6 +2833,7 @@ export function PullRequestDetailPanel({/>) : (<PullRequestTimelineTab+ scrollerRef={scrollerRefs.timeline}detail={detail}environmentId={environmentId}threadRef={threadRef}@@ -2821,6 +2849,7 @@ export function PullRequestDetailPanel({<div className={cn("absolute inset-0", tab !== "code" && "invisible")}><Suspense fallback={<DiffPanelLoadingState label="Loading pull request diff..." />}><PullRequestCodeTab+ scrollerRef={scrollerRefs.code}onAddToAgentSelection={addSelectionToAgent}environmentId={environmentId}reference={reference}diff --git a/apps/web/src/components/pullRequest/PullRequestSummaryTab.test.tsx b/apps/web/src/components/pullRequest/PullRequestSummaryTab.test.tsxindex 1362eb51d12..5c4ee4751bb 100644--- a/apps/web/src/components/pullRequest/PullRequestSummaryTab.test.tsx+++ b/apps/web/src/components/pullRequest/PullRequestSummaryTab.test.tsx@@ -101,6 +101,7 @@ afterEach(() => {function render(value = detail) {return (<PullRequestSummaryTab+ scrollerRef={() => {}}environmentId={EnvironmentId.make("environment")}threadRef={null}reference={value}diff --git a/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx b/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsxindex bfda1209f9c..2b3de089310 100644--- a/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx+++ b/apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx@@ -376,6 +376,7 @@ const CommentFilterSchema = Schema.Struct({ bots: Schema.Boolean, resolved: Scheconst DEFAULT_COMMENT_FILTER: typeof CommentFilterSchema.Type = { bots: false, resolved: false };export function PullRequestSummaryTab({+ scrollerRef,environmentId,threadRef,reference,@@ -390,6 +391,7 @@ export function PullRequestSummaryTab({onRefresh,onRefreshChecks = onRefresh,}: {+ scrollerRef: (node: HTMLDivElement | null) => void;environmentId: EnvironmentId;threadRef: ScopedThreadRef | null;reference: PullRequestRef;@@ -657,7 +659,7 @@ export function PullRequestSummaryTab({};return (- <div className="h-full overflow-y-auto" data-pull-request-summary-scroll>+ <div ref={scrollerRef} className="h-full overflow-y-auto" data-pull-request-summary-scroll><section className="px-4 pt-2.5 pb-1"><div className="space-y-2"><MetaRow icon={<UsersIcon className="size-3.5" />} label="Reviewers">diff --git a/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx b/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsxindex 69421fefbbc..b45bd73a7ea 100644--- a/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx+++ b/apps/web/src/components/pullRequest/PullRequestTimelineTab.tsx@@ -538,6 +538,7 @@ function ReviewVerdictEvent({}export function PullRequestTimelineTab({+ scrollerRef,detail,environmentId,threadRef = null,@@ -546,6 +547,7 @@ export function PullRequestTimelineTab({onOpenCommit,onRefresh,}: {+ scrollerRef: (node: HTMLDivElement | null) => void;detail: PullRequestDetailView;environmentId: EnvironmentId;threadRef?: ScopedThreadRef | null;@@ -577,7 +579,7 @@ export function PullRequestTimelineTab({};return (- <div className="scrollbar-gutter-both h-full overflow-y-auto px-4 py-5">+ <div ref={scrollerRef} className="scrollbar-gutter-both h-full overflow-y-auto px-4 py-5"><div className="mx-auto max-w-3xl"><div className="relative"><span aria-hidden className="absolute bottom-5 left-[15px] top-1 w-px bg-border/45" />