11import React , { useEffect } from 'react' ;
22import { View } from 'react-native' ;
3+ import type { OnyxCollection } from 'react-native-onyx' ;
34import type { ValueOf } from 'type-fest' ;
45import Button from '@components/Button' ;
56import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
@@ -24,13 +25,50 @@ import CONST from '@src/CONST';
2425import ONYXKEYS from '@src/ONYXKEYS' ;
2526import ROUTES from '@src/ROUTES' ;
2627import type { Route } from '@src/ROUTES' ;
28+ import type { ReportActions , SearchResults , Transaction } from '@src/types/onyx' ;
2729import type { TransactionChanges } from '@src/types/onyx/Transaction' ;
2830import { getTransactionEditContext } from './SearchEditMultipleUtils' ;
2931
32+ /**
33+ * After a hard refresh, invoice transaction and report action data may only exist in the search snapshot,
34+ * not in the main Onyx collections. These helpers fill gaps from the snapshot so bulk edit can work.
35+ */
36+ function withSnapshotTransactions ( onyxTransactions : OnyxCollection < Transaction > | undefined , snapshotData : SearchResults [ 'data' ] | undefined ) : OnyxCollection < Transaction > | undefined {
37+ if ( ! snapshotData ) {
38+ return onyxTransactions ;
39+ }
40+ const merged = { ...onyxTransactions } ;
41+ for ( const key of Object . keys ( snapshotData ) ) {
42+ if ( ! key . startsWith ( ONYXKEYS . COLLECTION . TRANSACTION ) ) {
43+ continue ;
44+ }
45+ const typedKey = key as `${typeof ONYXKEYS . COLLECTION . TRANSACTION } ${string } `;
46+ if ( ! merged [ typedKey ] ) {
47+ merged [ typedKey ] = snapshotData [ typedKey ] ?? null ;
48+ }
49+ }
50+ return merged ;
51+ }
52+
53+ function withSnapshotReportActions ( onyxReportActions : OnyxCollection < ReportActions > | undefined , snapshotData : SearchResults [ 'data' ] | undefined ) : OnyxCollection < ReportActions > | undefined {
54+ if ( ! snapshotData ) {
55+ return onyxReportActions ;
56+ }
57+ const merged = { ...onyxReportActions } ;
58+ for ( const key of Object . keys ( snapshotData ) ) {
59+ if ( ! key . startsWith ( ONYXKEYS . COLLECTION . REPORT_ACTIONS ) ) {
60+ continue ;
61+ }
62+ const typedKey = key as `${typeof ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${string } `;
63+ merged [ typedKey ] = { ...( snapshotData [ typedKey ] ?? { } ) , ...( merged [ typedKey ] ?? { } ) } ;
64+ }
65+ return merged ;
66+ }
67+
3068function SearchEditMultiplePage ( ) {
3169 const { translate} = useLocalize ( ) ;
3270 const styles = useThemeStyles ( ) ;
33- const { currentSearchHash} = useSearchStateContext ( ) ;
71+ const { currentSearchHash, currentSearchResults } = useSearchStateContext ( ) ;
3472 const { clearSelectedTransactions} = useSearchActionsContext ( ) ;
3573 const [ policies ] = useOnyx ( ONYXKEYS . COLLECTION . POLICY ) ;
3674 const [ activePolicyID ] = useOnyx ( ONYXKEYS . NVP_ACTIVE_POLICY_ID ) ;
@@ -39,10 +77,14 @@ function SearchEditMultiplePage() {
3977 const [ allReports ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT ) ;
4078 const [ allReportActions ] = useOnyx ( ONYXKEYS . COLLECTION . REPORT_ACTIONS ) ;
4179
80+ const snapshotData = currentSearchResults ?. data ;
81+ const mergedTransactions = withSnapshotTransactions ( allTransactions , snapshotData ) ;
82+ const mergedReportActions = withSnapshotReportActions ( allReportActions , snapshotData ) ;
83+
4284 const selectedTransactionIDs = draftTransaction ?. selectedTransactionIDs ?? [ ] ;
4385
4486 const selectedTransactionContexts = selectedTransactionIDs . flatMap ( ( transactionID ) => {
45- const context = getTransactionEditContext ( transactionID , allTransactions , allReports , allReportActions , policies ) ;
87+ const context = getTransactionEditContext ( transactionID , mergedTransactions , allReports , mergedReportActions , policies ) ;
4688 return context ? [ context ] : [ ] ;
4789 } ) ;
4890
@@ -85,7 +127,7 @@ function SearchEditMultiplePage() {
85127 return ! isIOUReport ( report ) && ! isInvoiceReport ( report ) && transactionPolicy ?. disabledFields ?. reimbursable === false && ! isManagedCardTransaction ( transaction ) ;
86128 } ) ;
87129
88- const policyID = getSearchBulkEditPolicyID ( selectedTransactionIDs , activePolicyID , allTransactions , allReports ) ;
130+ const policyID = getSearchBulkEditPolicyID ( selectedTransactionIDs , activePolicyID , mergedTransactions , allReports ) ;
89131
90132 const policy = policyID ? policies ?. [ `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` ] : undefined ;
91133 const [ policyCategories ] = useOnyx ( `${ ONYXKEYS . COLLECTION . POLICY_CATEGORIES } ${ policyID } ` ) ;
@@ -152,8 +194,8 @@ function SearchEditMultiplePage() {
152194 changes,
153195 policy,
154196 reports : allReports ,
155- transactions : allTransactions ,
156- reportActions : allReportActions ,
197+ transactions : mergedTransactions ,
198+ reportActions : mergedReportActions ,
157199 policyCategories,
158200 hash : currentSearchHash ,
159201 allPolicies : policies ,
0 commit comments