From 2e5f73ff6149f9ac2834b0417c84b76763ef5ee2 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 28 Sep 2023 12:41:44 -0700 Subject: Account quick switch modal (#1567) * quick switch menu * Some small tweaks and fixes to the account switch modal * Factor out the account switcher logic to a hook * Add haptic feedback on account switcher open * Fix bad merge --------- Co-authored-by: Samuel Newman --- src/lib/hooks/useAccountSwitcher.ts | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/lib/hooks/useAccountSwitcher.ts (limited to 'src/lib/hooks/useAccountSwitcher.ts') diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts new file mode 100644 index 000000000..85bd5d0d4 --- /dev/null +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -0,0 +1,41 @@ +import {useCallback, useState} from 'react' +import {useStores} from 'state/index' +import {useAnalytics} from 'lib/analytics/analytics' +import {StackActions, useNavigation} from '@react-navigation/native' +import {NavigationProp} from 'lib/routes/types' +import {AccountData} from 'state/models/session' +import {reset as resetNavigation} from '../../Navigation' +import * as Toast from 'view/com/util/Toast' + +export function useAccountSwitcher(): [ + boolean, + (v: boolean) => void, + (acct: AccountData) => Promise, +] { + const {track} = useAnalytics() + + const store = useStores() + const [isSwitching, setIsSwitching] = useState(false) + const navigation = useNavigation() + + const onPressSwitchAccount = useCallback( + async (acct: AccountData) => { + track('Settings:SwitchAccountButtonClicked') + setIsSwitching(true) + const success = await store.session.resumeSession(acct) + store.shell.closeAllActiveElements() + if (success) { + resetNavigation() + Toast.show(`Signed in as ${acct.displayName || acct.handle}`) + } else { + Toast.show('Sorry! We need you to enter your password.') + navigation.navigate('HomeTab') + navigation.dispatch(StackActions.popToTop()) + store.session.clear() + } + }, + [track, setIsSwitching, navigation, store], + ) + + return [isSwitching, setIsSwitching, onPressSwitchAccount] +} -- cgit 1.4.1