From 6e490190d62852a507d5f8f98f034aaee102a18a Mon Sep 17 00:00:00 2001 From: Edu Date: Thu, 6 Jun 2024 11:04:21 +0200 Subject: [PATCH 1/4] Unify useEffect to create the optionList in one place, clearing all the errors from success --- src/components/OptionListContextProvider.tsx | 36 +++----------------- src/libs/actions/Report.ts | 4 +-- 2 files changed, 6 insertions(+), 34 deletions(-) diff --git a/src/components/OptionListContextProvider.tsx b/src/components/OptionListContextProvider.tsx index c9bf5d7b9db2..85e4c3ebf8ed 100644 --- a/src/components/OptionListContextProvider.tsx +++ b/src/components/OptionListContextProvider.tsx @@ -53,26 +53,6 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp const personalDetails = usePersonalDetails() || CONST.EMPTY_OBJECT; const prevPersonalDetails = usePrevious(personalDetails); - const prevReports = usePrevious(reports); - - /** - * This effect is used to update the options list when a report is updated. - */ - useEffect(() => { - // there is no need to update the options if the options are not initialized - if (!areOptionsInitialized.current) { - return; - } - - const newReports = OptionsListUtils.createOptionList(personalDetails, reports).reports; - - setOptions((prevOptions) => { - const newOptions = {...prevOptions}; - newOptions.reports = newReports; - return newOptions; - }); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [reports]); /** * This effect is used to add a new report option or remove a report option from the list of options when a new report is added to/removed from the collection. @@ -81,21 +61,14 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp if (!areOptionsInitialized.current || !reports) { return; } - const missingReportIds = Object.keys(reports).filter((key) => prevReports && !(key in prevReports)); + const newReports = OptionsListUtils.createOptionList(personalDetails, reports).reports; setOptions((prevOptions) => { const newOptions = { ...prevOptions, - reports: prevOptions.reports.filter((report) => reports[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] !== null), + reports: newReports, }; - missingReportIds.forEach((missingReportId) => { - const report = missingReportId ? reports[missingReportId] : null; - if (!missingReportId || !report) { - return; - } - const reportOption = OptionsListUtils.createOptionFromReport(report, personalDetails); - newOptions.reports.push(reportOption); - }); + return newOptions; }); // eslint-disable-next-line react-hooks/exhaustive-deps @@ -154,6 +127,7 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp const loadOptions = useCallback(() => { const optionLists = OptionsListUtils.createOptionList(personalDetails, reports); + console.log('[CHATFINDERPAGE-CONTEXT] Loading options: ', optionLists); setOptions({ reports: optionLists.reports, personalDetails: optionLists.personalDetails, @@ -187,7 +161,7 @@ const useOptionsList = (options?: {shouldInitialize: boolean}) => { if (!shouldInitialize || areOptionsInitialized) { return; } - + console.log('[CHATFINDERPAGE-CONTEXT]Initializing options'); initializeOptions(); }, [shouldInitialize, initializeOptions, areOptionsInitialized]); diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 1262e8af7d44..77630acce2a4 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -787,9 +787,7 @@ function openReport( onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - errorFields: { - notFound: null, - }, + errorFields: null, }, }, { From 09297b6d2593d061208f5675ead8782aef77c522 Mon Sep 17 00:00:00 2001 From: Edu Date: Wed, 12 Jun 2024 15:13:04 +0200 Subject: [PATCH 2/4] removed console logs --- src/components/OptionListContextProvider.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/OptionListContextProvider.tsx b/src/components/OptionListContextProvider.tsx index 85e4c3ebf8ed..229e4ca13541 100644 --- a/src/components/OptionListContextProvider.tsx +++ b/src/components/OptionListContextProvider.tsx @@ -127,7 +127,6 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp const loadOptions = useCallback(() => { const optionLists = OptionsListUtils.createOptionList(personalDetails, reports); - console.log('[CHATFINDERPAGE-CONTEXT] Loading options: ', optionLists); setOptions({ reports: optionLists.reports, personalDetails: optionLists.personalDetails, @@ -161,7 +160,7 @@ const useOptionsList = (options?: {shouldInitialize: boolean}) => { if (!shouldInitialize || areOptionsInitialized) { return; } - console.log('[CHATFINDERPAGE-CONTEXT]Initializing options'); + initializeOptions(); }, [shouldInitialize, initializeOptions, areOptionsInitialized]); From 89cd42cfa7c1637725fcdef79829ed589ab6b3be Mon Sep 17 00:00:00 2001 From: Edu Date: Thu, 13 Jun 2024 15:28:45 +0200 Subject: [PATCH 3/4] Updated comments --- src/components/OptionListContextProvider.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/components/OptionListContextProvider.tsx b/src/components/OptionListContextProvider.tsx index 229e4ca13541..639da87f6f0b 100644 --- a/src/components/OptionListContextProvider.tsx +++ b/src/components/OptionListContextProvider.tsx @@ -55,12 +55,14 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp const prevPersonalDetails = usePrevious(personalDetails); /** - * This effect is used to add a new report option or remove a report option from the list of options when a new report is added to/removed from the collection. + * This effect is used to update the options list when reports change. */ useEffect(() => { + // there is no need to update the options if the options are not initialized if (!areOptionsInitialized.current || !reports) { return; } + // Since reports updates can happen in bulk, and some reports depend on other reports, we need to recreate the whole list from scratch. const newReports = OptionsListUtils.createOptionList(personalDetails, reports).reports; setOptions((prevOptions) => { @@ -71,6 +73,7 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp return newOptions; }); + // This effect is used to update the options list when reports change so we ignore all dependencies except reports // eslint-disable-next-line react-hooks/exhaustive-deps }, [reports]); From 2dabaac545f0a3c5edd1937434d43696a9362a4c Mon Sep 17 00:00:00 2001 From: Edu Date: Thu, 13 Jun 2024 17:01:30 +0200 Subject: [PATCH 4/4] removed extra comment --- src/components/OptionListContextProvider.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/OptionListContextProvider.tsx b/src/components/OptionListContextProvider.tsx index 639da87f6f0b..7bd988750c85 100644 --- a/src/components/OptionListContextProvider.tsx +++ b/src/components/OptionListContextProvider.tsx @@ -73,7 +73,6 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp return newOptions; }); - // This effect is used to update the options list when reports change so we ignore all dependencies except reports // eslint-disable-next-line react-hooks/exhaustive-deps }, [reports]);