@@ -50,7 +50,7 @@ import { type PullRequestDetail, type PullRequestListEntry } from "@t3tools/cont |
50 | 50 | import { PullRequestOverviewLoading } from "./PullRequestOverviewLoading"; |
51 | 51 | import { copyPullRequestLink, sharePullRequestLink } from "./pullRequestLink"; |
52 | 52 | import { pullRequestStatus } from "./pullRequestStatus"; |
53 | | -import { use, useEffect, useMemo, useRef, useState } from "react"; |
| 53 | +import { use, useEffect, useEffectEvent, useMemo, useRef, useState } from "react"; |
54 | 54 | import { |
55 | 55 | Linking, |
56 | 56 | Platform, |
@@ -123,6 +123,7 @@ import { PullRequestMergeControls } from "./PullRequestMergeControls"; |
123 | 123 | import { PullRequestStackCard } from "./PullRequestStackCard"; |
124 | 124 | import { usePullRequestStack, type PullRequestStackState } from "./usePullRequestStack"; |
125 | 125 | import { usePullRequestEnvironmentFallback } from "./usePullRequestEnvironmentFallback"; |
| 126 | +import { pullRequestFileJumpScope, subscribePullRequestFileJump } from "./pullRequestFileJump"; |
126 | 127 | import { |
127 | 128 | capitalizeTerm, |
128 | 129 | sourceControlClients, |
@@ -343,16 +344,26 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP |
343 | 344 | const params = route.params; |
344 | 345 | usePullRequestEnvironmentFallback(params); |
345 | 346 | const connected = usePullRequestConnected(params.environmentId); |
346 | | - const [section, setSection] = useState<"overview" | "activity" | "guide">("overview"); |
347 | | - const requestedSection = useRef(section); |
| 347 | + const [chosenSection, setSection] = useState<"overview" | "activity" | "guide">("overview"); |
| 348 | + const requestedSection = useRef(chosenSection); |
348 | 349 | const [listAnimationsReady, setListAnimationsReady] = useState(false); |
349 | 350 | const [descriptionExpanded, setDescriptionExpanded] = useState(false); |
350 | 351 | const filesContent = usePullRequestFilesSection(params); |
351 | 352 | const guidesEnabled = useReviewGuidesEnabled(params); |
| 353 | + // The guide tab goes away while guides are off and overview stands in. The pick is kept, so |
| 354 | + // guides coming back (a settings reload, a reconnect) return to the guide the user chose. |
| 355 | + const shown = (tab: typeof chosenSection) => |
| 356 | + !guidesEnabled && tab === "guide" ? "overview" : tab; |
| 357 | + const section = shown(chosenSection); |
352 | 358 | const [activityFilter, setActivityFilter] = useState<ActivityFilter>(DEFAULT_ACTIVITY_FILTER); |
353 | 359 | const activityContent = usePullRequestActivityRows(params, activityFilter); |
354 | 360 | const scrollOffsets = useRef<Record<string, number>>({}); |
355 | | - const currentScrollOffset = useRef(-insets.top); |
| 361 | + // The offset of the tab last shown, and which tab that was: a tab standing in for a hidden guide |
| 362 | + // has its own offset, which only its next scroll would record here. |
| 363 | + const currentScrollOffset = useRef<{ tab: typeof section; offset: number }>({ |
| 364 | + tab: "overview", |
| 365 | + offset: -insets.top, |
| 366 | + }); |
356 | 367 | const bodyY = useRef(0); |
357 | 368 | const [headerHeight, setHeaderHeight] = useState(0); |
358 | 369 | const [viewportHeight, setViewportHeight] = useState(0); |
@@ -371,27 +382,20 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP |
371 | 382 | }); |
372 | 383 | const selectSection = (next: typeof section) => { |
373 | 384 | if (next === section) return; |
374 | | - scrollOffsets.current[section] = currentScrollOffset.current; |
| 385 | + const from = shown(requestedSection.current); |
| 386 | + const current = |
| 387 | + currentScrollOffset.current.tab === from |
| 388 | + ? currentScrollOffset.current.offset |
| 389 | + : (scrollOffsets.current[from] ?? -insets.top); |
| 390 | + scrollOffsets.current[section] = current; |
375 | 391 | const headerEnd = Math.max(-insets.top, bodyY.current - insets.top); |
376 | | - const offset = pullRequestTabOffset( |
377 | | - currentScrollOffset.current, |
378 | | - scrollOffsets.current[next], |
379 | | - headerEnd, |
380 | | - ); |
| 392 | + const offset = pullRequestTabOffset(current, scrollOffsets.current[next], headerEnd); |
381 | 393 | requestedSection.current = next; |
382 | 394 | scrollOffsets.current[next] = offset; |
383 | | - currentScrollOffset.current = offset; |
| 395 | + currentScrollOffset.current = { tab: next, offset }; |
384 | 396 | scrollViews.current[next]?.scrollToOffset({ offset, animated: false }); |
385 | 397 | setSection(next); |
386 | 398 | }; |
387 | | - useEffect(() => { |
388 | | - if (!guidesEnabled && section === "guide") { |
389 | | - requestedSection.current = "overview"; |
390 | | - currentScrollOffset.current = scrollOffsets.current.overview ?? -insets.top; |
391 | | - // oxlint-disable-next-line react/set-state-in-effect -- When guides turn off, the tab and its saved scroll offset must switch to overview together. |
392 | | - setSection("overview"); |
393 | | - } |
394 | | - }, [guidesEnabled, section, insets.top, currentScrollOffset, requestedSection, scrollOffsets]); |
395 | 399 | const tabContent = activityContent; |
396 | 400 | const filesY = useRef(0); |
397 | 401 | const descriptionY = useRef(0); |
@@ -661,7 +665,8 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP |
661 | 665 | onScroll={(event) => { |
662 | 666 | const offset = event.nativeEvent.contentOffset.y; |
663 | 667 | scrollOffsets.current[tab] = offset; |
664 | | - if (requestedSection.current === tab) currentScrollOffset.current = offset; |
| 668 | + if (shown(requestedSection.current) === tab) |
| 669 | + currentScrollOffset.current = { tab, offset }; |
665 | 670 | }} |
666 | 671 | className="flex-1" |
667 | 672 | renderScrollComponent={ |
@@ -737,7 +742,8 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP |
737 | 742 | filesY.current = event.nativeEvent.layout.y; |
738 | 743 | if (pendingFilesJump.current) { |
739 | 744 | requestAnimationFrame(() => { |
740 | | - if (requestedSection.current === "overview") jumpToFiles(); |
| 745 | + if (shown(requestedSection.current) === "overview") |
| 746 | + jumpToFiles(); |
741 | 747 | }); |
742 | 748 | } |
743 | 749 | }} |
@@ -856,7 +862,6 @@ export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestP |
856 | 862 | type PullRequestFileRouteProps = StaticScreenProps< |
857 | 863 | PullRequestDiffParams & { |
858 | 864 | readonly path: string; |
859 | | - readonly fileSelectionId?: string; |
860 | 865 | readonly replacingDraftId?: string; |
861 | 866 | readonly focus?: GuideDiffFocus; |
862 | 867 | } |
@@ -982,40 +987,35 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { |
982 | 987 | const [collapsedIds, setCollapsedIds] = useState<readonly string[]>(EMPTY_IDS); |
983 | 988 | const [visibleFileId, setVisibleFileId] = useState<string | null>(null); |
984 | 989 | const nativeView = useRef<NativeReviewDiffViewHandle>(null); |
985 | | - // Native applies the entry destination before its first viewport. Imperative |
986 | | - // jumps below are for subsequent selections and filter changes only. |
987 | | - const jumpStarted = useRef(true); |
988 | | - const lastSelection = useRef({ path: route.params.path, id: route.params.fileSelectionId }); |
989 | | - useEffect(() => { |
990 | | - if ( |
991 | | - lastSelection.current.path === route.params.path && |
992 | | - lastSelection.current.id === route.params.fileSelectionId |
993 | | - ) |
994 | | - return; |
995 | | - lastSelection.current = { path: route.params.path, id: route.params.fileSelectionId }; |
996 | | - if (route.params.focus) navigation.setParams({ focus: undefined }); |
997 | | - setJumpPath(route.params.path); |
| 990 | + // The filter the current jump was made under. Hiding or showing test files replaces the rows, |
| 991 | + // so a jump made under the other filter has to run again. Native applies the entry |
| 992 | + // destination before its first viewport, so the first one counts as made. |
| 993 | + const jumpedUnder = useRef<boolean | null>(filter.hideTestFiles); |
| 994 | + const hasTestFiles = |
| 995 | + result.parsed.kind === "files" && |
| 996 | + result.parsed.files.some((file) => isPullRequestTestFile(file.path)); |
| 997 | + const jumpStarted = () => |
| 998 | + jumpedUnder.current !== null && (!hasTestFiles || jumpedUnder.current === filter.hideTestFiles); |
| 999 | + const scrollToFile = (fileId: string) => { |
| 1000 | + if (!nativeView.current) return; |
| 1001 | + jumpedUnder.current = filter.hideTestFiles; |
| 1002 | + void nativeView.current.scrollToFile(fileId, false).catch(() => { |
| 1003 | + jumpedUnder.current = null; |
| 1004 | + }); |
| 1005 | + }; |
| 1006 | + const jumpScope = pullRequestFileJumpScope(route.params); |
| 1007 | + const jumpToFile = useEffectEvent((path: string) => { |
| 1008 | + setJumpPath(path); |
998 | 1009 | setSelectedRowId(null); |
999 | | - jumpStarted.current = false; |
1000 | | - const file = data.files.find((entry) => entry.path === route.params.path); |
1001 | | - if (!file || !nativeView.current) return; |
| 1010 | + jumpedUnder.current = null; |
| 1011 | + const file = data.files.find((entry) => entry.path === path); |
| 1012 | + if (!file) return; |
1002 | 1013 | setCollapsedIds((ids) => ids.filter((id) => id !== file.id)); |
1003 | | - jumpStarted.current = true; |
1004 | | - void nativeView.current.scrollToFile(file.id, false).catch(() => { |
1005 | | - jumpStarted.current = false; |
1006 | | - }); |
1007 | | - }, [route.params.path, route.params.fileSelectionId, route.params.focus, data.files, navigation]); |
1008 | | - const appliedFilter = useRef(filter.hideTestFiles); |
1009 | | - useEffect(() => { |
1010 | | - if (appliedFilter.current === filter.hideTestFiles) return; |
1011 | | - appliedFilter.current = filter.hideTestFiles; |
1012 | | - if ( |
1013 | | - result.parsed.kind === "files" && |
1014 | | - result.parsed.files.some((file) => isPullRequestTestFile(file.path)) |
1015 | | - ) { |
1016 | | - jumpStarted.current = false; |
1017 | | - } |
1018 | | - }, [filter.hideTestFiles, result.parsed]); |
| 1014 | + scrollToFile(file.id); |
| 1015 | + }); |
| 1016 | + // Subscribed for the whole screen, so a pick made while the diff is still loading is kept for |
| 1017 | + // the first draw. |
| 1018 | + useEffect(() => subscribePullRequestFileJump(jumpScope, (path) => jumpToFile(path)), [jumpScope]); |
1019 | 1019 | const { nativeReviewDiffStyle } = useAppearanceCodeSurface(); |
1020 | 1020 | const { themeAppearance, appearance, setCodeWordBreak } = useAppearancePreferences(); |
1021 | 1021 | const bridge = useNativeReviewDiffBridge({ |
@@ -1040,6 +1040,9 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { |
1040 | 1040 | isIos: Platform.OS === "ios", |
1041 | 1041 | nativeView, |
1042 | 1042 | jumpStarted, |
| 1043 | + setJumpStarted: (started) => { |
| 1044 | + jumpedUnder.current = started ? filter.hideTestFiles : null; |
| 1045 | + }, |
1043 | 1046 | onDebug: bridge.onDebug, |
1044 | 1047 | loadMore, |
1045 | 1048 | }); |
@@ -1196,7 +1199,7 @@ function PullRequestFileRouteContent({ route }: PullRequestFileRouteProps) { |
1196 | 1199 | onPress={() => { |
1197 | 1200 | result.refresh(); |
1198 | 1201 | setDiffHead({ scopeKey, sha: detail.data?.headSha ?? null }); |
1199 | | - jumpStarted.current = false; |
| 1202 | + jumpedUnder.current = null; |
1200 | 1203 | setSelectedRowId(null); |
1201 | 1204 | setCollapsedIds(EMPTY_IDS); |
1202 | 1205 | }} |
|