about summary refs log tree commit diff
path: root/src/lib/hooks
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-09-28 12:41:44 -0700
committerGitHub <noreply@github.com>2023-09-28 12:41:44 -0700
commit2e5f73ff6149f9ac2834b0417c84b76763ef5ee2 (patch)
tree2a30023dc0f6c81c33704235655f65e0f204629f /src/lib/hooks
parent3e340b336ef92feb25da042430b6b3719c772b77 (diff)
downloadvoidsky-2e5f73ff6149f9ac2834b0417c84b76763ef5ee2.tar.zst
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 <mozzius@protonmail.com>
Diffstat (limited to 'src/lib/hooks')
-rw-r--r--src/lib/hooks/useAccountSwitcher.ts41
1 files changed, 41 insertions, 0 deletions
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<void>,
+] {
+  const {track} = useAnalytics()
+
+  const store = useStores()
+  const [isSwitching, setIsSwitching] = useState(false)
+  const navigation = useNavigation<NavigationProp>()
+
+  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]
+}