Refactor current route handling

This commit is contained in:
Eric Bailey
2026-01-21 11:52:16 -06:00
parent 3bc772a12a
commit de6efbdf52
3 changed files with 67 additions and 41 deletions
+38 -12
View File
@@ -28,7 +28,6 @@ import {
storePayloadForAccountSwitch, storePayloadForAccountSwitch,
} from '#/lib/hooks/useNotificationHandler' } from '#/lib/hooks/useNotificationHandler'
import {useWebScrollRestoration} from '#/lib/hooks/useWebScrollRestoration' import {useWebScrollRestoration} from '#/lib/hooks/useWebScrollRestoration'
import {logger as notyLogger} from '#/lib/notifications/util'
import {buildStateObject} from '#/lib/routes/helpers' import {buildStateObject} from '#/lib/routes/helpers'
import { import {
type AllNavigatorParams, type AllNavigatorParams,
@@ -38,12 +37,12 @@ import {
type MessagesTabNavigatorParams, type MessagesTabNavigatorParams,
type MyProfileTabNavigatorParams, type MyProfileTabNavigatorParams,
type NotificationsTabNavigatorParams, type NotificationsTabNavigatorParams,
type RouteParams,
type SearchTabNavigatorParams, type SearchTabNavigatorParams,
type State,
} from '#/lib/routes/types' } from '#/lib/routes/types'
import {type RouteParams, type State} from '#/lib/routes/types' import {logEvent} from '#/lib/statsig/statsig'
import {attachRouteToLogEvents, logEvent} from '#/lib/statsig/statsig'
import {bskyTitle} from '#/lib/strings/headings' import {bskyTitle} from '#/lib/strings/headings'
import {logger} from '#/logger'
import {useUnreadNotifications} from '#/state/queries/notifications/unread' import {useUnreadNotifications} from '#/state/queries/notifications/unread'
import {useSession} from '#/state/session' import {useSession} from '#/state/session'
import {useLoggedOutViewControls} from '#/state/shell/logged-out' import {useLoggedOutViewControls} from '#/state/shell/logged-out'
@@ -137,6 +136,7 @@ import {
EmailDialogScreenID, EmailDialogScreenID,
useEmailDialogControl, useEmailDialogControl,
} from '#/components/dialogs/EmailDialog' } from '#/components/dialogs/EmailDialog'
import {AnalyticsContext, useAnalytics, utils} from '#/analytics'
import {IS_NATIVE, IS_WEB} from '#/env' import {IS_NATIVE, IS_WEB} from '#/env'
import {router} from '#/routes' import {router} from '#/routes'
import {Referrer} from '../modules/expo-bluesky-swiss-army' import {Referrer} from '../modules/expo-bluesky-swiss-army'
@@ -879,6 +879,8 @@ const LINKING = {
let lastHandledNotificationDateDedupe: number | undefined let lastHandledNotificationDateDedupe: number | undefined
function RoutesContainer({children}: React.PropsWithChildren<{}>) { function RoutesContainer({children}: React.PropsWithChildren<{}>) {
const ax = useAnalytics()
const notyLogger = ax.logger.useContext(ax.logger.Context.Notifications)
const theme = useColorSchemeStyle(DefaultTheme, DarkTheme) const theme = useColorSchemeStyle(DefaultTheme, DarkTheme)
const {currentAccount, accounts} = useSession() const {currentAccount, accounts} = useSession()
const {onPressSwitchAccount} = useAccountSwitcher() const {onPressSwitchAccount} = useAccountSwitcher()
@@ -945,10 +947,15 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
const payload = getNotificationPayload(response.notification) const payload = getNotificationPayload(response.notification)
if (payload) { if (payload) {
notyLogger.metric( ax.metric(
'notifications:openApp', 'notifications:openApp',
{reason: payload.reason, causedBoot: true}, {reason: payload.reason, causedBoot: true},
{statsig: false}, {
navigation: {
previousScreen: prevLoggedRouteName.current,
currentScreen: getCurrentRouteName(),
},
},
) )
if (payload.reason === 'chat-message') { if (payload.reason === 'chat-message') {
@@ -984,24 +991,36 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
} }
return ( return (
<>
<NavigationContainer <NavigationContainer
ref={navigationRef} ref={navigationRef}
linking={LINKING} linking={LINKING}
theme={theme} theme={theme}
onStateChange={() => { onStateChange={() => {
logger.metric( ax.metric(
'router:navigate', 'router:navigate',
{from: prevLoggedRouteName.current}, {from: prevLoggedRouteName.current},
{statsig: false}, {
navigation: {
previousScreen: prevLoggedRouteName.current,
currentScreen: getCurrentRouteName(),
},
},
) )
prevLoggedRouteName.current = getCurrentRouteName() prevLoggedRouteName.current = getCurrentRouteName()
}} }}
onReady={() => { onReady={() => {
attachRouteToLogEvents(getCurrentRouteName)
logModuleInitTime() logModuleInitTime()
onReady() onReady()
logger.metric('router:navigate', {}, {statsig: false}) ax.metric(
'router:navigate',
{},
{
navigation: {
previousScreen: prevLoggedRouteName.current,
currentScreen: getCurrentRouteName(),
},
},
)
handlePushNotificationEntry() handlePushNotificationEntry()
}} }}
// WARNING: Implicit navigation to nested navigators is depreciated in React Navigation 7.x // WARNING: Implicit navigation to nested navigators is depreciated in React Navigation 7.x
@@ -1011,9 +1030,16 @@ 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 // We will need to confirm we handle nested navigators correctly by the time we migrate to React Navigation 8.x
// -sfn // -sfn
navigationInChildEnabled> navigationInChildEnabled>
<AnalyticsContext
metadata={utils.useMeta({
navigation: {
previousScreen: prevLoggedRouteName.current,
currentScreen: getCurrentRouteName(),
},
})}>
{children} {children}
</AnalyticsContext>
</NavigationContainer> </NavigationContainer>
</>
) )
} }
+6
View File
@@ -13,6 +13,11 @@ export type BaseMetadata = {
export type GeolocationMetadata = Geolocation export type GeolocationMetadata = Geolocation
export type NavigationMetadata = {
previousScreen?: string
currentScreen?: string
}
export type SessionMetadata = { export type SessionMetadata = {
did: string did: string
isBskyPds: boolean isBskyPds: boolean
@@ -24,6 +29,7 @@ export type PreferencesMetadata = {
} }
export type MergeableMetadata = { export type MergeableMetadata = {
navigation?: NavigationMetadata
session?: SessionMetadata session?: SessionMetadata
preferences?: PreferencesMetadata preferences?: PreferencesMetadata
} }
-6
View File
@@ -76,12 +76,6 @@ type FlatJSONRecord = Record<
let getCurrentRouteName: () => string | null | undefined = () => null let getCurrentRouteName: () => string | null | undefined = () => null
export function attachRouteToLogEvents(
getRouteName: () => string | null | undefined,
) {
getCurrentRouteName = getRouteName
}
export function toClout(n: number | null | undefined): number | undefined { export function toClout(n: number | null | undefined): number | undefined {
if (n == null) { if (n == null) {
return undefined return undefined