diff --git a/src/libs/Middleware/HandleUnusedOptimisticID.ts b/src/libs/Middleware/HandleUnusedOptimisticID.ts index 8fa00e7be47a..c6b4ac7d7b6f 100644 --- a/src/libs/Middleware/HandleUnusedOptimisticID.ts +++ b/src/libs/Middleware/HandleUnusedOptimisticID.ts @@ -16,18 +16,6 @@ import type {OnyxEntry} from 'react-native-onyx'; import clone from 'lodash/clone'; import Onyx from 'react-native-onyx'; -/** - * Use this only in non-React contexts (e.g. request middleware) where `useOnyx` is not available; - * React code should read the list via `useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST)` and pass it down. - */ -let allPersonalDetails: OnyxEntry; -Onyx.connectWithoutView({ - key: ONYXKEYS.PERSONAL_DETAILS_LIST, - callback: (value) => { - allPersonalDetails = value; - }, -}); - // Local cache of reportID to optimistic Onyx data const reportOptimisticData = new Map; redundantParticipants: Record} | undefined>(); @@ -49,7 +37,7 @@ const handleUnusedOptimisticID: Middleware = (requestResponse, request, isFromSe // We're opening a new report, which can be a new or preexisting report // For new report, clean up optimistic data after this request returned successfully // For report redirect a preexisting report, clean up optimistic data after the request of preexisting report returned successfully - reportOptimisticData.set(currentRequestReportID, prepareOnyxDataForCleanUpOptimisticParticipants(currentRequestReportID, allPersonalDetails)); + reportOptimisticData.set(currentRequestReportID, prepareOnyxDataForCleanUpOptimisticParticipants(currentRequestReportID)); } const responseOnyxData = response?.onyxData ?? []; diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index 9b72f3cf5500..c4a286274a13 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -2098,7 +2098,6 @@ function createGroupChat( function prepareOnyxDataForCleanUpOptimisticParticipants( reportID: string, - personalDetails: OnyxEntry, ): {settledPersonalDetails: OnyxEntry; redundantParticipants: Record} | undefined { const existingReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; if (!existingReport?.participants) { @@ -2107,7 +2106,7 @@ function prepareOnyxDataForCleanUpOptimisticParticipants( const settledPersonalDetails: OnyxEntry = {}; const redundantParticipants: Record = {}; for (const accountID in existingReport.participants) { - if (!personalDetails?.[accountID]?.isOptimisticPersonalDetail) { + if (!allPersonalDetails?.[accountID]?.isOptimisticPersonalDetail) { continue; } settledPersonalDetails[accountID] = null; @@ -2228,7 +2227,8 @@ function createTransactionThreadReport(params: CreateTransactionThreadReportPara reportID: optimisticTransactionThreadReportID, introSelected, participants, - personalDetails, + // TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656 + personalDetails: personalDetails ?? allPersonalDetails, newReportObject: optimisticTransactionThread, parentReportActionID: iouReportAction?.reportActionID, transaction, @@ -3961,7 +3961,18 @@ function navigateToConciergeChat( if (!checkIfCurrentPageActive()) { return; } - navigateToAndOpenReport([CONST.EMAIL.CONCIERGE], personalDetails, currentUserAccountID, introSelected, isSelfTourViewed, betas, shouldDismissModal, false, linkToOptions); + // TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656 + navigateToAndOpenReport( + [CONST.EMAIL.CONCIERGE], + personalDetails ?? allPersonalDetails, + currentUserAccountID, + introSelected, + isSelfTourViewed, + betas, + shouldDismissModal, + false, + linkToOptions, + ); }); } else if (shouldDismissModal) { const reportParams = {reportID: conciergeReportID, reportActionID}; diff --git a/tests/actions/ReportTest.ts b/tests/actions/ReportTest.ts index 2ed6d5702cb0..0236a0a39a0f 100644 --- a/tests/actions/ReportTest.ts +++ b/tests/actions/ReportTest.ts @@ -9009,65 +9009,4 @@ describe('actions/Report', () => { expect(result.delegateAccountID).toBeUndefined(); }); }); - - describe('prepareOnyxDataForCleanUpOptimisticParticipants', () => { - const REPORT_ID = '1'; - const OPTIMISTIC_ACCOUNT_ID = 100; - const SETTLED_ACCOUNT_ID = 200; - - it('marks only participants with optimistic personal details for clean up', async () => { - // Given a report with one optimistic and one settled participant - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, { - reportID: REPORT_ID, - participants: { - [OPTIMISTIC_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, - [SETTLED_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}, - }, - }); - await waitForBatchedUpdates(); - - // When preparing the clean up data using the provided personal details - const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants(REPORT_ID, { - [OPTIMISTIC_ACCOUNT_ID]: {accountID: OPTIMISTIC_ACCOUNT_ID, isOptimisticPersonalDetail: true}, - [SETTLED_ACCOUNT_ID]: {accountID: SETTLED_ACCOUNT_ID}, - }); - - // Then only the optimistic participant is marked for clean up - expect(result).toEqual({ - settledPersonalDetails: {[OPTIMISTIC_ACCOUNT_ID]: null}, - redundantParticipants: {[OPTIMISTIC_ACCOUNT_ID]: null}, - }); - }); - - it('returns empty clean up data when no participant has optimistic personal details', async () => { - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, { - reportID: REPORT_ID, - participants: {[SETTLED_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS}}, - }); - await waitForBatchedUpdates(); - - const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants(REPORT_ID, {[SETTLED_ACCOUNT_ID]: {accountID: SETTLED_ACCOUNT_ID}}); - - expect(result).toEqual({settledPersonalDetails: {}, redundantParticipants: {}}); - }); - - it('returns undefined when the report has no participants', async () => { - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, {reportID: REPORT_ID}); - await waitForBatchedUpdates(); - - const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants(REPORT_ID, { - [OPTIMISTIC_ACCOUNT_ID]: {accountID: OPTIMISTIC_ACCOUNT_ID, isOptimisticPersonalDetail: true}, - }); - - expect(result).toBeUndefined(); - }); - - it('returns undefined when the report does not exist', () => { - const result = Report.prepareOnyxDataForCleanUpOptimisticParticipants('non-existent-report', { - [OPTIMISTIC_ACCOUNT_ID]: {accountID: OPTIMISTIC_ACCOUNT_ID, isOptimisticPersonalDetail: true}, - }); - - expect(result).toBeUndefined(); - }); - }); });