properly close the switch account dialog (#3558)

* properly close the switch account dialog

* use it for switch account as well

* ensure dialog is closed on unmount

Revert "properly check if the ref is null"

This reverts commit 8f563808a5d39389b0bc47a31e73cd147d1e7e8b.

properly check if the ref is null

ensure dialog is closed on unmount

* Revert "ensure dialog is closed on unmount"

This reverts commit a48548fd8ed53ae3eb08a0e05bb89f641c112b95.
zio/stable
Hailey 2024-04-22 14:46:05 -07:00 committed by GitHub
parent f49d73dd00
commit 1a4e05e9f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 22 deletions

View File

@ -6,7 +6,6 @@ import {useLingui} from '@lingui/react'
import {useAccountSwitcher} from '#/lib/hooks/useAccountSwitcher' import {useAccountSwitcher} from '#/lib/hooks/useAccountSwitcher'
import {type SessionAccount, useSession} from '#/state/session' import {type SessionAccount, useSession} from '#/state/session'
import {useLoggedOutViewControls} from '#/state/shell/logged-out' import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import {useCloseAllActiveElements} from '#/state/util'
import {atoms as a} from '#/alf' import {atoms as a} from '#/alf'
import * as Dialog from '#/components/Dialog' import * as Dialog from '#/components/Dialog'
import {AccountList} from '../AccountList' import {AccountList} from '../AccountList'
@ -21,23 +20,25 @@ export function SwitchAccountDialog({
const {currentAccount} = useSession() const {currentAccount} = useSession()
const {onPressSwitchAccount} = useAccountSwitcher() const {onPressSwitchAccount} = useAccountSwitcher()
const {setShowLoggedOut} = useLoggedOutViewControls() const {setShowLoggedOut} = useLoggedOutViewControls()
const closeAllActiveElements = useCloseAllActiveElements()
const onSelectAccount = useCallback( const onSelectAccount = useCallback(
(account: SessionAccount) => { (account: SessionAccount) => {
if (account.did === currentAccount?.did) { if (account.did !== currentAccount?.did) {
control.close() control.close(() => {
onPressSwitchAccount(account, 'SwitchAccount')
})
} else { } else {
onPressSwitchAccount(account, 'SwitchAccount') control.close()
} }
}, },
[currentAccount, control, onPressSwitchAccount], [currentAccount, control, onPressSwitchAccount],
) )
const onPressAddAccount = useCallback(() => { const onPressAddAccount = useCallback(() => {
setShowLoggedOut(true) control.close(() => {
closeAllActiveElements() setShowLoggedOut(true)
}, [setShowLoggedOut, closeAllActiveElements]) })
}, [setShowLoggedOut, control])
return ( return (
<Dialog.Outer control={control}> <Dialog.Outer control={control}>

View File

@ -1,17 +1,15 @@
import {useCallback} from 'react' import {useCallback} from 'react'
import {isWeb} from '#/platform/detection'
import {useAnalytics} from '#/lib/analytics/analytics' import {useAnalytics} from '#/lib/analytics/analytics'
import {useSessionApi, SessionAccount} from '#/state/session' import {isWeb} from '#/platform/detection'
import * as Toast from '#/view/com/util/Toast' import {SessionAccount, useSessionApi} from '#/state/session'
import {useCloseAllActiveElements} from '#/state/util'
import {useLoggedOutViewControls} from '#/state/shell/logged-out' import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import * as Toast from '#/view/com/util/Toast'
import {LogEvents} from '../statsig/statsig' import {LogEvents} from '../statsig/statsig'
export function useAccountSwitcher() { export function useAccountSwitcher() {
const {track} = useAnalytics() const {track} = useAnalytics()
const {selectAccount, clearCurrentAccount} = useSessionApi() const {selectAccount, clearCurrentAccount} = useSessionApi()
const closeAllActiveElements = useCloseAllActiveElements()
const {requestSwitchToAccount} = useLoggedOutViewControls() const {requestSwitchToAccount} = useLoggedOutViewControls()
const onPressSwitchAccount = useCallback( const onPressSwitchAccount = useCallback(
@ -23,7 +21,6 @@ export function useAccountSwitcher() {
try { try {
if (account.accessJwt) { if (account.accessJwt) {
closeAllActiveElements()
if (isWeb) { if (isWeb) {
// We're switching accounts, which remounts the entire app. // We're switching accounts, which remounts the entire app.
// On mobile, this gets us Home, but on the web we also need reset the URL. // On mobile, this gets us Home, but on the web we also need reset the URL.
@ -37,7 +34,6 @@ export function useAccountSwitcher() {
Toast.show(`Signed in as @${account.handle}`) Toast.show(`Signed in as @${account.handle}`)
}, 100) }, 100)
} else { } else {
closeAllActiveElements()
requestSwitchToAccount({requestedAccount: account.did}) requestSwitchToAccount({requestedAccount: account.did})
Toast.show( Toast.show(
`Please sign in as @${account.handle}`, `Please sign in as @${account.handle}`,
@ -49,13 +45,7 @@ export function useAccountSwitcher() {
clearCurrentAccount() // back user out to login clearCurrentAccount() // back user out to login
} }
}, },
[ [track, clearCurrentAccount, selectAccount, requestSwitchToAccount],
track,
clearCurrentAccount,
selectAccount,
closeAllActiveElements,
requestSwitchToAccount,
],
) )
return {onPressSwitchAccount} return {onPressSwitchAccount}