1+ import {
2+ parsePullRequestAnchor ,
3+ pullRequestAnchorMatchesUrl ,
4+ pullRequestDiffPathMatches ,
5+ pullRequestLinkCommit ,
6+ } from "@t3tools/shared/pullRequestAnchor" ;
17import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator" ;
28import { Chip } from "../../components/ui/Chip" ;
39import { pullRequestTabOffset } from "./pullRequestTabScroll" ;
@@ -154,8 +160,25 @@ function usePullRequestActivityRows(params: PullRequestParams, filter: ActivityF
154160 } ) ;
155161 return rows ;
156162 } ) ( ) ;
163+ const location = parsePullRequestAnchor ( params . navigationUrl ) ;
164+ const anchor = location ?. kind === "timeline" ? location . anchor : null ;
165+ const targetComment = anchor
166+ ? result . data ?. comments . find ( ( comment ) => pullRequestAnchorMatchesUrl ( anchor , comment . url ) )
167+ : null ;
168+ const targetThread = anchor
169+ ? result . data ?. reviewThreads . find ( ( thread ) =>
170+ thread . comments . some ( ( comment ) => pullRequestAnchorMatchesUrl ( anchor , comment . url ) ) ,
171+ )
172+ : null ;
173+ const targetKey = targetThread
174+ ? `discussion:${ targetThread . id } `
175+ : targetComment
176+ ? `comment:${ targetComment . id } `
177+ : null ;
157178 return {
158179 rows,
180+ targetKey,
181+ targetThread,
159182 hiddenCount : filtered ?. hiddenCount ?? 0 ,
160183 pending : result . isPending && ! result . data ,
161184 error : result . error ,
@@ -334,7 +357,10 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP
334357 const params = route . params ;
335358 usePullRequestEnvironmentFallback ( params ) ;
336359 const connected = usePullRequestConnected ( params . environmentId ) ;
337- const [ chosenSection , setSection ] = useState < "overview" | "activity" | "guide" > ( "overview" ) ;
360+ const [ chosenSection , setSection ] = useState < "overview" | "activity" | "guide" > ( ( ) => {
361+ const kind = parsePullRequestAnchor ( params . navigationUrl ) ?. kind ;
362+ return kind === "timeline" || kind === "commits" ? "activity" : "overview" ;
363+ } ) ;
338364 const requestedSection = useRef ( chosenSection ) ;
339365 const [ listAnimationsReady , setListAnimationsReady ] = useState ( false ) ;
340366 const [ descriptionExpanded , setDescriptionExpanded ] = useState ( false ) ;
@@ -345,7 +371,14 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP
345371 const shown = ( tab : typeof chosenSection ) =>
346372 ! guidesEnabled && tab === "guide" ? "overview" : tab ;
347373 const section = shown ( chosenSection ) ;
348- const [ activityFilter , setActivityFilter ] = useState < ActivityFilter > ( DEFAULT_ACTIVITY_FILTER ) ;
374+ const [ activityFilter , setActivityFilter ] = useState < ActivityFilter > ( ( ) => {
375+ const kind = parsePullRequestAnchor ( params . navigationUrl ) ?. kind ;
376+ return kind === "timeline"
377+ ? { ...DEFAULT_ACTIVITY_FILTER , bots : true , resolved : true }
378+ : kind === "commits"
379+ ? { ...DEFAULT_ACTIVITY_FILTER , kind : "commits" }
380+ : DEFAULT_ACTIVITY_FILTER ;
381+ } ) ;
349382 const activityContent = usePullRequestActivityRows ( params , activityFilter ) ;
350383 const scrollOffsets = useRef < Record < string , number > > ( { } ) ;
351384 // The offset of the tab last shown, and which tab that was: a tab standing in for a hidden guide
@@ -386,6 +419,58 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP
386419 scrollViews . current [ next ] ?. scrollToOffset ( { offset, animated : false } ) ;
387420 setSection ( next ) ;
388421 } ;
422+ const followLink = useEffectEvent ( ( ) => {
423+ const target = parsePullRequestAnchor ( params . navigationUrl ) ;
424+ if ( target ?. kind === "diff" || target ?. kind === "code" ) {
425+ const commit = pullRequestLinkCommit ( params . navigationUrl ) ;
426+ navigation . navigate ( "PullRequestFile" , {
427+ ...params ,
428+ path : "" ,
429+ ...( commit ? { commit } : { } ) ,
430+ } ) ;
431+ } else if ( target ?. kind === "timeline" ) {
432+ selectSection ( "activity" ) ;
433+ setActivityFilter ( ( filter ) => ( { ...filter , kind : "all" , bots : true , resolved : true } ) ) ;
434+ } else if ( target ?. kind === "commits" ) {
435+ selectSection ( "activity" ) ;
436+ setActivityFilter ( ( filter ) => ( { ...filter , kind : "commits" } ) ) ;
437+ } else if ( target ?. kind === "checks" || target ?. kind === "summary" ) {
438+ selectSection ( "overview" ) ;
439+ }
440+ } ) ;
441+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a link selects its native PR section or diff even when the route is reused
442+ useEffect ( ( ) => {
443+ // oxlint-disable-next-line react/set-state-in-effect -- URL navigation selects the PR section and makes its target visible
444+ followLink ( ) ;
445+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- each clicked URL is a fresh navigation request
446+ } , [ params . navigationUrl , params . navigationId ] ) ;
447+
448+ const activityTargetIndex = activityContent . rows . findIndex (
449+ ( row ) => row . key === activityContent . targetKey ,
450+ ) ;
451+ const openedDiscussion = useRef < string | null > ( null ) ;
452+ const revealActivity = useEffectEvent ( ( ) => {
453+ const key = `${ params . navigationUrl } :${ params . navigationId ?? 0 } ` ;
454+ if ( activityContent . targetThread && openedDiscussion . current !== key ) {
455+ openedDiscussion . current = key ;
456+ navigation . navigate ( "PullRequestDiscussion" , {
457+ ...params ,
458+ discussionId : activityContent . targetThread . id ,
459+ } ) ;
460+ return ;
461+ }
462+ if ( activityTargetIndex >= 0 )
463+ void scrollViews . current . activity ?. scrollToIndex ( {
464+ index : activityTargetIndex + 1 ,
465+ viewPosition : 0.5 ,
466+ animated : false ,
467+ } ) ;
468+ } ) ;
469+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reveals the requested activity once its asynchronous rows arrive
470+ useEffect ( ( ) => {
471+ revealActivity ( ) ;
472+ // oxlint-disable-next-line react/exhaustive-effect-dependencies -- repositions when asynchronous activity resolves the URL target
473+ } , [ params . navigationUrl , params . navigationId , activityTargetIndex ] ) ;
389474 const tabContent = activityContent ;
390475 const filesY = useRef ( 0 ) ;
391476 const descriptionY = useRef ( 0 ) ;
@@ -848,9 +933,15 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
848933 ( detail . data ?? route . params . initialSummary ) ?. provider ,
849934 ) . changeRequest ;
850935 const result = usePullRequestDiff ( route . params , terminology ) ;
936+ const linkAnchor = parsePullRequestAnchor ( route . params . navigationUrl ) ;
937+ const linkPath =
938+ linkAnchor ?. kind === "diff" && result . parsed . kind === "files"
939+ ? result . parsed . files . find ( ( file ) => pullRequestDiffPathMatches ( linkAnchor . digest , file . path ) )
940+ ?. path
941+ : undefined ;
851942 const filter = usePullRequestFileFilter (
852943 result . parsed ,
853- route . params . focus ? route . params . path : undefined ,
944+ linkPath ?? ( route . params . focus ? route . params . path : undefined ) ,
854945 ) ;
855946 const { refresh : refreshDetail } = detail ;
856947 useFocusEffect ( refreshDetail ) ;
@@ -922,15 +1013,30 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
9221013 code : row . content ?? "" ,
9231014 } ) ;
9241015 } ;
925- const [ jumpPath , setJumpPath ] = useState ( route . params . path ) ;
1016+ const [ pickedPath , setJumpPath ] = useState < string | null > ( null ) ;
1017+ const jumpPath = pickedPath ?? linkPath ?? route . params . path ;
9261018 const requestedFile =
9271019 result . parsed . kind === "files"
9281020 ? result . parsed . files . find ( ( file ) => file . path === jumpPath )
9291021 : undefined ;
9301022 const targetFile =
9311023 data . files . find ( ( file ) => file . path === jumpPath ) ??
932- ( ! jumpPath || requestedFile ? data . files [ 0 ] : undefined ) ;
933- const focus = route . params . focus ;
1024+ ( linkAnchor ?. kind === "diff" && ! linkPath
1025+ ? undefined
1026+ : ! jumpPath || requestedFile
1027+ ? data . files [ 0 ]
1028+ : undefined ) ;
1029+ const range = pickedPath === null && linkAnchor ?. kind === "diff" ? linkAnchor . range : null ;
1030+ const focus =
1031+ route . params . focus ??
1032+ ( range
1033+ ? {
1034+ side : range . side === "left" ? ( "old" as const ) : ( "new" as const ) ,
1035+ start : range . start ,
1036+ end : range . end ,
1037+ endSide : range . endSide === "left" ? ( "old" as const ) : ( "new" as const ) ,
1038+ }
1039+ : undefined ) ;
9341040 const focusedRows = guideFocusedRows ( data . rows , targetFile ?. id , focus ) ;
9351041 const focusIndex = focusedRows . length
9361042 ? data . rows . findIndex ( ( row ) => row . id === focusedRows [ 0 ] ?. id )
@@ -941,7 +1047,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) {
9411047 // The filter the current jump was made under. Hiding or showing test files replaces the rows,
9421048 // so a jump made under the other filter has to run again. Native applies the entry
9431049 // destination before its first viewport, so the first one counts as made.
944- const jumpedUnder = useRef < boolean | null > ( filter . hideTestFiles ) ;
1050+ const jumpedUnder = useRef < boolean | null > ( targetFile ? filter . hideTestFiles : null ) ;
9451051 const hasTestFiles =
9461052 result . parsed . kind === "files" &&
9471053 result . parsed . files . some ( ( file ) => isTestChangedFile ( file . path ) ) ;
0 commit comments