From c826b13849a22d57a68736800cd30253ce22c995 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 18 Mar 2024 16:45:48 +0500 Subject: [PATCH 01/11] perf: use waitForCollectionCallback --- src/libs/OptionsListUtils.ts | 10 ++++------ src/libs/ReportActionsUtils.ts | 12 ++++-------- src/libs/TaskUtils.ts | 11 ++++------- src/libs/actions/PriorityMode.ts | 17 ++++------------- src/libs/actions/Welcome.ts | 11 ++++------- 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index e3e989dd877b..f02bfac0ad28 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -248,14 +248,12 @@ Onyx.connect({ }, }); -const policyExpenseReports: OnyxCollection = {}; +let policyExpenseReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!ReportUtils.isPolicyExpenseChat(report)) { - return; - } - policyExpenseReports[key] = report; + waitForCollectionCallback: true, + callback: (report) => { + policyExpenseReports = report; }, }); diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index fbee7ffd7b11..fb5809b16834 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -50,16 +50,12 @@ type MemberChangeMessageElement = MessageTextElement | MemberChangeMessageUserMe const policyChangeActionsSet = new Set(Object.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG)); -const allReports: OnyxCollection = {}; +let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!key || !report) { - return; - } - - const reportID = CollectionUtils.extractCollectionItemID(key); - allReports[reportID] = report; + waitForCollectionCallback: true, + callback: (report) => { + allReports = report; }, }); diff --git a/src/libs/TaskUtils.ts b/src/libs/TaskUtils.ts index 81a079003d0e..fc05707c0001 100644 --- a/src/libs/TaskUtils.ts +++ b/src/libs/TaskUtils.ts @@ -8,15 +8,12 @@ import type ReportAction from '@src/types/onyx/ReportAction'; import * as CollectionUtils from './CollectionUtils'; import * as Localize from './Localize'; -const allReports: Record = {}; +let allReports: Record = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!key || !report) { - return; - } - const reportID = CollectionUtils.extractCollectionItemID(key); - allReports[reportID] = report; + waitForCollectionCallback: true, + callback: (report) => { + allReports = report; }, }); diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index 7ae174ac8606..d40a5d2af675 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -36,21 +36,12 @@ Onyx.connect({ // eslint-disable-next-line @typescript-eslint/no-use-before-define const autoSwitchToFocusMode = debounce(tryFocusModeUpdate, 300, {leading: true}); -let allReports: OnyxCollection | undefined; +let allReports: OnyxCollection | undefined = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, - callback: (report, key) => { - if (!key || !report) { - return; - } - - if (!allReports) { - allReports = {}; - } - - const reportID = CollectionUtils.extractCollectionItemID(key); - - allReports[reportID] = report; + waitForCollectionCallback: true, + callback: (report) => { + allReports = report; // Each time a new report is added we will check to see if the user should be switched autoSwitchToFocusMode(); diff --git a/src/libs/actions/Welcome.ts b/src/libs/actions/Welcome.ts index 89d607645fb6..a637d12ed846 100644 --- a/src/libs/actions/Welcome.ts +++ b/src/libs/actions/Welcome.ts @@ -82,16 +82,13 @@ Onyx.connect({ }, }); -const allReports: OnyxCollection = {}; +let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, initWithStoredValues: false, - callback: (val, key) => { - if (!val || !key) { - return; - } - - allReports[key] = {...allReports[key], ...val}; + waitForCollectionCallback: true, + callback: (val) => { + allReports = val; }, }); From decdb46333d0a5da74f552cbc79f5c85c405b692 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 18 Mar 2024 16:46:33 +0500 Subject: [PATCH 02/11] perf: remove redundant report connection --- src/libs/actions/Report.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index f09b5c08b166..2eb74a89770b 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -150,6 +150,7 @@ Onyx.connect({ } const reportID = CollectionUtils.extractCollectionItemID(key); currentReportData[reportID] = report; + handleReportChanged(report); }, }); @@ -1124,11 +1125,6 @@ function handleReportChanged(report: OnyxEntry) { } } -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - callback: handleReportChanged, -}); - /** Deletes a comment from the report, basically sets it as empty string */ function deleteReportComment(reportID: string, reportAction: ReportAction) { const originalReportID = ReportUtils.getOriginalReportID(reportID, reportAction); From a9f7d46ce10ece850b33129034e7dacfbea49b7f Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 18 Mar 2024 16:47:13 +0500 Subject: [PATCH 03/11] perf: increase max cached keys count --- src/setup/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/setup/index.ts b/src/setup/index.ts index f79e6a461a3e..581dbd28056f 100644 --- a/src/setup/index.ts +++ b/src/setup/index.ts @@ -27,7 +27,7 @@ export default function () { keys: ONYXKEYS, // Increase the cached key count so that the app works more consistently for accounts with large numbers of reports - maxCachedKeysCount: 10000, + maxCachedKeysCount: 20000, safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], captureMetrics: Metrics.canCaptureOnyxMetrics(), initialKeyStates: { From 8078034ba86f4c61e94d4433d261cd6332560cbd Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 18 Mar 2024 18:02:10 +0500 Subject: [PATCH 04/11] fix: linting --- src/libs/TaskUtils.ts | 9 ++++----- src/libs/actions/PriorityMode.ts | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/libs/TaskUtils.ts b/src/libs/TaskUtils.ts index fc05707c0001..1a5a7810ffdd 100644 --- a/src/libs/TaskUtils.ts +++ b/src/libs/TaskUtils.ts @@ -1,14 +1,13 @@ -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report} from '@src/types/onyx'; import type {Message} from '@src/types/onyx/ReportAction'; import type ReportAction from '@src/types/onyx/ReportAction'; -import * as CollectionUtils from './CollectionUtils'; import * as Localize from './Localize'; -let allReports: Record = {}; +let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, @@ -39,11 +38,11 @@ function getTaskReportActionMessage(action: OnyxEntry): Pick) { diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index d40a5d2af675..d30a13a716fa 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -1,7 +1,6 @@ import debounce from 'lodash/debounce'; import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; -import * as CollectionUtils from '@libs/CollectionUtils'; import Log from '@libs/Log'; import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; From bbf4bee334adff04a585ab81f1b30364e26fc84c Mon Sep 17 00:00:00 2001 From: hurali97 Date: Tue, 19 Mar 2024 12:50:31 +0500 Subject: [PATCH 05/11] fix: add disable eslint for handleReportChanged --- src/libs/actions/Report.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 2eb74a89770b..5f5a10e87daa 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -150,6 +150,7 @@ Onyx.connect({ } const reportID = CollectionUtils.extractCollectionItemID(key); currentReportData[reportID] = report; + // eslint-disable-next-line @typescript-eslint/no-use-before-define handleReportChanged(report); }, }); From c9cb60a83e8535e95b63fc661f985a4b7b8abdfb Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 20 Mar 2024 17:21:36 +0500 Subject: [PATCH 06/11] refactor: update report to reports --- src/libs/OptionsListUtils.ts | 4 ++-- src/libs/ReportActionsUtils.ts | 4 ++-- src/libs/TaskUtils.ts | 4 ++-- src/libs/actions/PriorityMode.ts | 4 ++-- src/libs/actions/Welcome.ts | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 73620a2b6f15..7d353efe88be 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -252,8 +252,8 @@ let policyExpenseReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, - callback: (report) => { - policyExpenseReports = report; + callback: (reports) => { + policyExpenseReports = reports; }, }); diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index fb5809b16834..72ccc845d2ad 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -54,8 +54,8 @@ let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, - callback: (report) => { - allReports = report; + callback: (reports) => { + allReports = reports; }, }); diff --git a/src/libs/TaskUtils.ts b/src/libs/TaskUtils.ts index 1a5a7810ffdd..7cbc9f2dc1f8 100644 --- a/src/libs/TaskUtils.ts +++ b/src/libs/TaskUtils.ts @@ -11,8 +11,8 @@ let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, - callback: (report) => { - allReports = report; + callback: (reports) => { + allReports = reports; }, }); diff --git a/src/libs/actions/PriorityMode.ts b/src/libs/actions/PriorityMode.ts index d30a13a716fa..77347967b6cd 100644 --- a/src/libs/actions/PriorityMode.ts +++ b/src/libs/actions/PriorityMode.ts @@ -39,8 +39,8 @@ let allReports: OnyxCollection | undefined = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, - callback: (report) => { - allReports = report; + callback: (reports) => { + allReports = reports; // Each time a new report is added we will check to see if the user should be switched autoSwitchToFocusMode(); diff --git a/src/libs/actions/Welcome.ts b/src/libs/actions/Welcome.ts index a637d12ed846..150b23a4a0d9 100644 --- a/src/libs/actions/Welcome.ts +++ b/src/libs/actions/Welcome.ts @@ -87,8 +87,8 @@ Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, initWithStoredValues: false, waitForCollectionCallback: true, - callback: (val) => { - allReports = val; + callback: (reports) => { + allReports = reports; }, }); From 95ae11e1b2eb97d2c5fa31abf71ab0629178d011 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Mon, 25 Mar 2024 19:04:19 +0500 Subject: [PATCH 07/11] refactor: remove unnecessary variables for reports --- src/libs/OptionsListUtils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index c9ca5ac53612..9a95a48ed543 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -248,13 +248,11 @@ Onyx.connect({ }, }); -let policyExpenseReports: OnyxCollection = {}; let allReports: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, callback: (reports) => { - policyExpenseReports = reports; allReports = reports; }, }); @@ -768,7 +766,7 @@ function getReportOption(participant: Participant): ReportUtils.OptionData { * Get the option for a policy expense report. */ function getPolicyExpenseReportOption(report: Report): ReportUtils.OptionData { - const expenseReport = policyExpenseReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]; + const expenseReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]; const option = createOption( expenseReport?.visibleChatMemberAccountIDs ?? [], From ef8d762cd4b433215ee14c8e6edf766070aba780 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 27 Mar 2024 13:32:38 +0500 Subject: [PATCH 08/11] refactor: use report object from passed function parameters and add a safety validation --- src/libs/OptionsListUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 9a95a48ed543..27483c965a55 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -766,12 +766,12 @@ function getReportOption(participant: Participant): ReportUtils.OptionData { * Get the option for a policy expense report. */ function getPolicyExpenseReportOption(report: Report): ReportUtils.OptionData { - const expenseReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]; + const expenseReport = ReportUtils.isPolicyExpenseChat(report) ? report : null; const option = createOption( expenseReport?.visibleChatMemberAccountIDs ?? [], allPersonalDetails ?? {}, - expenseReport ?? null, + expenseReport, {}, { showChatPreviewLine: false, From c4b6d857fb09f87e1c631ca8a7e379249ce72931 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 27 Mar 2024 15:14:29 +0500 Subject: [PATCH 09/11] refactor: remove not needed Onyx.connect for Report collection --- src/libs/OptionsListUtils.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index ac5c9a193146..6cb13eaef1c0 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -244,15 +244,6 @@ Onyx.connect({ }, }); -let allReports: OnyxCollection = {}; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (reports) => { - allReports = reports; - }, -}); - let allTransactions: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.TRANSACTION, From 11917c49b0e999ed7f5712c5b108682136aacd02 Mon Sep 17 00:00:00 2001 From: hurali97 Date: Wed, 27 Mar 2024 17:25:27 +0500 Subject: [PATCH 10/11] refactor: use correct types --- src/libs/OptionsListUtils.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 6cb13eaef1c0..49bff4f4492c 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -752,13 +752,13 @@ function getReportOption(participant: Participant): ReportUtils.OptionData { /** * Get the option for a policy expense report. */ -function getPolicyExpenseReportOption(report: Report): ReportUtils.OptionData { - const expenseReport = ReportUtils.isPolicyExpenseChat(report) ? report : null; +function getPolicyExpenseReportOption(participant: Participant | ReportUtils.OptionData): ReportUtils.OptionData { + const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? ReportUtils.getReport(participant.reportID) : null; const option = createOption( expenseReport?.visibleChatMemberAccountIDs ?? [], allPersonalDetails ?? {}, - expenseReport, + !isEmptyObject(expenseReport) ? expenseReport : null, {}, { showChatPreviewLine: false, @@ -769,8 +769,8 @@ function getPolicyExpenseReportOption(report: Report): ReportUtils.OptionData { // Update text & alternateText because createOption returns workspace name only if report is owned by the user option.text = ReportUtils.getPolicyName(expenseReport); option.alternateText = Localize.translateLocal('workspace.common.workspace'); - option.selected = report.selected; - option.isSelected = report.selected; + option.selected = expenseReport?.selected; + option.isSelected = expenseReport?.selected; return option; } From acc3455dac3aa6c630f962e5260b37b8ac6d1773 Mon Sep 17 00:00:00 2001 From: Muhammad Hur Ali Date: Thu, 28 Mar 2024 13:15:01 +0500 Subject: [PATCH 11/11] refactor: use participant instead of expenseReport Co-authored-by: Shubham Agrawal <58412969+shubham1206agra@users.noreply.github.com> --- src/libs/OptionsListUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 49bff4f4492c..83f17d9c1a96 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -769,8 +769,8 @@ function getPolicyExpenseReportOption(participant: Participant | ReportUtils.Opt // Update text & alternateText because createOption returns workspace name only if report is owned by the user option.text = ReportUtils.getPolicyName(expenseReport); option.alternateText = Localize.translateLocal('workspace.common.workspace'); - option.selected = expenseReport?.selected; - option.isSelected = expenseReport?.selected; + option.selected = participant.selected; + option.isSelected = participant.selected; return option; }