-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
InitialSettingsPage.tsx
executable file
·531 lines (495 loc) · 23.9 KB
/
InitialSettingsPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import {useRoute} from '@react-navigation/native';
import React, {useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {GestureResponderEvent, ScrollView as RNScrollView, ScrollViewProps, StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import AvatarWithImagePicker from '@components/AvatarWithImagePicker';
import ConfirmModal from '@components/ConfirmModal';
import CurrentUserPersonalDetailsSkeletonView from '@components/CurrentUserPersonalDetailsSkeletonView';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import {PressableWithFeedback} from '@components/Pressable';
import ScreenWrapper from '@components/ScreenWrapper';
import {ScrollOffsetContext} from '@components/ScrollOffsetContextProvider';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import Tooltip from '@components/Tooltip';
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import useActiveRoute from '@hooks/useActiveRoute';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useSingleExecution from '@hooks/useSingleExecution';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWaitForNavigation from '@hooks/useWaitForNavigation';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import shouldShowSubscriptionsMenu from '@libs/shouldShowSubscriptionsMenu';
import * as UserUtils from '@libs/UserUtils';
import {hasGlobalWorkspaceSettingsRBR} from '@libs/WorkspacesSettingsUtils';
import * as ReportActionContextMenu from '@pages/home/report/ContextMenu/ReportActionContextMenu';
import variables from '@styles/variables';
import * as Link from '@userActions/Link';
import * as PaymentMethods from '@userActions/PaymentMethods';
import * as PersonalDetails from '@userActions/PersonalDetails';
import * as Session from '@userActions/Session';
import * as Wallet from '@userActions/Wallet';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
import type {Icon as TIcon} from '@src/types/onyx/OnyxCommon';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
type InitialSettingsPageOnyxProps = {
/** The user's session */
session: OnyxEntry<OnyxTypes.Session>;
/** The user's wallet account */
userWallet: OnyxEntry<OnyxTypes.UserWallet>;
/** List of bank accounts */
bankAccountList: OnyxEntry<OnyxTypes.BankAccountList>;
/** List of user's cards */
fundList: OnyxEntry<OnyxTypes.FundList>;
/** Information about the user accepting the terms for payments */
walletTerms: OnyxEntry<OnyxTypes.WalletTerms>;
/** Login list for the user that is signed in */
loginList: OnyxEntry<OnyxTypes.LoginList>;
/** The policies which the user has access to */
policies: OnyxCollection<OnyxTypes.Policy>;
};
type InitialSettingsPageProps = InitialSettingsPageOnyxProps & WithCurrentUserPersonalDetailsProps;
type MenuData = {
translationKey: TranslationPaths;
icon: IconAsset;
routeName?: Route;
brickRoadIndicator?: ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS>;
action?: () => void;
link?: string | (() => Promise<string>);
iconType?: typeof CONST.ICON_TYPE_ICON | typeof CONST.ICON_TYPE_AVATAR | typeof CONST.ICON_TYPE_WORKSPACE;
iconStyles?: StyleProp<ViewStyle>;
fallbackIcon?: IconAsset;
shouldStackHorizontally?: boolean;
avatarSize?: (typeof CONST.AVATAR_SIZE)[keyof typeof CONST.AVATAR_SIZE];
floatRightAvatars?: TIcon[];
title?: string;
shouldShowRightIcon?: boolean;
iconRight?: IconAsset;
};
type Menu = {sectionStyle: StyleProp<ViewStyle>; sectionTranslationKey: TranslationPaths; items: MenuData[]};
function InitialSettingsPage({session, userWallet, bankAccountList, fundList, walletTerms, loginList, currentUserPersonalDetails, policies}: InitialSettingsPageProps) {
const network = useNetwork();
const theme = useTheme();
const styles = useThemeStyles();
const {isExecuting, singleExecution} = useSingleExecution();
const waitForNavigate = useWaitForNavigation();
const popoverAnchor = useRef(null);
const {translate, formatPhoneNumber} = useLocalize();
const activeRoute = useActiveRoute();
const emojiCode = currentUserPersonalDetails?.status?.emojiCode ?? '';
const [shouldShowSignoutConfirmModal, setShouldShowSignoutConfirmModal] = useState(false);
useEffect(() => {
Wallet.openInitialSettingsPage();
}, []);
const toggleSignoutConfirmModal = (value: boolean) => {
setShouldShowSignoutConfirmModal(value);
};
const signOut = useCallback(
(shouldForceSignout = false) => {
if (!network.isOffline || shouldForceSignout) {
Session.signOutAndRedirectToSignIn();
return;
}
// When offline, warn the user that any actions they took while offline will be lost if they sign out
toggleSignoutConfirmModal(true);
},
[network.isOffline],
);
/**
* Retuns a list of menu items data for account section
* @returns object with translationKey, style and items for the account section
*/
const accountMenuItemsData: Menu = useMemo(() => {
const profileBrickRoadIndicator = UserUtils.getLoginListBrickRoadIndicator(loginList);
const paymentCardList = fundList;
const defaultMenu: Menu = {
sectionStyle: styles.accountSettingsSectionContainer,
sectionTranslationKey: 'initialSettingsPage.account',
items: [
{
translationKey: 'exitSurvey.goToExpensifyClassic',
icon: Expensicons.ExpensifyLogoNew,
routeName: ROUTES.SETTINGS_EXIT_SURVEY_REASON,
},
{
translationKey: 'common.profile',
icon: Expensicons.Profile,
routeName: ROUTES.SETTINGS_PROFILE,
brickRoadIndicator: profileBrickRoadIndicator,
},
{
translationKey: 'common.wallet',
icon: Expensicons.Wallet,
routeName: ROUTES.SETTINGS_WALLET,
brickRoadIndicator:
PaymentMethods.hasPaymentMethodError(bankAccountList, paymentCardList) || !isEmptyObject(userWallet?.errors) || !isEmptyObject(walletTerms?.errors)
? 'error'
: undefined,
},
{
translationKey: 'common.preferences',
icon: Expensicons.Gear,
routeName: ROUTES.SETTINGS_PREFERENCES,
},
{
translationKey: 'initialSettingsPage.security',
icon: Expensicons.Lock,
routeName: ROUTES.SETTINGS_SECURITY,
},
],
};
return defaultMenu;
}, [loginList, fundList, styles.accountSettingsSectionContainer, bankAccountList, userWallet?.errors, walletTerms?.errors]);
/**
* Retuns a list of menu items data for workspace section
* @returns object with translationKey, style and items for the workspace section
*/
const workspaceMenuItemsData: Menu = useMemo(() => {
const items: MenuData[] = [
{
translationKey: 'common.workspaces',
icon: Expensicons.Building,
routeName: ROUTES.SETTINGS_WORKSPACES,
brickRoadIndicator: hasGlobalWorkspaceSettingsRBR(policies) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined,
},
{
translationKey: 'allSettingsScreen.cardsAndDomains',
icon: Expensicons.CardsAndDomains,
action: () => {
Link.openOldDotLink(CONST.OLDDOT_URLS.ADMIN_DOMAINS_URL);
},
shouldShowRightIcon: true,
iconRight: Expensicons.NewWindow,
link: () => Link.buildOldDotURL(CONST.OLDDOT_URLS.ADMIN_DOMAINS_URL),
},
];
if (shouldShowSubscriptionsMenu) {
items.splice(1, 0, {
translationKey: 'allSettingsScreen.subscriptions',
icon: Expensicons.MoneyBag,
action: () => {
Link.openOldDotLink(CONST.OLDDOT_URLS.ADMIN_POLICIES_URL);
},
shouldShowRightIcon: true,
iconRight: Expensicons.NewWindow,
link: () => Link.buildOldDotURL(CONST.OLDDOT_URLS.ADMIN_POLICIES_URL),
});
}
return {
sectionStyle: styles.workspaceSettingsSectionContainer,
sectionTranslationKey: 'common.workspaces',
items,
};
}, [policies, styles.workspaceSettingsSectionContainer]);
/**
* Retuns a list of menu items data for general section
* @returns object with translationKey, style and items for the general section
*/
const generalMenuItemsData: Menu = useMemo(() => {
const signOutTranslationKey = Session.isSupportAuthToken() && Session.hasStashedSession() ? 'initialSettingsPage.restoreStashed' : 'initialSettingsPage.signOut';
const defaultMenu: Menu = {
sectionStyle: {
...styles.pt4,
},
sectionTranslationKey: 'initialSettingsPage.general',
items: [
{
translationKey: 'initialSettingsPage.help',
icon: Expensicons.QuestionMark,
action: () => {
Link.openExternalLink(CONST.NEWHELP_URL);
},
iconRight: Expensicons.NewWindow,
shouldShowRightIcon: true,
link: CONST.NEWHELP_URL,
},
{
translationKey: 'initialSettingsPage.about',
icon: Expensicons.Info,
routeName: ROUTES.SETTINGS_ABOUT,
},
{
translationKey: 'initialSettingsPage.aboutPage.troubleshoot',
icon: Expensicons.Lightbulb,
routeName: ROUTES.SETTINGS_TROUBLESHOOT,
},
{
translationKey: 'sidebarScreen.saveTheWorld',
icon: Expensicons.Heart,
routeName: ROUTES.SETTINGS_SAVE_THE_WORLD,
},
{
translationKey: signOutTranslationKey,
icon: Expensicons.Exit,
action: () => {
signOut(false);
},
},
],
};
return defaultMenu;
}, [styles.pt4, signOut]);
/**
* Retuns JSX.Element with menu items
* @param menuItemsData list with menu items data
* @returns the menu items for passed data
*/
const getMenuItemsSection = useCallback(
(menuItemsData: Menu) => {
/**
* @param isPaymentItem whether the item being rendered is the payments menu item
* @returns the user's wallet balance
*/
const getWalletBalance = (isPaymentItem: boolean): string | undefined => (isPaymentItem ? CurrencyUtils.convertToDisplayString(userWallet?.currentBalance) : undefined);
const openPopover = (link: string | (() => Promise<string>) | undefined, event: GestureResponderEvent | MouseEvent) => {
if (typeof link === 'function') {
link?.()?.then((url) => ReportActionContextMenu.showContextMenu(CONST.CONTEXT_MENU_TYPES.LINK, event, url, popoverAnchor.current));
} else if (link) {
ReportActionContextMenu.showContextMenu(CONST.CONTEXT_MENU_TYPES.LINK, event, link, popoverAnchor.current);
}
};
return (
<View style={[menuItemsData.sectionStyle, styles.pb4, styles.mh3]}>
<Text style={styles.sectionTitle}>{translate(menuItemsData.sectionTranslationKey)}</Text>
{menuItemsData.items.map((item) => {
const keyTitle = item.translationKey ? translate(item.translationKey) : item.title;
const isPaymentItem = item.translationKey === 'common.wallet';
return (
<MenuItem
key={keyTitle}
wrapperStyle={styles.sectionMenuItem}
title={keyTitle}
icon={item.icon}
iconType={item.iconType}
disabled={isExecuting}
onPress={singleExecution(() => {
if (item.action) {
item.action();
} else {
waitForNavigate(() => {
Navigation.navigate(item.routeName);
})();
}
})}
iconStyles={item.iconStyles}
badgeText={getWalletBalance(isPaymentItem)}
fallbackIcon={item.fallbackIcon}
brickRoadIndicator={item.brickRoadIndicator}
floatRightAvatars={item.floatRightAvatars}
shouldStackHorizontally={item.shouldStackHorizontally}
floatRightAvatarSize={item.avatarSize}
ref={popoverAnchor}
hoverAndPressStyle={styles.hoveredComponentBG}
shouldBlockSelection={Boolean(item.link)}
onSecondaryInteraction={item.link ? (event) => openPopover(item.link, event) : undefined}
focused={
!!activeRoute?.name && !!item.routeName && !!(activeRoute?.name.toLowerCase().replaceAll('_', '') === item.routeName.toLowerCase().replaceAll('/', ''))
}
isPaneMenu
iconRight={item.iconRight}
shouldShowRightIcon={item.shouldShowRightIcon}
/>
);
})}
</View>
);
},
[
styles.pb4,
styles.mh3,
styles.sectionTitle,
styles.sectionMenuItem,
styles.hoveredComponentBG,
translate,
userWallet?.currentBalance,
isExecuting,
singleExecution,
activeRoute,
waitForNavigate,
],
);
const accountMenuItems = useMemo(() => getMenuItemsSection(accountMenuItemsData), [accountMenuItemsData, getMenuItemsSection]);
const generalMenuItems = useMemo(() => getMenuItemsSection(generalMenuItemsData), [generalMenuItemsData, getMenuItemsSection]);
const workspaceMenuItems = useMemo(() => getMenuItemsSection(workspaceMenuItemsData), [workspaceMenuItemsData, getMenuItemsSection]);
const currentUserDetails = currentUserPersonalDetails;
const avatarURL = currentUserDetails?.avatar ?? '';
const accountID = currentUserDetails?.accountID ?? '';
const headerContent = (
<View style={[styles.avatarSectionWrapperSettings, styles.justifyContentCenter, styles.ph5, styles.pb5]}>
{isEmptyObject(currentUserPersonalDetails) || currentUserPersonalDetails.displayName === undefined ? (
<CurrentUserPersonalDetailsSkeletonView avatarSize={CONST.AVATAR_SIZE.XLARGE} />
) : (
<>
<View style={[styles.flexRow, styles.w100, styles.justifyContentBetween, styles.alignItemsCenter, styles.pb5]}>
<Tooltip text={translate('common.shareCode')}>
<PressableWithFeedback
accessibilityLabel={translate('common.shareCode')}
accessibilityRole="button"
accessible
onPress={() => Navigation.navigate(ROUTES.SETTINGS_SHARE_CODE)}
>
<View style={styles.primaryMediumIcon}>
<Icon
src={Expensicons.QrCode}
width={variables.iconSizeNormal}
height={variables.iconSizeNormal}
fill={theme.icon}
/>
</View>
</PressableWithFeedback>
</Tooltip>
<Tooltip text={translate('statusPage.status')}>
<PressableWithFeedback
accessibilityLabel={translate('statusPage.status')}
accessibilityRole="button"
accessible
onPress={() => Navigation.navigate(ROUTES.SETTINGS_STATUS)}
>
<View style={styles.primaryMediumIcon}>
{emojiCode ? (
<Text style={styles.primaryMediumText}>{emojiCode}</Text>
) : (
<Icon
src={Expensicons.Emoji}
width={variables.iconSizeNormal}
height={variables.iconSizeNormal}
fill={theme.icon}
/>
)}
</View>
</PressableWithFeedback>
</Tooltip>
</View>
<View style={[styles.mb3, styles.w100]}>
<AvatarWithImagePicker
isUsingDefaultAvatar={UserUtils.isDefaultAvatar(currentUserDetails?.avatar ?? '')}
source={avatarURL}
avatarID={accountID}
onImageSelected={PersonalDetails.updateAvatar}
onImageRemoved={PersonalDetails.deleteAvatar}
size={CONST.AVATAR_SIZE.XLARGE}
avatarStyle={styles.avatarXLarge}
pendingAction={currentUserPersonalDetails?.pendingFields?.avatar ?? undefined}
errors={currentUserPersonalDetails?.errorFields?.avatar ?? null}
errorRowStyles={styles.mt6}
onErrorClose={PersonalDetails.clearAvatarErrors}
onViewPhotoPress={() => Navigation.navigate(ROUTES.PROFILE_AVATAR.getRoute(String(accountID)))}
previewSource={UserUtils.getFullSizeAvatar(avatarURL, accountID)}
originalFileName={currentUserDetails.originalFileName}
headerTitle={translate('profilePage.profileAvatar')}
fallbackIcon={currentUserDetails?.fallbackIcon}
editIconStyle={styles.smallEditIconAccount}
/>
</View>
<Text
style={[styles.textHeadline, styles.pre, styles.textAlignCenter]}
numberOfLines={1}
>
{currentUserPersonalDetails.displayName ? currentUserPersonalDetails.displayName : formatPhoneNumber(session?.email ?? '')}
</Text>
{Boolean(currentUserPersonalDetails.displayName) && (
<Text
style={[styles.textLabelSupporting, styles.mt1, styles.w100, styles.textAlignCenter]}
numberOfLines={1}
>
{formatPhoneNumber(session?.email ?? '')}
</Text>
)}
</>
)}
</View>
);
const {saveScrollOffset, getScrollOffset} = useContext(ScrollOffsetContext);
const route = useRoute();
const scrollViewRef = useRef<RNScrollView>(null);
const onScroll = useCallback<NonNullable<ScrollViewProps['onScroll']>>(
(e) => {
// If the layout measurement is 0, it means the flashlist is not displayed but the onScroll may be triggered with offset value 0.
// We should ignore this case.
if (e.nativeEvent.layoutMeasurement.height === 0) {
return;
}
saveScrollOffset(route, e.nativeEvent.contentOffset.y);
},
[route, saveScrollOffset],
);
useLayoutEffect(() => {
const scrollOffset = getScrollOffset(route);
if (!scrollOffset || !scrollViewRef.current) {
return;
}
scrollViewRef.current.scrollTo({y: scrollOffset, animated: false});
}, [getScrollOffset, route]);
return (
<ScreenWrapper
style={[styles.w100, styles.pb0]}
includePaddingTop={false}
includeSafeAreaPaddingBottom={false}
testID={InitialSettingsPage.displayName}
>
<ScrollView
ref={scrollViewRef}
onScroll={onScroll}
scrollEventThrottle={16}
contentContainerStyle={[styles.w100, styles.pt4]}
>
{headerContent}
{accountMenuItems}
{workspaceMenuItems}
{generalMenuItems}
<ConfirmModal
danger
title={translate('common.areYouSure')}
prompt={translate('initialSettingsPage.signOutConfirmationText')}
confirmText={translate('initialSettingsPage.signOut')}
cancelText={translate('common.cancel')}
isVisible={shouldShowSignoutConfirmModal}
onConfirm={() => signOut(true)}
onCancel={() => toggleSignoutConfirmModal(false)}
/>
</ScrollView>
</ScreenWrapper>
);
}
InitialSettingsPage.displayName = 'InitialSettingsPage';
export default withCurrentUserPersonalDetails(
withOnyx<InitialSettingsPageProps, InitialSettingsPageOnyxProps>({
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
fundList: {
key: ONYXKEYS.FUND_LIST,
},
walletTerms: {
key: ONYXKEYS.WALLET_TERMS,
},
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
})(InitialSettingsPage),
);