about summary refs log tree commit diff
path: root/src/lib/hooks/useAccountSwitcher.ts
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-05-02 18:25:09 +0100
committerGitHub <noreply@github.com>2024-05-02 18:25:09 +0100
commit1a07e23192d06afd3879e89252f1bcfbb33df081 (patch)
treeb3d84d07475a4a97864387025144ea67d81dd5cb /src/lib/hooks/useAccountSwitcher.ts
parent5ec945b7625a8daff3baaf76826c2f9e3c12e279 (diff)
downloadvoidsky-1a07e23192d06afd3879e89252f1bcfbb33df081.tar.zst
[Session] Extract selectAccount out (#3812)
Diffstat (limited to 'src/lib/hooks/useAccountSwitcher.ts')
-rw-r--r--src/lib/hooks/useAccountSwitcher.ts16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts
index 6d2f7b36b..d4e026958 100644
--- a/src/lib/hooks/useAccountSwitcher.ts
+++ b/src/lib/hooks/useAccountSwitcher.ts
@@ -1,4 +1,4 @@
-import {useCallback} from 'react'
+import {useCallback, useState} from 'react'
 import {msg} from '@lingui/macro'
 import {useLingui} from '@lingui/react'
 
@@ -8,12 +8,14 @@ import {isWeb} from '#/platform/detection'
 import {SessionAccount, useSessionApi} from '#/state/session'
 import {useLoggedOutViewControls} from '#/state/shell/logged-out'
 import * as Toast from '#/view/com/util/Toast'
+import {logEvent} from '../statsig/statsig'
 import {LogEvents} from '../statsig/statsig'
 
 export function useAccountSwitcher() {
+  const [isSwitchingAccounts, setIsSwitchingAccounts] = useState(false)
   const {_} = useLingui()
   const {track} = useAnalytics()
-  const {selectAccount, clearCurrentAccount} = useSessionApi()
+  const {initSession, clearCurrentAccount} = useSessionApi()
   const {requestSwitchToAccount} = useLoggedOutViewControls()
 
   const onPressSwitchAccount = useCallback(
@@ -24,6 +26,7 @@ export function useAccountSwitcher() {
       track('Settings:SwitchAccountButtonClicked')
 
       try {
+        setIsSwitchingAccounts(true)
         if (account.accessJwt) {
           if (isWeb) {
             // We're switching accounts, which remounts the entire app.
@@ -33,7 +36,8 @@ export function useAccountSwitcher() {
             // So we change the URL ourselves. The navigator will pick it up on remount.
             history.pushState(null, '', '/')
           }
-          await selectAccount(account, logContext)
+          await initSession(account)
+          logEvent('account:loggedIn', {logContext, withPassword: false})
           setTimeout(() => {
             Toast.show(_(msg`Signed in as @${account.handle}`))
           }, 100)
@@ -52,10 +56,12 @@ export function useAccountSwitcher() {
         setTimeout(() => {
           Toast.show(_(msg`Sorry! We need you to enter your password.`))
         }, 100)
+      } finally {
+        setIsSwitchingAccounts(false)
       }
     },
-    [_, track, clearCurrentAccount, selectAccount, requestSwitchToAccount],
+    [_, track, clearCurrentAccount, initSession, requestSwitchToAccount],
   )
 
-  return {onPressSwitchAccount}
+  return {onPressSwitchAccount, isSwitchingAccounts}
 }