Skip to content

Commit

Permalink
Merge pull request #43164 from callstack-internal/gedu/43094_duplicat…
Browse files Browse the repository at this point in the history
…e_search_result

Search result duplication
  • Loading branch information
Beamanator authored Jun 18, 2024
2 parents f0f3b03 + bf61954 commit b278b8f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
35 changes: 5 additions & 30 deletions src/components/OptionListContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,24 @@ 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.
* 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) {
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.
*/
useEffect(() => {
if (!areOptionsInitialized.current || !reports) {
return;
}
const missingReportIds = Object.keys(reports).filter((key) => prevReports && !(key in prevReports));
// 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) => {
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
Expand Down
4 changes: 1 addition & 3 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,7 @@ function openReport(
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
errorFields: {
notFound: null,
},
errorFields: null,
},
},
{
Expand Down

0 comments on commit b278b8f

Please sign in to comment.