[Session] Extract selectAccount out (#3812)

zio/stable
dan 2024-05-02 18:25:09 +01:00 committed by GitHub
parent 5ec945b762
commit 1a07e23192
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 48 additions and 42 deletions

View File

@ -16,12 +16,14 @@ export function AccountList({
onSelectAccount, onSelectAccount,
onSelectOther, onSelectOther,
otherLabel, otherLabel,
isSwitchingAccounts,
}: { }: {
onSelectAccount: (account: SessionAccount) => void onSelectAccount: (account: SessionAccount) => void
onSelectOther: () => void onSelectOther: () => void
otherLabel?: string otherLabel?: string
isSwitchingAccounts: boolean
}) { }) {
const {isSwitchingAccounts, currentAccount, accounts} = useSession() const {currentAccount, accounts} = useSession()
const t = useTheme() const t = useTheme()
const {_} = useLingui() const {_} = useLingui()

View File

@ -18,7 +18,7 @@ export function SwitchAccountDialog({
}) { }) {
const {_} = useLingui() const {_} = useLingui()
const {currentAccount} = useSession() const {currentAccount} = useSession()
const {onPressSwitchAccount} = useAccountSwitcher() const {onPressSwitchAccount, isSwitchingAccounts} = useAccountSwitcher()
const {setShowLoggedOut} = useLoggedOutViewControls() const {setShowLoggedOut} = useLoggedOutViewControls()
const onSelectAccount = useCallback( const onSelectAccount = useCallback(
@ -54,6 +54,7 @@ export function SwitchAccountDialog({
onSelectAccount={onSelectAccount} onSelectAccount={onSelectAccount}
onSelectOther={onPressAddAccount} onSelectOther={onPressAddAccount}
otherLabel={_(msg`Add account`)} otherLabel={_(msg`Add account`)}
isSwitchingAccounts={isSwitchingAccounts}
/> />
</View> </View>
</Dialog.ScrollableInner> </Dialog.ScrollableInner>

View File

@ -1,4 +1,4 @@
import {useCallback} from 'react' import {useCallback, useState} from 'react'
import {msg} from '@lingui/macro' import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@ -8,12 +8,14 @@ import {isWeb} from '#/platform/detection'
import {SessionAccount, useSessionApi} from '#/state/session' import {SessionAccount, useSessionApi} from '#/state/session'
import {useLoggedOutViewControls} from '#/state/shell/logged-out' import {useLoggedOutViewControls} from '#/state/shell/logged-out'
import * as Toast from '#/view/com/util/Toast' import * as Toast from '#/view/com/util/Toast'
import {logEvent} from '../statsig/statsig'
import {LogEvents} from '../statsig/statsig' import {LogEvents} from '../statsig/statsig'
export function useAccountSwitcher() { export function useAccountSwitcher() {
const [isSwitchingAccounts, setIsSwitchingAccounts] = useState(false)
const {_} = useLingui() const {_} = useLingui()
const {track} = useAnalytics() const {track} = useAnalytics()
const {selectAccount, clearCurrentAccount} = useSessionApi() const {initSession, clearCurrentAccount} = useSessionApi()
const {requestSwitchToAccount} = useLoggedOutViewControls() const {requestSwitchToAccount} = useLoggedOutViewControls()
const onPressSwitchAccount = useCallback( const onPressSwitchAccount = useCallback(
@ -24,6 +26,7 @@ export function useAccountSwitcher() {
track('Settings:SwitchAccountButtonClicked') track('Settings:SwitchAccountButtonClicked')
try { try {
setIsSwitchingAccounts(true)
if (account.accessJwt) { if (account.accessJwt) {
if (isWeb) { if (isWeb) {
// We're switching accounts, which remounts the entire app. // 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. // So we change the URL ourselves. The navigator will pick it up on remount.
history.pushState(null, '', '/') history.pushState(null, '', '/')
} }
await selectAccount(account, logContext) await initSession(account)
logEvent('account:loggedIn', {logContext, withPassword: false})
setTimeout(() => { setTimeout(() => {
Toast.show(_(msg`Signed in as @${account.handle}`)) Toast.show(_(msg`Signed in as @${account.handle}`))
}, 100) }, 100)
@ -52,10 +56,12 @@ export function useAccountSwitcher() {
setTimeout(() => { setTimeout(() => {
Toast.show(_(msg`Sorry! We need you to enter your password.`)) Toast.show(_(msg`Sorry! We need you to enter your password.`))
}, 100) }, 100)
} finally {
setIsSwitchingAccounts(false)
} }
}, },
[_, track, clearCurrentAccount, selectAccount, requestSwitchToAccount], [_, track, clearCurrentAccount, initSession, requestSwitchToAccount],
) )
return {onPressSwitchAccount} return {onPressSwitchAccount, isSwitchingAccounts}
} }

View File

@ -22,6 +22,7 @@ export const ChooseAccountForm = ({
onSelectAccount: (account?: SessionAccount) => void onSelectAccount: (account?: SessionAccount) => void
onPressBack: () => void onPressBack: () => void
}) => { }) => {
const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false)
const {track, screen} = useAnalytics() const {track, screen} = useAnalytics()
const {_} = useLingui() const {_} = useLingui()
const {currentAccount} = useSession() const {currentAccount} = useSession()
@ -40,6 +41,7 @@ export const ChooseAccountForm = ({
Toast.show(_(msg`Already signed in as @${account.handle}`)) Toast.show(_(msg`Already signed in as @${account.handle}`))
} else { } else {
try { try {
setIsSwitchingAccounts(true)
await initSession(account) await initSession(account)
logEvent('account:loggedIn', { logEvent('account:loggedIn', {
logContext: 'ChooseAccountForm', logContext: 'ChooseAccountForm',
@ -54,6 +56,8 @@ export const ChooseAccountForm = ({
message: e.message, message: e.message,
}) })
onSelectAccount(account) onSelectAccount(account)
} finally {
setIsSwitchingAccounts(false)
} }
} }
} else { } else {
@ -74,6 +78,7 @@ export const ChooseAccountForm = ({
<AccountList <AccountList
onSelectAccount={onSelect} onSelectAccount={onSelect}
onSelectOther={() => onSelectAccount()} onSelectOther={() => onSelectAccount()}
isSwitchingAccounts={isSwitchingAccounts}
/> />
</View> </View>
<View style={[a.flex_row]}> <View style={[a.flex_row]}>

View File

@ -35,7 +35,6 @@ const PUBLIC_BSKY_AGENT = new BskyAgent({service: PUBLIC_BSKY_SERVICE})
configureModerationForGuest() configureModerationForGuest()
const StateContext = React.createContext<SessionStateContext>({ const StateContext = React.createContext<SessionStateContext>({
isSwitchingAccounts: false,
accounts: [], accounts: [],
currentAccount: undefined, currentAccount: undefined,
hasSession: false, hasSession: false,
@ -47,7 +46,6 @@ const ApiContext = React.createContext<SessionApiContext>({
logout: async () => {}, logout: async () => {},
initSession: async () => {}, initSession: async () => {},
removeAccount: () => {}, removeAccount: () => {},
selectAccount: async () => {},
updateCurrentAccount: () => {}, updateCurrentAccount: () => {},
clearCurrentAccount: () => {}, clearCurrentAccount: () => {},
}) })
@ -65,7 +63,6 @@ type State = {
} }
export function Provider({children}: React.PropsWithChildren<{}>) { export function Provider({children}: React.PropsWithChildren<{}>) {
const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false)
const [state, setState] = React.useState<State>({ const [state, setState] = React.useState<State>({
accounts: persisted.get('session').accounts, accounts: persisted.get('session').accounts,
currentAccountDid: undefined, // assume logged out to start currentAccountDid: undefined, // assume logged out to start
@ -437,23 +434,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[setState], [setState],
) )
const selectAccount = React.useCallback<SessionApiContext['selectAccount']>(
async (account, logContext) => {
setIsSwitchingAccounts(true)
try {
await initSession(account)
setIsSwitchingAccounts(false)
logEvent('account:loggedIn', {logContext, withPassword: false})
} catch (e) {
// reset this in case of error
setIsSwitchingAccounts(false)
// but other listeners need a throw
throw e
}
},
[initSession],
)
React.useEffect(() => { React.useEffect(() => {
if (state.needsPersist) { if (state.needsPersist) {
state.needsPersist = false state.needsPersist = false
@ -529,10 +509,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
currentAccount: state.accounts.find( currentAccount: state.accounts.find(
a => a.did === state.currentAccountDid, a => a.did === state.currentAccountDid,
), ),
isSwitchingAccounts,
hasSession: !!state.currentAccountDid, hasSession: !!state.currentAccountDid,
}), }),
[state, isSwitchingAccounts], [state],
) )
const api = React.useMemo( const api = React.useMemo(
@ -542,7 +521,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logout, logout,
initSession, initSession,
removeAccount, removeAccount,
selectAccount,
updateCurrentAccount, updateCurrentAccount,
clearCurrentAccount, clearCurrentAccount,
}), }),
@ -552,7 +530,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logout, logout,
initSession, initSession,
removeAccount, removeAccount,
selectAccount,
updateCurrentAccount, updateCurrentAccount,
clearCurrentAccount, clearCurrentAccount,
], ],

View File

@ -6,7 +6,6 @@ export type SessionAccount = PersistedAccount
export type SessionStateContext = { export type SessionStateContext = {
accounts: SessionAccount[] accounts: SessionAccount[]
currentAccount: SessionAccount | undefined currentAccount: SessionAccount | undefined
isSwitchingAccounts: boolean
hasSession: boolean hasSession: boolean
} }
export type SessionApiContext = { export type SessionApiContext = {
@ -46,10 +45,6 @@ export type SessionApiContext = {
clearCurrentAccount: () => void clearCurrentAccount: () => void
initSession: (account: SessionAccount) => Promise<void> initSession: (account: SessionAccount) => Promise<void>
removeAccount: (account: SessionAccount) => void removeAccount: (account: SessionAccount) => void
selectAccount: (
account: SessionAccount,
logContext: LogEvents['account:loggedIn']['logContext'],
) => Promise<void>
updateCurrentAccount: ( updateCurrentAccount: (
account: Partial< account: Partial<
Pick< Pick<

View File

@ -70,14 +70,24 @@ import {navigate, resetToTab} from '#/Navigation'
import {Email2FAToggle} from './Email2FAToggle' import {Email2FAToggle} from './Email2FAToggle'
import {ExportCarDialog} from './ExportCarDialog' import {ExportCarDialog} from './ExportCarDialog'
function SettingsAccountCard({account}: {account: SessionAccount}) { function SettingsAccountCard({
account,
isSwitchingAccounts,
onPressSwitchAccount,
}: {
account: SessionAccount
isSwitchingAccounts: boolean
onPressSwitchAccount: (
account: SessionAccount,
logContext: 'Settings',
) => void
}) {
const pal = usePalette('default') const pal = usePalette('default')
const {_} = useLingui() const {_} = useLingui()
const {isSwitchingAccounts, currentAccount} = useSession() const {currentAccount} = useSession()
const {logout} = useSessionApi() const {logout} = useSessionApi()
const {data: profile} = useProfileQuery({did: account.did}) const {data: profile} = useProfileQuery({did: account.did})
const isCurrentAccount = account.did === currentAccount?.did const isCurrentAccount = account.did === currentAccount?.did
const {onPressSwitchAccount} = useAccountSwitcher()
const contents = ( const contents = (
<View style={[pal.view, styles.linkCard]}> <View style={[pal.view, styles.linkCard]}>
@ -165,12 +175,13 @@ export function SettingsScreen({}: Props) {
const {isMobile} = useWebMediaQueries() const {isMobile} = useWebMediaQueries()
const {screen, track} = useAnalytics() const {screen, track} = useAnalytics()
const {openModal} = useModalControls() const {openModal} = useModalControls()
const {isSwitchingAccounts, accounts, currentAccount} = useSession() const {accounts, currentAccount} = useSession()
const {mutate: clearPreferences} = useClearPreferencesMutation() const {mutate: clearPreferences} = useClearPreferencesMutation()
const {setShowLoggedOut} = useLoggedOutViewControls() const {setShowLoggedOut} = useLoggedOutViewControls()
const closeAllActiveElements = useCloseAllActiveElements() const closeAllActiveElements = useCloseAllActiveElements()
const exportCarControl = useDialogControl() const exportCarControl = useDialogControl()
const birthdayControl = useDialogControl() const birthdayControl = useDialogControl()
const {isSwitchingAccounts, onPressSwitchAccount} = useAccountSwitcher()
// TODO: TEMP REMOVE WHEN CLOPS ARE RELEASED // TODO: TEMP REMOVE WHEN CLOPS ARE RELEASED
const gate = useGate() const gate = useGate()
@ -385,13 +396,22 @@ export function SettingsScreen({}: Props) {
<ActivityIndicator /> <ActivityIndicator />
</View> </View>
) : ( ) : (
<SettingsAccountCard account={currentAccount!} /> <SettingsAccountCard
account={currentAccount!}
onPressSwitchAccount={onPressSwitchAccount}
isSwitchingAccounts={isSwitchingAccounts}
/>
)} )}
{accounts {accounts
.filter(a => a.did !== currentAccount?.did) .filter(a => a.did !== currentAccount?.did)
.map(account => ( .map(account => (
<SettingsAccountCard key={account.did} account={account} /> <SettingsAccountCard
key={account.did}
account={account}
onPressSwitchAccount={onPressSwitchAccount}
isSwitchingAccounts={isSwitchingAccounts}
/>
))} ))}
<TouchableOpacity <TouchableOpacity