From b9e6d1a9c7403b5e0551af6a4529d858e32ef746 Mon Sep 17 00:00:00 2001 From: DS Boyce <260543580+ds-boyce@users.noreply.github.com> Date: Wed, 28 Oct 2026 13:50:11 -0700 Subject: [PATCH] Scope navigation types to stacks and current routes (#12128) --- src/components/Link.tsx | 8 +++++-- .../inviteFriends/InviteScannerScreen.tsx | 8 +++++-- src/lib/routes/types.ts | 23 ++++++++++++++++--- .../Messages/components/MessageInputEmbed.tsx | 5 +++- src/screens/ModerationInbox/index.tsx | 3 ++- src/screens/Search/Shell.tsx | 16 ++++++++++--- src/screens/StarterPack/Wizard/index.tsx | 8 ++++++- src/view/shell/Drawer.tsx | 4 ++-- 8 files changed, 60 insertions(+), 15 deletions(-) diff --git a/src/components/Link.tsx b/src/components/Link.tsx index f2df1811a..27120ef69 100644 --- a/src/components/Link.tsx +++ b/src/components/Link.tsx @@ -17,7 +17,11 @@ import {BSKY_DOWNLOAD_URL} from '#/lib/constants' import {useGroupChatJoinIntent} from '#/lib/hooks/useIntentHandler' import {useNavigationDeduped} from '#/lib/hooks/useNavigationDeduped' import {useOpenLink} from '#/lib/hooks/useOpenLink' -import {type AllNavigatorParams, type RouteParams} from '#/lib/routes/types' +import { + type AllNavigatorParams, + type FlatNavigatorParams, + type RouteParams, +} from '#/lib/routes/types' import {shareUrl} from '#/lib/sharing' import { convertBskyAppUrlIfNeeded, @@ -188,7 +192,7 @@ export function useLink({ void openLink(href) } else { const [screen, params] = router.matchPath(href) as [ - screen: keyof AllNavigatorParams, + screen: keyof FlatNavigatorParams, params?: RouteParams, ] diff --git a/src/features/inviteFriends/InviteScannerScreen.tsx b/src/features/inviteFriends/InviteScannerScreen.tsx index 8ceae9bdd..2f779c18b 100644 --- a/src/features/inviteFriends/InviteScannerScreen.tsx +++ b/src/features/inviteFriends/InviteScannerScreen.tsx @@ -10,7 +10,10 @@ import { import {useLingui} from '@lingui/react/macro' import {useNavigation} from '@react-navigation/native' -import {type NavigationProp} from '#/lib/routes/types' +import { + type CommonNavigatorParams, + type NavigationProp, +} from '#/lib/routes/types' import {atoms as a, useTheme} from '#/alf' import {Button, ButtonText} from '#/components/Button' import {ArrowLeft_Stroke2_Corner0_Rounded as ArrowLeftIcon} from '#/components/icons/Arrow' @@ -27,7 +30,8 @@ export function InviteScannerScreen() { const t = useTheme() const ax = useAnalytics() const insets = useSafeAreaInsets() - const navigation = useNavigation() + const navigation = + useNavigation>() const [permission, requestPermission] = useCameraPermissions() const [showError, setShowError] = useState(false) const [scannerEnabled, setScannerEnabled] = useState(true) diff --git a/src/lib/routes/types.ts b/src/lib/routes/types.ts index e8fa00f23..92e5f50f3 100644 --- a/src/lib/routes/types.ts +++ b/src/lib/routes/types.ts @@ -1,6 +1,9 @@ import { + type CompositeNavigationProp, + type NavigationProp as BaseNavigationProp, type NavigationState, type NavigatorScreenParams, + type ParamListBase, type PartialState, } from '@react-navigation/native' import {type NativeStackNavigationProp} from '@react-navigation/native-stack' @@ -166,11 +169,25 @@ export type FlatNavigatorParams = CommonNavigatorParams & { export type AllNavigatorParams = FlatNavigatorParams & BottomTabNavigatorParams +/** Shared root navigation without assuming a native tab or web stack router. */ +export type RootNavigationProp = BaseNavigationProp + /** - * Shared navigation across the web stack and native tab stacks. Destination - * params are checked, but stack helpers are not scoped to the current navigator. + * Stack navigation with access to native parent tab destinations. Only the + * parent's common navigation methods are included because shared callers also + * run on web, where there is no tab navigator. + * + * Pass the current stack and route to scope stack helpers and param updates. + * The default retains all web leaf routes for shared callers whose native + * stack is not known, but excludes tab routes from stack helpers. */ -export type NavigationProp = NativeStackNavigationProp +export type NavigationProp< + ParamList extends ParamListBase = FlatNavigatorParams, + RouteName extends keyof ParamList & string = keyof ParamList & string, +> = CompositeNavigationProp< + NativeStackNavigationProp, + BaseNavigationProp +> export type State = NavigationState | Omit, 'stale'> diff --git a/src/screens/Messages/components/MessageInputEmbed.tsx b/src/screens/Messages/components/MessageInputEmbed.tsx index d5bcd4043..e6c08cb84 100644 --- a/src/screens/Messages/components/MessageInputEmbed.tsx +++ b/src/screens/Messages/components/MessageInputEmbed.tsx @@ -45,7 +45,10 @@ export type MessageEmbedState = export function useMessageEmbed() { const route = useRoute>() - const navigation = useNavigation() + const navigation = + useNavigation< + NavigationProp + >() const embedFromParams = route.params.embed const [embed, setEmbed] = useState( diff --git a/src/screens/ModerationInbox/index.tsx b/src/screens/ModerationInbox/index.tsx index 8099f2e83..9fb685f26 100644 --- a/src/screens/ModerationInbox/index.tsx +++ b/src/screens/ModerationInbox/index.tsx @@ -48,7 +48,8 @@ type InboxReport = tools.ozone.inbox.listReports.$OutputBody['reports'][number] export function ModerationInboxScreen() { const {t: l} = useLingui() const ax = useAnalytics() - const navigation = useNavigation() + const navigation = + useNavigation>() const route = useRoute>() const routeTab = route.params?.tab ?? 'reports' const initialPage = routeTab === 'account' ? 1 : 0 diff --git a/src/screens/Search/Shell.tsx b/src/screens/Search/Shell.tsx index e583ff3a0..fb7f8629a 100644 --- a/src/screens/Search/Shell.tsx +++ b/src/screens/Search/Shell.tsx @@ -26,7 +26,11 @@ import {useQueryClient} from '@tanstack/react-query' import {HITSLOP_10, HITSLOP_20} from '#/lib/constants' import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback' import {MagnifyingGlassIcon} from '#/lib/icons' -import {type NavigationProp, type SearchParams} from '#/lib/routes/types' +import { + type CommonNavigatorParams, + type NavigationProp, + type SearchParams, +} from '#/lib/routes/types' import {listenSoftReset} from '#/state/events' import { unstableCacheProfileView, @@ -113,7 +117,10 @@ export function SearchScreenShell({ const ax = useAnalytics() const t = useTheme() const {gtMobile} = useBreakpoints() - const navigation = useNavigation() + const navigation = + useNavigation< + NavigationProp + >() const route = useRoute() const textInput = useRef>(null) const {t: l} = useLingui() @@ -787,7 +794,10 @@ function useQueryManager({ initialQuery: string fixedParams?: SearchFilters }) { - const navigation = useNavigation() + const navigation = + useNavigation< + NavigationProp + >() const route = useRoute() // A raw Me operator remains part of the query until the advanced dialog diff --git a/src/screens/StarterPack/Wizard/index.tsx b/src/screens/StarterPack/Wizard/index.tsx index d8607e7ea..e87e9be85 100644 --- a/src/screens/StarterPack/Wizard/index.tsx +++ b/src/screens/StarterPack/Wizard/index.tsx @@ -159,7 +159,13 @@ function WizardInner({ fromDialog?: boolean onSuccess?: () => void }) { - const navigation = useNavigation() + const navigation = + useNavigation< + NavigationProp< + CommonNavigatorParams, + 'StarterPackEdit' | 'StarterPackWizard' + > + >() const ax = useAnalytics() const {t: l} = useLingui() const [state, dispatch] = useWizardState() diff --git a/src/view/shell/Drawer.tsx b/src/view/shell/Drawer.tsx index b48dd4a50..cb37bdc42 100644 --- a/src/view/shell/Drawer.tsx +++ b/src/view/shell/Drawer.tsx @@ -16,7 +16,7 @@ import {type PressableScale} from '#/lib/custom-animations/PressableScale' import {useNavigationTabState} from '#/lib/hooks/useNavigationTabState' import {getTabState, TabState} from '#/lib/routes/helpers' import {type SharedNavTab, TAB_TO_NAV_ITEM} from '#/lib/routes/tab-to-nav-item' -import {type NavigationProp} from '#/lib/routes/types' +import {type RootNavigationProp} from '#/lib/routes/types' import {sanitizeHandle} from '#/lib/strings/handles' import {emitSoftReset} from '#/state/events' import {useUnreadNotifications} from '#/state/queries/notifications/unread' @@ -198,7 +198,7 @@ let DrawerContent = ({}: React.PropsWithoutRef<{}>): React.ReactNode => { const t = useTheme() const insets = useSafeAreaInsets() const setDrawerOpen = useSetDrawerOpen() - const navigation = useNavigation() + const navigation = useNavigation() const ax = useAnalytics() const { isAtHome, -- knotmirror 0.0.1