Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions src/libs/Firebase/utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
import type {OnyxCollection} from 'react-native-onyx';
// We have opted for `Onyx.connectWithoutView` here as this logic is strictly non-UI in nature.
import Onyx from 'react-native-onyx';
import {getAllTransactions, getAllTransactionViolationsLength} from '@libs/actions/Transaction';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import {getActivePolicy, getAllPoliciesLength} from '@libs/PolicyUtils';
import {getReportActionsLength} from '@libs/ReportActionsUtils';
import {getActivePolicy} from '@libs/PolicyUtils';
import * as SessionUtils from '@libs/SessionUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report} from '@src/types/onyx';
import type {PerfAttributes} from './types';

let allReports: OnyxCollection<Report>;
Onyx.connect({
let reportsCount = 0;
Onyx.connectWithoutView({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
reportsCount = Object.keys(value ?? {}).length;
},
});

let reportActionsCount = 0;
Onyx.connectWithoutView({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (value) => {
reportActionsCount = Object.keys(value ?? {}).length;
},
});

let transactionViolationsCount = 0;
Onyx.connectWithoutView({
key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS,
waitForCollectionCallback: true,
callback: (value) => {
transactionViolationsCount = Object.keys(value ?? {}).length;
},
});

let transactionsCount = 0;
Onyx.connectWithoutView({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
callback: (value) => {
transactionsCount = Object.keys(value ?? {}).length;
},
});

let policiesCount = 0;
Onyx.connectWithoutView({
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => {
policiesCount = Object.keys(value ?? {}).length;
},
});

let personalDetailsCount = 0;
Onyx.connectWithoutView({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
personalDetailsCount = Object.keys(value ?? {}).length;
Comment on lines +17 to +57

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, why did we decide that this is a better approach than what we had before?

By doing this, we're adding more dependencies to those onyx objects instead of using the other utils file that already had those dependencies. Won't this end up using more memory in the app?

Also we now have another process of creating an array (using keys function) to calculate the length, compared to using the utils function that would do .length in an existing array.

asking this because we're trying to reduce usages of Object.keys (https://expensify.slack.com/archives/C05LX9D6E07/p1753812175447629?thread_ts=1753810372.474349&cid=C05LX9D6E07)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll ask for @mountiny @luacmartins @tgolen's opinions here too.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danieldoglas Yeah, good catch! With this approach, it could create multiple subscriptions for the same key. We should subscribe to the field in only one place and reuse it across the app. I think we can create a new file to handle the Onyx subscription, and other files can access the Onyx data from that single subscription.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DylanDylann No, we need to remove nearly all existing Onyx.connect. @danieldoglas concern is about memory consumption for the time it has duplicated connections.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is quite a specific use case indeed. We could keep using the getters, but I dont think adding these individual connects would have a lot of impact on performance either

},
});

Expand All @@ -24,12 +64,12 @@ function getAttributes<T extends keyof PerfAttributes>(attributes?: T[]): Pick<P

const allAttributes: PerfAttributes = {
accountId: session?.accountID?.toString() ?? 'N/A',
reportsLength: Object.keys(allReports ?? {}).length.toString(),
reportActionsLength: getReportActionsLength().toString(),
personalDetailsLength: PersonalDetailsUtils.getPersonalDetailsLength().toString(),
transactionViolationsLength: getAllTransactionViolationsLength().toString(),
policiesLength: getAllPoliciesLength().toString(),
transactionsLength: getAllTransactions().toString(),
reportsLength: reportsCount.toString(),
reportActionsLength: reportActionsCount.toString(),
personalDetailsLength: personalDetailsCount.toString(),
transactionViolationsLength: transactionViolationsCount.toString(),
policiesLength: policiesCount.toString(),
transactionsLength: transactionsCount.toString(),
policyType: policy?.type ?? 'N/A',
policyRole: policy?.role ?? 'N/A',
};
Expand Down
13 changes: 0 additions & 13 deletions src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
let personalDetails: Array<PersonalDetails | null> = [];
let allPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
let emailToPersonalDetailsCache: Record<string, PersonalDetails> = {};
Onyx.connect({

Check warning on line 24 in src/libs/PersonalDetailsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => {
personalDetails = Object.values(val ?? {});
Expand All @@ -38,7 +38,7 @@
let hiddenTranslation = '';
let youTranslation = '';

Onyx.connect({

Check warning on line 41 in src/libs/PersonalDetailsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.ARE_TRANSLATIONS_LOADING,
initWithStoredValues: false,
callback: (value) => {
Expand All @@ -52,7 +52,7 @@

let defaultCountry = '';

Onyx.connect({

Check warning on line 55 in src/libs/PersonalDetailsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COUNTRY,
callback: (value) => {
if (!value) {
Expand Down Expand Up @@ -390,17 +390,6 @@
return {firstName: '', lastName: ''};
}

/**
* Whether personal details is empty
*/
function isPersonalDetailsEmpty() {
return !personalDetails.length;
}

function getPersonalDetailsLength() {
return personalDetails.length;
}

function getUserNameByEmail(email: string, nameToDisplay: 'firstName' | 'displayName') {
const userDetails = getPersonalDetailByEmail(email);
if (userDetails) {
Expand Down Expand Up @@ -446,7 +435,6 @@
};

export {
isPersonalDetailsEmpty,
getDisplayNameOrDefault,
getPersonalDetailsByIDs,
getPersonalDetailByEmail,
Expand All @@ -461,7 +449,6 @@
createDisplayName,
extractFirstAndLastNameFromAvailableDetails,
getNewAccountIDsAndLogins,
getPersonalDetailsLength,
getUserNameByEmail,
getShortMentionIfFound,
getDefaultCountry,
Expand Down
5 changes: 0 additions & 5 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@
let activePolicyId: OnyxEntry<string>;
let isLoadingReportData = true;

Onyx.connect({

Check warning on line 62 in src/libs/PolicyUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
});

Onyx.connect({

Check warning on line 68 in src/libs/PolicyUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (activePolicyId = value),
});

Onyx.connect({

Check warning on line 73 in src/libs/PolicyUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
initWithStoredValues: false,
callback: (value) => (isLoadingReportData = value ?? false),
Expand Down Expand Up @@ -1331,10 +1331,6 @@
return policy?.approvalMode === CONST.POLICY.APPROVAL_MODE.OPTIONAL || !!policy?.errorFields?.approvalMode;
}

function getAllPoliciesLength() {
return Object.keys(allPolicies ?? {}).length;
}

function getActivePolicy(): OnyxEntry<Policy> {
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
// eslint-disable-next-line deprecation/deprecation
Expand Down Expand Up @@ -1635,7 +1631,6 @@
hasUnsupportedIntegration,
getWorkflowApprovalsUnavailable,
getNetSuiteImportCustomFieldLabel,
getAllPoliciesLength,
getActivePolicy,
getUserFriendlyWorkspaceType,
isPolicyAccessible,
Expand Down
5 changes: 0 additions & 5 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
type MemberChangeMessageElement = MessageTextElement | MemberChangeMessageUserMentionElement | MemberChangeMessageRoomReferenceElement;

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 60 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand All @@ -69,7 +69,7 @@
});

let allReports: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 72 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -78,14 +78,14 @@
});

let isNetworkOffline = false;
Onyx.connect({

Check warning on line 81 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NETWORK,
callback: (val) => (isNetworkOffline = val?.isOffline ?? false),
});

let currentUserAccountID: number | undefined;
let currentEmail = '';
Onyx.connect({

Check warning on line 88 in src/libs/ReportActionsUtils.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
// When signed out, value is undefined
Expand Down Expand Up @@ -2910,10 +2910,6 @@
}
}

function getReportActionsLength() {
return Object.keys(allReportActions ?? {}).length;
}

function getReportActions(report: Report) {
return allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`];
}
Expand Down Expand Up @@ -3125,7 +3121,6 @@
getCardIssuedMessage,
getRemovedConnectionMessage,
getActionableJoinRequestPendingReportAction,
getReportActionsLength,
getFilteredReportActionsForReportView,
wasMessageReceivedWhileOffline,
shouldShowAddMissingDetails,
Expand Down
10 changes: 0 additions & 10 deletions src/libs/actions/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,14 +584,6 @@ function getRecentWaypoints() {
return recentWaypoints;
}

function getAllTransactionViolationsLength() {
return allTransactionViolations.length;
}

function getAllTransactions() {
return Object.keys(allTransactions ?? {}).length;
}

/**
* Returns a client generated 16 character hexadecimal value for the transactionID
*/
Expand Down Expand Up @@ -1069,8 +1061,6 @@ export {
openDraftDistanceExpense,
getRecentWaypoints,
sanitizeRecentWaypoints,
getAllTransactionViolationsLength,
getAllTransactions,
getLastModifiedExpense,
revert,
changeTransactionsReport,
Expand Down
Loading