Skip to content

Commit b7d429f

Browse files
authored
Merge pull request Expensify#67724 from DylanDylann/remove-connect-method-in-userutil
Refactor src/libs/UserUtils.ts to remove Onyx.connect() references
2 parents 1bb8a27 + 2da831d commit b7d429f

12 files changed

Lines changed: 380 additions & 50 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand",
4747
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
4848
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
49-
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=276 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=274 --cache --cache-location=node_modules/.cache/eslint",
5050
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
5151
"lint-watch": "npx eslint-watch --watch --changed",
5252
"shellcheck": "./scripts/shellCheck.sh",

src/hooks/useAccountTabIndicatorStatus.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function useAccountTabIndicatorStatus(): AccountTabIndicatorStatusResult {
2525
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS, {canBeMissing: true});
2626
const [allCards] = useOnyx(`${ONYXKEYS.CARD_LIST}`, {canBeMissing: true});
2727
const hasBrokenFeedConnection = checkIfFeedConnectionIsBroken(allCards, CONST.EXPENSIFY_CARD.BANK);
28+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true});
2829

2930
// All of the error & info-checking methods are put into an array. This is so that using _.some() will return
3031
// early as soon as the first error / info condition is returned. This makes the checks very efficient since
@@ -41,7 +42,7 @@ function useAccountTabIndicatorStatus(): AccountTabIndicatorStatusResult {
4142
};
4243

4344
const infoChecking: Partial<Record<AccountTabIndicatorStatus, boolean>> = {
44-
[CONST.INDICATOR_STATUS.HAS_LOGIN_LIST_INFO]: !!loginList && hasLoginListInfo(loginList),
45+
[CONST.INDICATOR_STATUS.HAS_LOGIN_LIST_INFO]: !!loginList && hasLoginListInfo(loginList, session?.email),
4546
};
4647

4748
const [error] = Object.entries(errorChecking).find(([, value]) => value) ?? [];

src/hooks/useIndicatorStatus.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ type IndicatorStatusResult = {
2222

2323
function useIndicatorStatus(): IndicatorStatusResult {
2424
const theme = useTheme();
25-
const [allConnectionSyncProgresses] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS);
26-
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY);
27-
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
28-
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT);
29-
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST);
30-
const [userWallet] = useOnyx(ONYXKEYS.USER_WALLET);
31-
const [walletTerms] = useOnyx(ONYXKEYS.WALLET_TERMS);
32-
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
33-
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS);
34-
const [allCards] = useOnyx(`${ONYXKEYS.CARD_LIST}`);
25+
const [allConnectionSyncProgresses] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CONNECTION_SYNC_PROGRESS, {canBeMissing: true});
26+
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
27+
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true});
28+
const [reimbursementAccount] = useOnyx(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {canBeMissing: true});
29+
const [fundList] = useOnyx(ONYXKEYS.FUND_LIST, {canBeMissing: true});
30+
const [userWallet] = useOnyx(ONYXKEYS.USER_WALLET, {canBeMissing: true});
31+
const [walletTerms] = useOnyx(ONYXKEYS.WALLET_TERMS, {canBeMissing: true});
32+
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST, {canBeMissing: true});
33+
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS, {canBeMissing: true});
34+
const [allCards] = useOnyx(`${ONYXKEYS.CARD_LIST}`, {canBeMissing: true});
3535
const hasBrokenFeedConnection = checkIfFeedConnectionIsBroken(allCards, CONST.EXPENSIFY_CARD.BANK);
36+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true});
3637

3738
// If a policy was just deleted from Onyx, then Onyx will pass a null value to the props, and
3839
// those should be cleaned out before doing any error checking
@@ -65,7 +66,7 @@ function useIndicatorStatus(): IndicatorStatusResult {
6566
};
6667

6768
const infoChecking: Partial<Record<IndicatorStatus, boolean>> = {
68-
[CONST.INDICATOR_STATUS.HAS_LOGIN_LIST_INFO]: !!loginList && hasLoginListInfo(loginList),
69+
[CONST.INDICATOR_STATUS.HAS_LOGIN_LIST_INFO]: !!loginList && hasLoginListInfo(loginList, session?.email),
6970
[CONST.INDICATOR_STATUS.HAS_SUBSCRIPTION_INFO]: hasSubscriptionGreenDotInfo(),
7071
};
7172

src/libs/UserUtils.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {Str} from 'expensify-common';
22
import type {OnyxEntry} from 'react-native-onyx';
3-
import Onyx from 'react-native-onyx';
43
import type {ValueOf} from 'type-fest';
54
import * as defaultAvatars from '@components/Icon/DefaultAvatars';
65
import {ConciergeAvatar, NotificationsAvatar} from '@components/Icon/Expensicons';
76
import CONST from '@src/CONST';
8-
import ONYXKEYS from '@src/ONYXKEYS';
9-
import type {Account, LoginList, PrivatePersonalDetails, Session, VacationDelegate} from '@src/types/onyx';
7+
import type {LoginList, PrivatePersonalDetails, VacationDelegate} from '@src/types/onyx';
108
import type Login from '@src/types/onyx/Login';
119
import {isEmptyObject} from '@src/types/utils/EmptyObject';
1210
import type IconAsset from '@src/types/utils/IconAsset';
@@ -18,22 +16,6 @@ type AvatarSource = IconAsset | string;
1816

1917
type LoginListIndicator = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | undefined;
2018

21-
let account: OnyxEntry<Account>;
22-
Onyx.connect({
23-
key: ONYXKEYS.ACCOUNT,
24-
callback: (value) => {
25-
account = value ?? {};
26-
},
27-
});
28-
29-
let session: OnyxEntry<Session>;
30-
Onyx.connect({
31-
key: ONYXKEYS.SESSION,
32-
callback: (value) => {
33-
session = value ?? {};
34-
},
35-
});
36-
3719
/**
3820
* Searches through given loginList for any contact method / login with an error.
3921
*
@@ -64,30 +46,30 @@ function hasLoginListError(loginList: OnyxEntry<LoginList>): boolean {
6446
* an Info brick road status indicator. Currently this only applies if the user
6547
* has an unvalidated contact method.
6648
*/
67-
function hasLoginListInfo(loginList: OnyxEntry<LoginList>): boolean {
68-
return Object.values(loginList ?? {}).some((login) => session?.email !== login.partnerUserID && !login.validatedDate);
49+
function hasLoginListInfo(loginList: OnyxEntry<LoginList>, email: string | undefined): boolean {
50+
return Object.values(loginList ?? {}).some((login) => email !== login.partnerUserID && !login.validatedDate);
6951
}
7052

7153
/**
7254
* Checks if the current user has a validated the primary contact method
7355
*/
74-
function isCurrentUserValidated(loginList: OnyxEntry<LoginList>): boolean {
75-
if (!loginList || !session?.email) {
56+
function isCurrentUserValidated(loginList: OnyxEntry<LoginList>, email: string | undefined): boolean {
57+
if (!loginList || !email) {
7658
return false;
7759
}
7860

79-
return !!loginList?.[session.email]?.validatedDate;
61+
return !!loginList?.[email]?.validatedDate;
8062
}
8163

8264
/**
8365
* Gets the appropriate brick road indicator status for a given loginList.
8466
* Error status is higher priority, so we check for that first.
8567
*/
86-
function getLoginListBrickRoadIndicator(loginList: OnyxEntry<LoginList>): LoginListIndicator {
68+
function getLoginListBrickRoadIndicator(loginList: OnyxEntry<LoginList>, email: string | undefined): LoginListIndicator {
8769
if (hasLoginListError(loginList)) {
8870
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
8971
}
90-
if (hasLoginListInfo(loginList)) {
72+
if (hasLoginListInfo(loginList, email)) {
9173
return CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
9274
}
9375

@@ -102,12 +84,13 @@ function getProfilePageBrickRoadIndicator(
10284
loginList: OnyxEntry<LoginList>,
10385
privatePersonalDetails: OnyxEntry<PrivatePersonalDetails>,
10486
vacationDelegate: OnyxEntry<VacationDelegate>,
87+
email: string | undefined,
10588
): LoginListIndicator {
10689
const hasPhoneNumberError = !!privatePersonalDetails?.errorFields?.phoneNumber;
10790
if (hasLoginListError(loginList) || hasPhoneNumberError || !isEmptyObject(vacationDelegate?.errors)) {
10891
return CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
10992
}
110-
if (hasLoginListInfo(loginList)) {
93+
if (hasLoginListInfo(loginList, email)) {
11194
return CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
11295
}
11396

@@ -262,8 +245,8 @@ function getSecondaryPhoneLogin(loginList: OnyxEntry<Login>): string | undefined
262245
/**
263246
* Gets the contact method
264247
*/
265-
function getContactMethod(): string {
266-
return account?.primaryLogin ?? session?.email ?? '';
248+
function getContactMethod(primaryLogin: string | undefined, email: string | undefined): string {
249+
return primaryLogin ?? email ?? '';
267250
}
268251

269252
export {

src/pages/OnboardingPersonalDetails/BaseOnboardingPersonalDetails.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function BaseOnboardingPersonalDetails({currentUserPersonalDetails, shouldUseNat
4141
const [onboardingValues] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {canBeMissing: true});
4242
const [conciergeChatReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID, {canBeMissing: true});
4343
const {onboardingMessages} = useOnboardingMessages();
44+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true});
4445

4546
// When we merge public email with work email, we now want to navigate to the
4647
// concierge chat report of the new work email and not the last accessed report.
@@ -53,7 +54,7 @@ function BaseOnboardingPersonalDetails({currentUserPersonalDetails, shouldUseNat
5354
const {isBetaEnabled} = usePermissions();
5455

5556
const isPrivateDomainAndHasAccessiblePolicies = !account?.isFromPublicDomain && !!account?.hasAccessibleDomainPolicies;
56-
const isValidated = isCurrentUserValidated(loginList);
57+
const isValidated = isCurrentUserValidated(loginList, session?.email);
5758

5859
const isVsb = onboardingValues?.signupQualifier === CONST.ONBOARDING_SIGNUP_QUALIFIERS.VSB;
5960
const isSmb = onboardingValues?.signupQualifier === CONST.ONBOARDING_SIGNUP_QUALIFIERS.SMB;

src/pages/OnboardingPrivateDomain/BaseOnboardingPrivateDomain.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function BaseOnboardingPrivateDomain({shouldUseNativeStyles, route}: BaseOnboard
2323
const styles = useThemeStyles();
2424
const {translate} = useLocalize();
2525
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST, {canBeMissing: false});
26-
2726
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
2827

2928
const [getAccessiblePoliciesAction] = useOnyx(ONYXKEYS.VALIDATE_USER_AND_GET_ACCESSIBLE_POLICIES, {canBeMissing: true});
@@ -35,7 +34,7 @@ function BaseOnboardingPrivateDomain({shouldUseNativeStyles, route}: BaseOnboard
3534
const email = session?.email ?? '';
3635
const domain = email.split('@').at(1) ?? '';
3736

38-
const isValidated = isCurrentUserValidated(loginList);
37+
const isValidated = isCurrentUserValidated(loginList, session?.email);
3938

4039
const [onboardingValues] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {canBeMissing: true});
4140
const isVsb = onboardingValues?.signupQualifier === CONST.ONBOARDING_SIGNUP_QUALIFIERS.VSB;

src/pages/OnboardingWorkspaces/BaseOnboardingWorkspaces.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ function BaseOnboardingWorkspaces({route, shouldUseNativeStyles}: BaseOnboarding
4747
const [onboardingPersonalDetails] = useOnyx(ONYXKEYS.FORMS.ONBOARDING_PERSONAL_DETAILS_FORM, {canBeMissing: true});
4848

4949
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST, {canBeMissing: true});
50+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: true});
5051

51-
const isValidated = isCurrentUserValidated(loginList);
52+
const isValidated = isCurrentUserValidated(loginList, session?.email);
5253

5354
const {isBetaEnabled} = usePermissions();
5455

src/pages/settings/InitialSettingsPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
9393
const [tryNewDot] = useOnyx(ONYXKEYS.NVP_TRY_NEW_DOT, {canBeMissing: true});
9494
const [allCards] = useOnyx(ONYXKEYS.CARD_LIST, {canBeMissing: true});
9595
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true});
96+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
9697

9798
const {shouldUseNarrowLayout} = useResponsiveLayout();
9899
const network = useNetwork();
@@ -150,7 +151,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
150151
* @returns object with translationKey, style and items for the account section
151152
*/
152153
const accountMenuItemsData: Menu = useMemo(() => {
153-
const profileBrickRoadIndicator = getProfilePageBrickRoadIndicator(loginList, privatePersonalDetails, vacationDelegate);
154+
const profileBrickRoadIndicator = getProfilePageBrickRoadIndicator(loginList, privatePersonalDetails, vacationDelegate, session?.email);
154155
const items: MenuData[] = [
155156
{
156157
translationKey: 'common.profile',
@@ -202,6 +203,7 @@ function InitialSettingsPage({currentUserPersonalDetails}: InitialSettingsPagePr
202203
loginList,
203204
privatePersonalDetails,
204205
vacationDelegate,
206+
session?.email,
205207
walletBrickRoadIndicator,
206208
hasActivatedWallet,
207209
userWallet?.currentBalance,

src/pages/settings/Profile/Contacts/NewContactMethodPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ import type {Errors} from '@src/types/onyx/OnyxCommon';
4444
type NewContactMethodPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.NEW_CONTACT_METHOD>;
4545

4646
function NewContactMethodPage({route}: NewContactMethodPageProps) {
47-
const contactMethod = getContactMethod();
47+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
48+
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: false});
49+
const contactMethod = getContactMethod(account?.primaryLogin, session?.email);
4850
const styles = useThemeStyles();
4951
const {translate} = useLocalize();
5052
const loginInputRef = useRef<AnimatedTextInputRef>(null);

src/pages/settings/Profile/ProfilePage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function ProfilePage() {
4646
const scrollEnabled = useScrollEnabled();
4747
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST, {canBeMissing: true});
4848
const [privatePersonalDetails] = useOnyx(ONYXKEYS.PRIVATE_PERSONAL_DETAILS, {canBeMissing: false});
49+
const [session] = useOnyx(ONYXKEYS.SESSION, {canBeMissing: false});
4950
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
5051
const route = useRoute<PlatformStackRouteProp<SettingsSplitNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.ROOT>>();
5152
const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP, {canBeMissing: false});
@@ -57,7 +58,7 @@ function ProfilePage() {
5758
const avatarURL = currentUserPersonalDetails?.avatar ?? '';
5859
const accountID = currentUserPersonalDetails?.accountID ?? CONST.DEFAULT_NUMBER_ID;
5960

60-
const contactMethodBrickRoadIndicator = getLoginListBrickRoadIndicator(loginList);
61+
const contactMethodBrickRoadIndicator = getLoginListBrickRoadIndicator(loginList, session?.email);
6162
const emojiCode = currentUserPersonalDetails?.status?.emojiCode ?? '';
6263
const privateDetails = privatePersonalDetails ?? {};
6364
const legalName = `${privateDetails.legalFirstName ?? ''} ${privateDetails.legalLastName ?? ''}`.trim();

0 commit comments

Comments
 (0)