From 712768dd8f9172ff79700765f9f09db07ca00028 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Thu, 4 Apr 2024 03:18:14 +0100 Subject: [PATCH] Use ALF for the account quick switch dialog (#3327) * Use ALF for account quick switch * clean up modal type * add haptics to dialog opening * move account list to it's own component and share * make tick slightly darker --- src/components/AccountList.tsx | 141 ++++++ src/components/dialogs/BirthDateSettings.tsx | 20 +- src/components/dialogs/SwitchAccount.tsx | 61 +++ src/screens/Login/ChooseAccountForm.tsx | 118 +---- src/state/modals/index.tsx | 11 +- src/view/com/modals/Modal.tsx | 4 - src/view/com/modals/SwitchAccount.tsx | 169 ------- src/view/shell/bottom-bar/BottomBar.tsx | 442 ++++++++++--------- 8 files changed, 449 insertions(+), 517 deletions(-) create mode 100644 src/components/AccountList.tsx create mode 100644 src/components/dialogs/SwitchAccount.tsx delete mode 100644 src/view/com/modals/SwitchAccount.tsx diff --git a/src/components/AccountList.tsx b/src/components/AccountList.tsx new file mode 100644 index 00000000..169e7b84 --- /dev/null +++ b/src/components/AccountList.tsx @@ -0,0 +1,141 @@ +import React, {useCallback} from 'react' +import {View} from 'react-native' +import {msg, Trans} from '@lingui/macro' +import {useLingui} from '@lingui/react' + +import {useProfileQuery} from '#/state/queries/profile' +import {type SessionAccount, useSession} from '#/state/session' +import {UserAvatar} from '#/view/com/util/UserAvatar' +import {atoms as a, useTheme} from '#/alf' +import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check' +import {ChevronRight_Stroke2_Corner0_Rounded as Chevron} from '#/components/icons/Chevron' +import {Button} from './Button' +import {Text} from './Typography' + +export function AccountList({ + onSelectAccount, + onSelectOther, + otherLabel, +}: { + onSelectAccount: (account: SessionAccount) => void + onSelectOther: () => void + otherLabel?: string +}) { + const {isSwitchingAccounts, currentAccount, accounts} = useSession() + const t = useTheme() + const {_} = useLingui() + + const onPressAddAccount = useCallback(() => { + onSelectOther() + }, [onSelectOther]) + + return ( + + {accounts.map(account => ( + + + + + ))} + + + ) +} + +function AccountItem({ + account, + onSelect, + isCurrentAccount, +}: { + account: SessionAccount + onSelect: (account: SessionAccount) => void + isCurrentAccount: boolean +}) { + const t = useTheme() + const {_} = useLingui() + const {data: profile} = useProfileQuery({did: account.did}) + + const onPress = React.useCallback(() => { + onSelect(account) + }, [account, onSelect]) + + return ( + + ) +} diff --git a/src/components/dialogs/BirthDateSettings.tsx b/src/components/dialogs/BirthDateSettings.tsx index 4a3e96e5..d831c600 100644 --- a/src/components/dialogs/BirthDateSettings.tsx +++ b/src/components/dialogs/BirthDateSettings.tsx @@ -1,23 +1,23 @@ import React from 'react' -import {useLingui} from '@lingui/react' -import {Trans, msg} from '@lingui/macro' import {View} from 'react-native' +import {msg, Trans} from '@lingui/macro' +import {useLingui} from '@lingui/react' -import * as Dialog from '#/components/Dialog' -import {Text} from '../Typography' -import {DateInput} from '#/view/com/util/forms/DateInput' +import {cleanError} from '#/lib/strings/errors' import {logger} from '#/logger' +import {isIOS, isWeb} from '#/platform/detection' import { usePreferencesQuery, - usePreferencesSetBirthDateMutation, UsePreferencesQueryResponse, + usePreferencesSetBirthDateMutation, } from '#/state/queries/preferences' -import {Button, ButtonIcon, ButtonText} from '../Button' -import {atoms as a, useTheme} from '#/alf' import {ErrorMessage} from '#/view/com/util/error/ErrorMessage' -import {cleanError} from '#/lib/strings/errors' -import {isIOS, isWeb} from '#/platform/detection' +import {DateInput} from '#/view/com/util/forms/DateInput' +import {atoms as a, useTheme} from '#/alf' +import * as Dialog from '#/components/Dialog' import {Loader} from '#/components/Loader' +import {Button, ButtonIcon, ButtonText} from '../Button' +import {Text} from '../Typography' export function BirthDateSettingsDialog({ control, diff --git a/src/components/dialogs/SwitchAccount.tsx b/src/components/dialogs/SwitchAccount.tsx new file mode 100644 index 00000000..645113d4 --- /dev/null +++ b/src/components/dialogs/SwitchAccount.tsx @@ -0,0 +1,61 @@ +import React, {useCallback} from 'react' +import {View} from 'react-native' +import {msg, Trans} from '@lingui/macro' +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' +import {Text} from '../Typography' + +export function SwitchAccountDialog({ + control, +}: { + control: Dialog.DialogControlProps +}) { + const {_} = useLingui() + const {currentAccount} = useSession() + const {onPressSwitchAccount} = useAccountSwitcher() + const {setShowLoggedOut} = useLoggedOutViewControls() + const closeAllActiveElements = useCloseAllActiveElements() + + const onSelectAccount = useCallback( + (account: SessionAccount) => { + if (account.did === currentAccount?.did) { + control.close() + } else { + onPressSwitchAccount(account, 'SwitchAccount') + } + }, + [currentAccount, control, onPressSwitchAccount], + ) + + const onPressAddAccount = useCallback(() => { + setShowLoggedOut(true) + closeAllActiveElements() + }, [setShowLoggedOut, closeAllActiveElements]) + + return ( + + + + + + + Switch Account + + + + + + + ) +} diff --git a/src/screens/Login/ChooseAccountForm.tsx b/src/screens/Login/ChooseAccountForm.tsx index d0d4c784..15c06dbe 100644 --- a/src/screens/Login/ChooseAccountForm.tsx +++ b/src/screens/Login/ChooseAccountForm.tsx @@ -5,76 +5,15 @@ import {useLingui} from '@lingui/react' import {useAnalytics} from '#/lib/analytics/analytics' import {logEvent} from '#/lib/statsig/statsig' -import {colors} from '#/lib/styles' -import {useProfileQuery} from '#/state/queries/profile' import {SessionAccount, useSession, useSessionApi} from '#/state/session' import {useLoggedOutViewControls} from '#/state/shell/logged-out' import * as Toast from '#/view/com/util/Toast' -import {UserAvatar} from '#/view/com/util/UserAvatar' -import {atoms as a, useTheme} from '#/alf' +import {atoms as a} from '#/alf' +import {AccountList} from '#/components/AccountList' import {Button} from '#/components/Button' import * as TextField from '#/components/forms/TextField' -import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check' -import {ChevronRight_Stroke2_Corner0_Rounded as Chevron} from '#/components/icons/Chevron' -import {Text} from '#/components/Typography' import {FormContainer} from './FormContainer' -function AccountItem({ - account, - onSelect, - isCurrentAccount, -}: { - account: SessionAccount - onSelect: (account: SessionAccount) => void - isCurrentAccount: boolean -}) { - const t = useTheme() - const {_} = useLingui() - const {data: profile} = useProfileQuery({did: account.did}) - - const onPress = React.useCallback(() => { - onSelect(account) - }, [account, onSelect]) - - return ( - - ) -} export const ChooseAccountForm = ({ onSelectAccount, onPressBack, @@ -84,8 +23,7 @@ export const ChooseAccountForm = ({ }) => { const {track, screen} = useAnalytics() const {_} = useLingui() - const t = useTheme() - const {accounts, currentAccount} = useSession() + const {currentAccount} = useSession() const {initSession} = useSessionApi() const {setShowLoggedOut} = useLoggedOutViewControls() @@ -125,52 +63,10 @@ export const ChooseAccountForm = ({ Sign in as... - - {accounts.map(account => ( - - - - - ))} - - + onSelectAccount()} + /> + + - - - - - - - - - )} - + + )} + + ) }