From 4245f71cd8c04ecf67a2089481cc622a46a5a338 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 22 Jan 2026 11:26:14 -0600 Subject: [PATCH] Checkpoint: works but feels hacky --- src/Navigation.tsx | 54 +++++++++++++++++-- src/analytics/utils.ts | 3 +- .../createNativeStackNavigatorWithAuth.tsx | 46 ++++++---------- 3 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/Navigation.tsx b/src/Navigation.tsx index f354ecbe7a..de286ad9bb 100644 --- a/src/Navigation.tsx +++ b/src/Navigation.tsx @@ -1,4 +1,4 @@ -import {type JSX, useCallback, useRef} from 'react' +import {type JSX, useCallback, useEffect, useRef, useState} from 'react' import {Linking} from 'react-native' import * as Notifications from 'expo-notifications' import {i18n, type MessageDescriptor} from '@lingui/core' @@ -14,7 +14,9 @@ import { DefaultTheme, type LinkingOptions, NavigationContainer, + type NavigationState, StackActions, + useNavigation, } from '@react-navigation/native' import {timeout} from '#/lib/async/timeout' @@ -136,7 +138,12 @@ import { EmailDialogScreenID, useEmailDialogControl, } from '#/components/dialogs/EmailDialog' -import {useAnalytics} from '#/analytics' +import { + AnalyticsContext, + type AnalyticsContextType, + useAnalytics, + utils, +} from '#/analytics' import {IS_NATIVE, IS_WEB} from '#/env' import {router} from '#/routes' import {Referrer} from '../modules/expo-bluesky-swiss-army' @@ -1065,11 +1072,52 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) { // We will need to confirm we handle nested navigators correctly by the time we migrate to React Navigation 8.x // -sfn navigationInChildEnabled> - {children} + {children} ) } +function getActiveRouteFromNavigationState(state?: NavigationState) { + if (!state) return undefined + const currentRoute = state?.routes[state.index] + return currentRoute.name +} + +function NavigationAnalyticsContext({children}: {children: React.ReactNode}) { + const nav = useNavigation() + const [previousScreen, setPreviousScreen] = useState( + () => getActiveRouteFromNavigationState(nav.getState()) ?? 'Home', + ) + const [metadata, setMetadata] = useState< + Pick + >(() => { + return { + navigation: { + previousScreen, + currentScreen: previousScreen, + }, + } + }) + useEffect(() => { + return nav.addListener('state', payload => { + const curr = + getActiveRouteFromNavigationState(payload.data.state) ?? 'Home' + setMetadata({ + navigation: { + previousScreen, + currentScreen: curr, + }, + }) + setPreviousScreen(curr) + }) + }, [nav, previousScreen]) + return ( + + {children} + + ) +} + function getCurrentRouteName() { if (navigationRef.isReady()) { return navigationRef.getCurrentRoute()?.name diff --git a/src/analytics/utils.ts b/src/analytics/utils.ts index 8d94c4661f..941dcf5cd5 100644 --- a/src/analytics/utils.ts +++ b/src/analytics/utils.ts @@ -12,10 +12,11 @@ import { * Thin `useMemo` wrapper that marks the metadata as memoized and provides a * type guard. */ -export function useMeta(metadata: MergeableMetadata) { +export function useMeta(metadata?: MergeableMetadata) { const m = useMemo(() => metadata, [metadata]) // @ts-ignore m.__meta = true + console.log('useMeta', JSON.stringify(m, null, 2)) return m } diff --git a/src/view/shell/createNativeStackNavigatorWithAuth.tsx b/src/view/shell/createNativeStackNavigatorWithAuth.tsx index e5a9e0a636..76f4f19ac0 100644 --- a/src/view/shell/createNativeStackNavigatorWithAuth.tsx +++ b/src/view/shell/createNativeStackNavigatorWithAuth.tsx @@ -37,7 +37,6 @@ import {Onboarding} from '#/screens/Onboarding' import {SignupQueued} from '#/screens/SignupQueued' import {atoms as a, useLayoutBreakpoints} from '#/alf' import {PolicyUpdateOverlay} from '#/components/PolicyUpdateOverlay' -import {AnalyticsContext, utils} from '#/analytics' import {IS_NATIVE, IS_WEB} from '#/env' import {BottomBarWeb} from './bottom-bar/BottomBarWeb' import {DesktopLeftNav} from './desktop/LeftNav' @@ -47,8 +46,6 @@ type NativeStackNavigationOptionsWithAuth = NativeStackNavigationOptions & { requireAuth?: boolean } -let prevActiveRouteName: string | undefined - function NativeStackNavigator({ id, initialRouteName, @@ -117,7 +114,6 @@ function NativeStackNavigator({ const {setShowLoggedOut} = useLoggedOutViewControls() const {isMobile} = useWebMediaQueries() const {leftNavMinimal} = useLayoutBreakpoints() - if (!hasSession && (activeRouteRequiresAuth || IS_NATIVE)) { return } @@ -152,32 +148,24 @@ function NativeStackNavigator({ return ( - - - - - {IS_WEB && ( - <> - {showBottomBar ? : } - {!isMobile && } - - )} + + + + {IS_WEB && ( + <> + {showBottomBar ? : } + {!isMobile && } + + )} - {/* Only shown after logged in and onboaring etc are complete */} - {hasSession && } - + {/* Only shown after logged in and onboaring etc are complete */} + {hasSession && } ) }