Nav home after switch accounts (#2002)

zio/stable
Eric Bailey 2023-11-27 12:27:38 -06:00 committed by GitHub
parent 828e53d533
commit 6758755316
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 2 deletions

View File

@ -1,4 +1,8 @@
import {useCallback} from 'react'
import {useNavigation} from '@react-navigation/native'
import {isWeb} from '#/platform/detection'
import {NavigationProp} from '#/lib/routes/types'
import {useAnalytics} from '#/lib/analytics/analytics'
import {useSessionApi, SessionAccount} from '#/state/session'
import * as Toast from '#/view/com/util/Toast'
@ -8,21 +12,29 @@ export function useAccountSwitcher() {
const {track} = useAnalytics()
const {selectAccount, clearCurrentAccount} = useSessionApi()
const closeAllActiveElements = useCloseAllActiveElements()
const navigation = useNavigation<NavigationProp>()
const onPressSwitchAccount = useCallback(
async (acct: SessionAccount) => {
track('Settings:SwitchAccountButtonClicked')
try {
await selectAccount(acct)
closeAllActiveElements()
navigation.navigate(isWeb ? 'Home' : 'HomeTab')
await selectAccount(acct)
Toast.show(`Signed in as ${acct.handle}`)
} catch (e) {
Toast.show('Sorry! We need you to enter your password.')
clearCurrentAccount() // back user out to login
}
},
[track, clearCurrentAccount, selectAccount, closeAllActiveElements],
[
track,
clearCurrentAccount,
selectAccount,
closeAllActiveElements,
navigation,
],
)
return {onPressSwitchAccount}