diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/AccountList.tsx | 141 | ||||
-rw-r--r-- | src/components/dialogs/BirthDateSettings.tsx | 20 | ||||
-rw-r--r-- | src/components/dialogs/SwitchAccount.tsx | 61 |
3 files changed, 212 insertions, 10 deletions
diff --git a/src/components/AccountList.tsx b/src/components/AccountList.tsx new file mode 100644 index 000000000..169e7b84f --- /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 ( + <View + style={[ + a.rounded_md, + a.overflow_hidden, + a.border, + t.atoms.border_contrast_low, + ]}> + {accounts.map(account => ( + <React.Fragment key={account.did}> + <AccountItem + account={account} + onSelect={onSelectAccount} + isCurrentAccount={account.did === currentAccount?.did} + /> + <View style={[a.border_b, t.atoms.border_contrast_low]} /> + </React.Fragment> + ))} + <Button + testID="chooseAddAccountBtn" + style={[a.flex_1]} + onPress={isSwitchingAccounts ? undefined : onPressAddAccount} + label={_(msg`Login to account that is not listed`)}> + {({hovered, pressed}) => ( + <View + style={[ + a.flex_1, + a.flex_row, + a.align_center, + {height: 48}, + (hovered || pressed || isSwitchingAccounts) && + t.atoms.bg_contrast_25, + ]}> + <Text + style={[ + a.align_baseline, + a.flex_1, + a.flex_row, + a.py_sm, + {paddingLeft: 48}, + ]}> + {otherLabel ?? <Trans>Other account</Trans>} + </Text> + <Chevron size="sm" style={[t.atoms.text, a.mr_md]} /> + </View> + )} + </Button> + </View> + ) +} + +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 ( + <Button + testID={`chooseAccountBtn-${account.handle}`} + key={account.did} + style={[a.flex_1]} + onPress={onPress} + label={ + isCurrentAccount + ? _(msg`Continue as ${account.handle} (currently signed in)`) + : _(msg`Sign in as ${account.handle}`) + }> + {({hovered, pressed}) => ( + <View + style={[ + a.flex_1, + a.flex_row, + a.align_center, + {height: 48}, + (hovered || pressed) && t.atoms.bg_contrast_25, + ]}> + <View style={a.p_md}> + <UserAvatar avatar={profile?.avatar} size={24} /> + </View> + <Text style={[a.align_baseline, a.flex_1, a.flex_row, a.py_sm]}> + <Text style={[a.font_bold]}> + {profile?.displayName || account.handle}{' '} + </Text> + <Text style={[t.atoms.text_contrast_medium]}>{account.handle}</Text> + </Text> + {isCurrentAccount ? ( + <Check + size="sm" + style={[{color: t.palette.positive_600}, a.mr_md]} + /> + ) : ( + <Chevron size="sm" style={[t.atoms.text, a.mr_md]} /> + )} + </View> + )} + </Button> + ) +} diff --git a/src/components/dialogs/BirthDateSettings.tsx b/src/components/dialogs/BirthDateSettings.tsx index 4a3e96e56..d831c6002 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 000000000..645113d4a --- /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 ( + <Dialog.Outer control={control}> + <Dialog.Handle /> + + <Dialog.ScrollableInner label={_(msg`Switch Account`)}> + <View style={[a.gap_lg]}> + <Text style={[a.text_2xl, a.font_bold]}> + <Trans>Switch Account</Trans> + </Text> + + <AccountList + onSelectAccount={onSelectAccount} + onSelectOther={onPressAddAccount} + otherLabel={_(msg`Add account`)} + /> + </View> + </Dialog.ScrollableInner> + </Dialog.Outer> + ) +} |