Skip to content

Commit 8603cfc

Browse files
committed
fix(web): comment links open the pull request panel at the linked comment
A link to a pull request comment (#issuecomment-, #pullrequestreview-, #discussion_r, /files#r, GitLab #note_) opened the PR in the panel but dropped the fragment, so the panel showed the top of the PR. The link handler now hands the panel the clicked link as a one-shot revealUrl on the surface. The panel finds the comment in its loaded conversation, switches to Summary, pages it in, shows it past the comment filter, opens a collapsed section or resolved thread around it without animating, then holds it below the Comments heading while the content settles and marks it. The reveal is cleared once shown, so returning to the tab or reopening a closed one does not scroll again. Fixes #32547
1 parent e455200 commit 8603cfc

9 files changed

Lines changed: 316 additions & 30 deletions

‎apps/web/src/components/chat/RightPanelSurfaceContent.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ export function RightPanelSurfaceContent(props: {
153153
onTabChange={(tab) => {
154154
useRightPanelStore.getState().setPullRequestDetailTab(threadRef, surface.id, tab);
155155
}}
156+
revealUrl={surface.revealUrl}
157+
onRevealSettled={() => {
158+
useRightPanelStore.getState().settlePullRequestReveal(threadRef, surface.id);
159+
}}
156160
context={pullRequestPanelContext(
157161
{
158162
projectId: pullRequest.projectId,

‎apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ import {
126126
isStackedPullRequestBase,
127127
pullRequestActionNeedsHostRefresh,
128128
pullRequestCheckoutCommand,
129+
pullRequestCommentLinkTarget,
130+
pullRequestConversationItems,
129131
pullRequestFindingKey,
130132
pullRequestHandoffLabels,
131133
PULL_REQUEST_MERGE_METHOD_LABELS,
@@ -273,6 +275,8 @@ export function PullRequestDetailPanel({
273275
onGuideShown,
274276
initialTab = "summary",
275277
onTabChange,
278+
revealUrl,
279+
onRevealSettled,
276280
}: {
277281
environmentId: EnvironmentId;
278282
shortcutsEnabled: boolean;
@@ -299,6 +303,9 @@ export function PullRequestDetailPanel({
299303
onGuideShown?: (() => void) | undefined;
300304
initialTab?: PullRequestDetailTab;
301305
onTabChange?: ((tab: PullRequestDetailTab) => void) | undefined;
306+
/** A link that may name one of this pull request's comments, waiting to be shown. */
307+
revealUrl?: string | undefined;
308+
onRevealSettled?: (() => void) | undefined;
302309
}) {
303310
const environmentConfigs = useServerConfigs();
304311
const projects = useProjects();
@@ -356,6 +363,15 @@ export function PullRequestDetailPanel({
356363
detail,
357364
} = usePullRequestDetail(environmentId, reference, matchingListEntry);
358365
const [selectedTab, setTab] = useState<PullRequestDetailTab>(initialTab);
366+
const revealCommentId =
367+
revealUrl === undefined || detail === null
368+
? null
369+
: (pullRequestCommentLinkTarget(
370+
pullRequestConversationItems(detail.comments, detail.reviewThreads),
371+
revealUrl,
372+
)?.commentId ?? null);
373+
// A comment link shows the conversation holding it.
374+
if (revealCommentId !== null && !tabOnPage && selectedTab !== "summary") setTab("summary");
359375
const reviewGuidesEnabled = useEnvironmentSettings(
360376
environmentId,
361377
(settings) => settings.reviewGuidesEnabled,
@@ -561,6 +577,32 @@ export function PullRequestDetailPanel({
561577
: "Checking stack membership…";
562578
const activityPending = activityQuery.isPending && activity === null;
563579
const activityError = activity === null ? activityQuery.error : null;
580+
// Loaded activity without the comment, keyed by load so a refetch is checked again.
581+
const revealMissedAt =
582+
revealUrl !== undefined &&
583+
revealCommentId === null &&
584+
detail !== null &&
585+
!activityQuery.isPending
586+
? activityQuery.dataUpdatedAt
587+
: null;
588+
const revealRefreshedFor = useRef<string | null>(null);
589+
const showRevealedComment = useEffectEvent(() => {
590+
if (revealCommentId === null) {
591+
// Cached activity can predate the comment, so load it once more before giving up.
592+
if (revealUrl !== undefined && revealRefreshedFor.current !== revealUrl) {
593+
revealRefreshedFor.current = revealUrl;
594+
return activityQuery.refresh();
595+
}
596+
return onRevealSettled?.();
597+
}
598+
// selectTab's store writes; the local tab already switched during render.
599+
if (tabOnPage) useGuideRunStore.getState().showPageDetailTab(guideKey, "summary");
600+
onTabChange?.("summary");
601+
});
602+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the tab and the pending link live in stores outside the panel
603+
useEffect(() => {
604+
if (revealCommentId !== null || revealMissedAt !== null) showRevealedComment();
605+
}, [revealCommentId, revealMissedAt]);
564606
const refreshDetail = () => {
565607
detailQuery.refresh();
566608
activityQuery.refresh();
@@ -1751,6 +1793,8 @@ export function PullRequestDetailPanel({
17511793
reference={reference}
17521794
detail={detail}
17531795
activityPending={activityPending}
1796+
revealCommentId={tab === "summary" ? revealCommentId : null}
1797+
{...(onRevealSettled ? { onCommentRevealed: onRevealSettled } : {})}
17541798
checksStale={checksStale}
17551799
activityError={activityError}
17561800
pendingFinding={handoff}

‎apps/web/src/components/pullRequest/PullRequestSummaryTab.tsx‎

Lines changed: 141 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { pullRequestEnvironment } from "~/state/pullRequests";
2929
import { useLocalStorage } from "~/hooks/useLocalStorage";
3030
import { cn } from "~/lib/utils";
3131
import { useOpenLink } from "~/browser/useOpenLink";
32+
import { observeResize } from "~/lib/observeResize";
3233
import { RelativeTime } from "~/components/RelativeTime";
3334

3435
import { Button } from "../ui/button";
@@ -260,6 +261,8 @@ function CollapsedComment({
260261
replies = null,
261262
moreReplies = null,
262263
onAddToChat,
264+
revealCount = 0,
265+
articleProps,
263266
}: {
264267
comment: PullRequestComment;
265268
editing: CommentEditing;
@@ -272,12 +275,27 @@ function CollapsedComment({
272275
replies?: ReactNode;
273276
moreReplies?: ReactNode;
274277
onAddToChat?: (() => void) | undefined;
278+
/** Opens the conversation each time a link reveals a comment in it. */
279+
revealCount?: number;
280+
articleProps?: LinkedArticleProps | undefined;
275281
}) {
276-
const [open, setOpen] = useState(false);
282+
// A reveal opens at once: the opening animation would move the comment after it is scrolled to.
283+
const [open, setOpen] = useState<boolean | "revealed">(revealCount > 0 && "revealed");
284+
const [seenRevealCount, setSeenRevealCount] = useState(revealCount);
285+
if (revealCount !== seenRevealCount) {
286+
setSeenRevealCount(revealCount);
287+
if (revealCount > 0) setOpen("revealed");
288+
}
277289
const statusTriggerRef = useRef<HTMLButtonElement>(null);
278290
return (
279-
<Collapsible open={open} onOpenChange={setOpen}>
280-
<article className="group rounded-lg border border-border/60 [contain-intrinsic-block-size:44px] [content-visibility:auto]">
291+
<Collapsible open={open !== false} onOpenChange={setOpen}>
292+
<article
293+
{...articleProps}
294+
className={cn(
295+
"group rounded-lg border border-border/60 [contain-intrinsic-block-size:44px] [content-visibility:auto]",
296+
articleProps?.className,
297+
)}
298+
>
281299
<div className="p-3">
282300
<div className="flex flex-wrap items-baseline gap-2">
283301
<CommentIdentity comment={comment} detail={detail} />
@@ -316,7 +334,7 @@ function CollapsedComment({
316334
</CollapsibleTrigger>
317335
) : null}
318336
</div>
319-
<CollapsiblePanel>
337+
<CollapsiblePanel animate={open !== "revealed"}>
320338
{open ? (
321339
<div className="px-3 pb-3">
322340
{body === null && !editing.canEdit(comment) ? null : (
@@ -332,6 +350,50 @@ function CollapsedComment({
332350
);
333351
}
334352

353+
interface LinkedArticleProps {
354+
readonly ref: (node: HTMLElement | null) => void;
355+
readonly tabIndex: -1;
356+
readonly "data-linked-comment": true;
357+
readonly className: string;
358+
}
359+
360+
const USER_SCROLL_INTENT = ["wheel", "touchstart", "pointerdown", "keydown"] as const;
361+
362+
/**
363+
* Holds a linked comment just below the sticky Comments heading while the conversations around it
364+
* open and their markdown loads, until the user scrolls or another comment is linked. Only the
365+
* summary scroller moves; scrollIntoView would also scroll the panel's clipped ancestors.
366+
*/
367+
function holdCommentInView(node: HTMLElement) {
368+
const scroller = node.closest<HTMLElement>("[data-pull-request-summary-scroll]");
369+
if (!scroller) return;
370+
const controller = new AbortController();
371+
const stop = () => {
372+
unobserve();
373+
controller.abort();
374+
};
375+
const align = () => {
376+
if (!node.isConnected || !node.hasAttribute("data-linked-comment")) return stop();
377+
const heading = node.closest("[data-pull-request-summary-section]")?.firstElementChild;
378+
const headingHeight = heading?.getBoundingClientRect().height ?? 0;
379+
scroller.scrollTop +=
380+
node.getBoundingClientRect().top - scroller.getBoundingClientRect().top - headingHeight - 8;
381+
};
382+
const unobserve = observeResize([...scroller.children], align);
383+
for (const type of USER_SCROLL_INTENT) {
384+
scroller.addEventListener(type, stop, { passive: true, signal: controller.signal });
385+
}
386+
align();
387+
// Activity can load after the click; by then the user may be typing elsewhere.
388+
const active = document.activeElement;
389+
if (
390+
!(active instanceof HTMLElement) ||
391+
!(active.isContentEditable || active.matches("input, textarea, select"))
392+
) {
393+
node.focus({ preventScroll: true });
394+
}
395+
}
396+
335397
function MetaRow({
336398
icon,
337399
label,
@@ -356,16 +418,25 @@ function Section({
356418
title,
357419
defaultOpen = true,
358420
keepMounted = false,
421+
revealCount = 0,
359422
actions,
360423
children,
361424
}: {
362425
title: string;
363426
defaultOpen?: boolean;
364427
keepMounted?: boolean;
428+
/** Reopens the section each time a link reveals something in it. */
429+
revealCount?: number;
365430
actions?: ReactNode;
366431
children: ReactNode;
367432
}) {
368-
const [open, setOpen] = useState(defaultOpen);
433+
// Opened by a reveal without animating, like a revealed conversation.
434+
const [open, setOpen] = useState<boolean | "revealed">(defaultOpen);
435+
const [seenRevealCount, setSeenRevealCount] = useState(revealCount);
436+
if (revealCount !== seenRevealCount) {
437+
setSeenRevealCount(revealCount);
438+
setOpen("revealed");
439+
}
369440
const headingRef = useRef<HTMLDivElement>(null);
370441
const setOpenWithScrollAnchor = (nextOpen: boolean) => {
371442
if (!nextOpen) {
@@ -387,7 +458,7 @@ function Section({
387458
};
388459
return (
389460
<Collapsible
390-
open={open}
461+
open={open !== false}
391462
onOpenChange={setOpenWithScrollAnchor}
392463
render={<section aria-label={title} />}
393464
data-pull-request-summary-section
@@ -408,7 +479,7 @@ function Section({
408479
</CollapsibleTrigger>
409480
{actions}
410481
</div>
411-
<CollapsiblePanel keepMounted={keepMounted}>
482+
<CollapsiblePanel keepMounted={keepMounted} animate={open !== "revealed"}>
412483
<div className="px-4 pb-4">{children}</div>
413484
</CollapsiblePanel>
414485
</Collapsible>
@@ -431,6 +502,8 @@ export function PullRequestSummaryTab({
431502
checksStale = false,
432503
activityError,
433504
pendingFinding,
505+
revealCommentId = null,
506+
onCommentRevealed,
434507
fixFindingLabel = "Fix in a thread",
435508
fixCheckLabel = "Fix",
436509
onFixFinding,
@@ -447,6 +520,9 @@ export function PullRequestSummaryTab({
447520
checksStale?: boolean;
448521
activityError: string | null;
449522
pendingFinding?: string | null;
523+
/** A linked comment to scroll to and mark, until onCommentRevealed reports it shown. */
524+
revealCommentId?: string | null;
525+
onCommentRevealed?: () => void;
450526
fixFindingLabel?: string;
451527
fixCheckLabel?: string;
452528
onFixFinding?: (finding: PullRequestFinding) => void;
@@ -464,7 +540,23 @@ export function PullRequestSummaryTab({
464540
CommentFilterSchema,
465541
);
466542
const items = pullRequestConversationItems(detail.comments, detail.reviewThreads);
467-
const filteredItems = filterPullRequestConversationItems(items, commentFilter);
543+
// The linked comment stays marked after it is shown, until another link replaces it.
544+
const [linked, setLinked] = useState<{
545+
readonly commentId: string;
546+
readonly revealCount: number;
547+
readonly scrolled: boolean;
548+
} | null>(null);
549+
const [seenRevealCommentId, setSeenRevealCommentId] = useState<string | null>(null);
550+
const linkedCommentId = revealCommentId ?? linked?.commentId ?? null;
551+
const linkedItem =
552+
linkedCommentId === null
553+
? undefined
554+
: items.find((item) =>
555+
pullRequestConversationItemComments(item).some(
556+
(comment) => comment.id === linkedCommentId,
557+
),
558+
);
559+
const filteredItems = filterPullRequestConversationItems(items, commentFilter, linkedItem);
468560
const commentCount = items.reduce(
469561
(count, item) => count + pullRequestConversationItemComments(item).length,
470562
0,
@@ -475,6 +567,38 @@ export function PullRequestSummaryTab({
475567
(count, item) => count + pullRequestConversationItemComments(item).length,
476568
0,
477569
);
570+
if (revealCommentId !== seenRevealCommentId) {
571+
setSeenRevealCommentId(revealCommentId);
572+
if (revealCommentId !== null && linkedItem !== undefined) {
573+
setLinked({
574+
commentId: revealCommentId,
575+
revealCount: (linked?.revealCount ?? 0) + 1,
576+
scrolled: false,
577+
});
578+
// Page in whole pages, so the older-conversations button keeps its usual step.
579+
const fromNewest = filteredItems.length - filteredItems.indexOf(linkedItem);
580+
if (fromNewest > shownConversations) {
581+
setShown({
582+
url: detail.url,
583+
count: Math.ceil(fromNewest / COMMENT_PAGE) * COMMENT_PAGE,
584+
});
585+
}
586+
}
587+
}
588+
const linkedArticleProps = (comment: PullRequestComment): LinkedArticleProps | undefined =>
589+
linked?.commentId === comment.id
590+
? {
591+
ref: (node) => {
592+
if (node === null || linked.scrolled) return;
593+
holdCommentInView(node);
594+
setLinked({ ...linked, scrolled: true });
595+
onCommentRevealed?.();
596+
},
597+
tabIndex: -1,
598+
"data-linked-comment": true,
599+
className: "border-primary/60 ring-1 ring-primary/40 outline-none",
600+
}
601+
: undefined;
478602
const { shown: recentItems, hidden: hiddenConversationCount } = pagePullRequestConversationItems(
479603
filteredItems,
480604
shownConversations,
@@ -643,10 +767,15 @@ export function PullRequestSummaryTab({
643767
: { kind: "thread", thread };
644768
// A reply goes with its conversation, from the card that heads it.
645769
const chat = reply ? null : chatSubject(comment, thread);
770+
const articleProps = linkedArticleProps(comment);
646771
return (
647772
<article
648773
key={`${detail.url}:${comment.id}`}
649-
className="group rounded-lg border border-border/60 bg-background [contain-intrinsic-block-size:160px] [content-visibility:auto]"
774+
{...articleProps}
775+
className={cn(
776+
"group rounded-lg border border-border/60 bg-background [contain-intrinsic-block-size:160px] [content-visibility:auto]",
777+
articleProps?.className,
778+
)}
650779
>
651780
<div className="flex flex-wrap items-baseline gap-2 rounded-t-lg bg-muted/25 px-3 py-2.5">
652781
<div className="flex min-w-0 flex-1 flex-wrap items-baseline gap-2 text-xs text-muted-foreground">
@@ -719,6 +848,8 @@ export function PullRequestSummaryTab({
719848
moreReplies={moreReplies}
720849
onAddToChat={onAddToChat && chat !== null ? () => onAddToChat(chat) : undefined}
721850
reactionBar={reactionBarFor(comment)}
851+
revealCount={item === linkedItem ? (linked?.revealCount ?? 0) : 0}
852+
articleProps={linkedArticleProps(comment)}
722853
/>
723854
);
724855
}
@@ -936,6 +1067,7 @@ export function PullRequestSummaryTab({
9361067

9371068
<Section
9381069
title={`Comments (${detail.commentCount})`}
1070+
revealCount={linked?.revealCount ?? 0}
9391071
actions={
9401072
<div className="flex shrink-0 items-center gap-1">
9411073
<Menu>

‎apps/web/src/components/pullRequest/PullRequestsPage.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,6 +1551,13 @@ export function PullRequestsPage() {
15511551
}
15521552
refreshToken={detailRefreshToken}
15531553
tabOnPage
1554+
revealUrl={renderedPullRequestSurface.revealUrl}
1555+
onRevealSettled={() => {
1556+
if (rightPanelRef === null) return;
1557+
useRightPanelStore
1558+
.getState()
1559+
.settlePullRequestReveal(rightPanelRef, renderedPullRequestSurface.id);
1560+
}}
15541561
onActed={(action, phase = "done", reply) => {
15551562
// From every row held, not the ones on screen: a pull request closed from an open
15561563
// list has left the screen, and reopening it has to find it anyway.

0 commit comments

Comments
 (0)