···38 type MessagesTabNavigatorParams,
39 type MyProfileTabNavigatorParams,
40 type NotificationsTabNavigatorParams,
041 type SearchTabNavigatorParams,
042} from '#/lib/routes/types'
43-import {type RouteParams, type State} from '#/lib/routes/types'
44import {attachRouteToLogEvents, logEvent} from '#/lib/statsig/statsig'
45import {bskyTitle} from '#/lib/strings/headings'
46import {logger} from '#/logger'
···144import {Referrer} from '../modules/expo-bluesky-swiss-army'
145146const navigationRef = createNavigationContainerRef<AllNavigatorParams>()
000000000000000000147148const HomeTab = createNativeStackNavigatorWithAuth<HomeTabNavigatorParams>()
149const SearchTab = createNativeStackNavigatorWithAuth<SearchTabNavigatorParams>()
···899900 const disableVerifyEmailReminder = useDisableVerifyEmailReminder()
901000902 /**
903 * Handle navigation to a conversation, or prepares for account switch.
904 *
···1005 ref={navigationRef}
1006 linking={LINKING}
1007 theme={theme}
01008 onStateChange={() => {
1009 logger.metric(
1010 'router:navigate',
···38 type MessagesTabNavigatorParams,
39 type MyProfileTabNavigatorParams,
40 type NotificationsTabNavigatorParams,
41+ type RouteParams,
42 type SearchTabNavigatorParams,
43+ type State,
44} from '#/lib/routes/types'
045import {attachRouteToLogEvents, logEvent} from '#/lib/statsig/statsig'
46import {bskyTitle} from '#/lib/strings/headings'
47import {logger} from '#/logger'
···145import {Referrer} from '../modules/expo-bluesky-swiss-army'
146147const navigationRef = createNavigationContainerRef<AllNavigatorParams>()
148+149+/**
150+ * Stores the navigation state before account switch on native.
151+ * This allows the user to stay on the same page when switching accounts.
152+ */
153+let storedNavigationStateForAccountSwitch: State | undefined
154+155+export function storeNavigationStateForAccountSwitch() {
156+ if (IS_NATIVE && navigationRef.isReady()) {
157+ storedNavigationStateForAccountSwitch = navigationRef.getRootState()
158+ }
159+}
160+161+function consumeStoredNavigationState(): State | undefined {
162+ const state = storedNavigationStateForAccountSwitch
163+ storedNavigationStateForAccountSwitch = undefined
164+ return state
165+}
166167const HomeTab = createNativeStackNavigatorWithAuth<HomeTabNavigatorParams>()
168const SearchTab = createNativeStackNavigatorWithAuth<SearchTabNavigatorParams>()
···918919 const disableVerifyEmailReminder = useDisableVerifyEmailReminder()
920921+ // Consume stored navigation state from account switch (native only)
922+ const initialState = IS_NATIVE ? consumeStoredNavigationState() : undefined
923+924 /**
925 * Handle navigation to a conversation, or prepares for account switch.
926 *
···1027 ref={navigationRef}
1028 linking={LINKING}
1029 theme={theme}
1030+ initialState={initialState}
1031 onStateChange={() => {
1032 logger.metric(
1033 'router:navigate',
+3
src/lib/hooks/useAccountSwitcher.ts
···6import {type SessionAccount, useSessionApi} from '#/state/session'
7import {useLoggedOutViewControls} from '#/state/shell/logged-out'
8import * as Toast from '#/view/com/util/Toast'
09import {logEvent} from '../statsig/statsig'
10import {type LogEvents} from '../statsig/statsig'
11···27 try {
28 setPendingDid(account.did)
29 if (account.accessJwt) {
0030 await resumeSession(account, true)
31 logEvent('account:loggedIn', {logContext, withPassword: false})
32 Toast.show(_(msg`Signed in as @${account.handle}`))
···6import {type SessionAccount, useSessionApi} from '#/state/session'
7import {useLoggedOutViewControls} from '#/state/shell/logged-out'
8import * as Toast from '#/view/com/util/Toast'
9+import {storeNavigationStateForAccountSwitch} from '#/Navigation'
10import {logEvent} from '../statsig/statsig'
11import {type LogEvents} from '../statsig/statsig'
12···28 try {
29 setPendingDid(account.did)
30 if (account.accessJwt) {
31+ // Store navigation state before switching so user stays on the same page
32+ storeNavigationStateForAccountSwitch()
33 await resumeSession(account, true)
34 logEvent('account:loggedIn', {logContext, withPassword: false})
35 Toast.show(_(msg`Signed in as @${account.handle}`))