diff --git a/config/eslint/eslint.seatbelt.tsv b/config/eslint/eslint.seatbelt.tsv index e15ba9a0d303..9180201f93f7 100644 --- a/config/eslint/eslint.seatbelt.tsv +++ b/config/eslint/eslint.seatbelt.tsv @@ -1109,7 +1109,7 @@ "../../src/pages/Search/SearchPage.tsx" "react-hooks/set-state-in-effect" 1 "../../src/pages/Search/SearchSavePage.tsx" "@typescript-eslint/no-unsafe-type-assertion" 3 "../../src/pages/Search/SearchTransactionsChangeReport.tsx" "@typescript-eslint/no-deprecated/InteractionManager.runAfterInteractions" 1 -"../../src/pages/Search/SearchTransactionsChangeReport.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1 +"../../src/pages/Search/SearchTransactionsChangeReport.tsx" "@typescript-eslint/no-unsafe-type-assertion" 2 "../../src/pages/Share/ShareRootPage.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/pages/Share/ShareTab.tsx" "@typescript-eslint/no-unsafe-type-assertion" 1 "../../src/pages/ShareCodePage.tsx" "@typescript-eslint/no-unsafe-type-assertion" 2 diff --git a/src/pages/Search/SearchTransactionsChangeReport.tsx b/src/pages/Search/SearchTransactionsChangeReport.tsx index 08a28f0ede15..c00ab3bad851 100644 --- a/src/pages/Search/SearchTransactionsChangeReport.tsx +++ b/src/pages/Search/SearchTransactionsChangeReport.tsx @@ -1,9 +1,10 @@ -import React, {useMemo} from 'react'; +import React, {useEffect, useMemo} from 'react'; // eslint-disable-next-line no-restricted-imports import {InteractionManager} from 'react-native'; import type {OnyxCollection} from 'react-native-onyx'; +import Onyx from 'react-native-onyx'; import {usePersonalDetails, useSession} from '@components/OnyxListItemProvider'; -import {useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; +import {useSearchResultsContext, useSearchSelectionActions, useSearchSelectionContext} from '@components/Search/SearchContext'; import type {ListItem} from '@components/SelectionList/types'; import useConditionalCreateEmptyReportConfirmation from '@hooks/useConditionalCreateEmptyReportConfirmation'; import useHasPerDiemTransactions from '@hooks/useHasPerDiemTransactions'; @@ -21,7 +22,7 @@ import IOURequestEditReportCommon from '@pages/iou/request/step/IOURequestEditRe import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES, {DYNAMIC_ROUTES} from '@src/ROUTES'; -import type {PersonalDetails, Transaction} from '@src/types/onyx'; +import type {PersonalDetails, Report, Transaction} from '@src/types/onyx'; type TransactionGroupListItem = ListItem & { /** reportID of the report */ @@ -31,6 +32,7 @@ type TransactionGroupListItem = ListItem & { function SearchTransactionsChangeReport() { const {selectedTransactions} = useSearchSelectionContext(); const {clearSelectedTransactions} = useSearchSelectionActions(); + const {currentSearchResults} = useSearchResultsContext(); const selectedTransactionsKeys = useMemo(() => Object.keys(selectedTransactions), [selectedTransactions]); const transactions = useMemo( () => @@ -97,6 +99,46 @@ function SearchTransactionsChangeReport() { return report?.ownerAccountID; }, [selectedTransactions, selectedTransactionsKeys, allReports]); const targetOwnerPersonalDetails = useMemo(() => getPersonalDetailsForAccountID(targetOwnerAccountID, personalDetails) as PersonalDetails, [personalDetails, targetOwnerAccountID]); + + useEffect(() => { + const snapshotData = currentSearchResults?.data; + if (!snapshotData) { + return; + } + + const onyxUpdates: Array<{ + onyxMethod: typeof Onyx.METHOD.MERGE; + key: `${typeof ONYXKEYS.COLLECTION.REPORT}${string}`; + value: Report; + }> = []; + + for (const key of Object.keys(snapshotData)) { + if (!key.startsWith(ONYXKEYS.COLLECTION.REPORT) || key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS) || key.startsWith(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS)) { + continue; + } + + const typedKey = key as `${typeof ONYXKEYS.COLLECTION.REPORT}${string}`; + if (allReports?.[typedKey]) { + continue; + } + + const report = snapshotData[typedKey]; + if (report) { + onyxUpdates.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: typedKey, + value: report, + }); + } + } + + if (onyxUpdates.length > 0) { + Onyx.update(onyxUpdates); + } + // Hydration should only run once on mount using the initial snapshot data + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + const createReportForPolicy = (shouldDismissEmptyReportsConfirmation?: boolean) => { const optimisticReport = createNewReport( targetOwnerPersonalDetails,