@@ -29,6 +29,7 @@ import { pullRequestEnvironment } from "~/state/pullRequests";
2929import { useLocalStorage } from "~/hooks/useLocalStorage" ;
3030import { cn } from "~/lib/utils" ;
3131import { useOpenLink } from "~/browser/useOpenLink" ;
32+ import { observeResize } from "~/lib/observeResize" ;
3233import { RelativeTime } from "~/components/RelativeTime" ;
3334
3435import { 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+
335397function 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 >
0 commit comments