Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly close the switch account dialog #3558

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/components/dialogs/SwitchAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {useLingui} from '@lingui/react'
import {useAccountSwitcher} from '#/lib/hooks/useAccountSwitcher'
import {type SessionAccount, useSession} from '#/state/session'
import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import {useCloseAllActiveElements} from '#/state/util'
import {atoms as a} from '#/alf'
import * as Dialog from '#/components/Dialog'
import {AccountList} from '../AccountList'
Expand All @@ -21,23 +20,25 @@ export function SwitchAccountDialog({
const {currentAccount} = useSession()
const {onPressSwitchAccount} = useAccountSwitcher()
const {setShowLoggedOut} = useLoggedOutViewControls()
const closeAllActiveElements = useCloseAllActiveElements()

const onSelectAccount = useCallback(
(account: SessionAccount) => {
if (account.did === currentAccount?.did) {
control.close()
if (account.did !== currentAccount?.did) {
control.close(() => {
onPressSwitchAccount(account, 'SwitchAccount')
})
} else {
onPressSwitchAccount(account, 'SwitchAccount')
control.close()
}
},
[currentAccount, control, onPressSwitchAccount],
)

const onPressAddAccount = useCallback(() => {
setShowLoggedOut(true)
closeAllActiveElements()
}, [setShowLoggedOut, closeAllActiveElements])
control.close(() => {
setShowLoggedOut(true)
})
}, [setShowLoggedOut, control])

return (
<Dialog.Outer control={control}>
Expand Down
18 changes: 4 additions & 14 deletions src/lib/hooks/useAccountSwitcher.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {useCallback} from 'react'

import {isWeb} from '#/platform/detection'
import {useAnalytics} from '#/lib/analytics/analytics'
import {useSessionApi, SessionAccount} from '#/state/session'
import * as Toast from '#/view/com/util/Toast'
import {useCloseAllActiveElements} from '#/state/util'
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 {LogEvents} from '../statsig/statsig'

export function useAccountSwitcher() {
const {track} = useAnalytics()
const {selectAccount, clearCurrentAccount} = useSessionApi()
const closeAllActiveElements = useCloseAllActiveElements()
const {requestSwitchToAccount} = useLoggedOutViewControls()

const onPressSwitchAccount = useCallback(
Expand All @@ -23,7 +21,6 @@ export function useAccountSwitcher() {

try {
if (account.accessJwt) {
closeAllActiveElements()
if (isWeb) {
// 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.
Expand All @@ -37,7 +34,6 @@ export function useAccountSwitcher() {
Toast.show(`Signed in as @${account.handle}`)
}, 100)
} else {
closeAllActiveElements()
requestSwitchToAccount({requestedAccount: account.did})
Toast.show(
`Please sign in as @${account.handle}`,
Expand All @@ -49,13 +45,7 @@ export function useAccountSwitcher() {
clearCurrentAccount() // back user out to login
}
},
[
track,
clearCurrentAccount,
selectAccount,
closeAllActiveElements,
requestSwitchToAccount,
],
[track, clearCurrentAccount, selectAccount, requestSwitchToAccount],
)

return {onPressSwitchAccount}
Expand Down
Loading