1- import React , { useCallback , useContext , useMemo , useState } from 'react' ;
1+ import React , { useCallback , useContext , useMemo , useRef , useState } from 'react' ;
22import { isMoneyRequestReport } from '@libs/ReportUtils' ;
33import { isReportListItemType , isTransactionListItemType } from '@libs/SearchUIUtils' ;
44import CONST from '@src/CONST' ;
@@ -37,6 +37,7 @@ function SearchContextProvider({children}: ChildrenProps) {
3737 const [ shouldShowFiltersBarLoading , setShouldShowFiltersBarLoading ] = useState ( false ) ;
3838 const [ lastSearchType , setLastSearchType ] = useState < string | undefined > ( undefined ) ;
3939 const [ searchContextData , setSearchContextData ] = useState ( defaultSearchContextData ) ;
40+ const areTransactionsEmpty = useRef ( true ) ;
4041
4142 const setCurrentSearchHash = useCallback ( ( searchHash : number ) => {
4243 setSearchContextData ( ( prevState ) => ( {
@@ -45,42 +46,41 @@ function SearchContextProvider({children}: ChildrenProps) {
4546 } ) ) ;
4647 } , [ ] ) ;
4748
48- const setSelectedTransactions : SearchContext [ 'setSelectedTransactions' ] = useCallback (
49- ( selectedTransactions , data = [ ] ) => {
50- if ( selectedTransactions instanceof Array ) {
51- if ( ! selectedTransactions . length && ! searchContextData . selectedTransactionIDs . length ) {
52- return ;
53- }
54- return setSearchContextData ( ( prevState ) => ( {
55- ...prevState ,
56- selectedTransactionIDs : selectedTransactions ,
57- } ) ) ;
49+ const setSelectedTransactions : SearchContext [ 'setSelectedTransactions' ] = useCallback ( ( selectedTransactions , data = [ ] ) => {
50+ if ( selectedTransactions instanceof Array ) {
51+ if ( ! selectedTransactions . length && areTransactionsEmpty . current ) {
52+ areTransactionsEmpty . current = true ;
53+ return ;
5854 }
55+ areTransactionsEmpty . current = false ;
56+ return setSearchContextData ( ( prevState ) => ( {
57+ ...prevState ,
58+ selectedTransactionIDs : selectedTransactions ,
59+ } ) ) ;
60+ }
5961
60- // When selecting transactions, we also need to manage the reports to which these transactions belong. This is done to ensure proper exporting to CSV.
61- let selectedReports : SearchContext [ 'selectedReports' ] = [ ] ;
62+ // When selecting transactions, we also need to manage the reports to which these transactions belong. This is done to ensure proper exporting to CSV.
63+ let selectedReports : SearchContext [ 'selectedReports' ] = [ ] ;
6264
63- if ( data . length && data . every ( isReportListItemType ) ) {
64- selectedReports = data
65- . filter ( ( item ) => isMoneyRequestReport ( item ) && item . transactions . every ( ( { keyForList} ) => selectedTransactions [ keyForList ] ?. isSelected ) )
66- . map ( ( { reportID, action = CONST . SEARCH . ACTION_TYPES . VIEW , total = CONST . DEFAULT_NUMBER_ID , policyID} ) => ( { reportID, action, total, policyID} ) ) ;
67- }
65+ if ( data . length && data . every ( isReportListItemType ) ) {
66+ selectedReports = data
67+ . filter ( ( item ) => isMoneyRequestReport ( item ) && item . transactions . every ( ( { keyForList} ) => selectedTransactions [ keyForList ] ?. isSelected ) )
68+ . map ( ( { reportID, action = CONST . SEARCH . ACTION_TYPES . VIEW , total = CONST . DEFAULT_NUMBER_ID , policyID} ) => ( { reportID, action, total, policyID} ) ) ;
69+ }
6870
69- if ( data . length && data . every ( isTransactionListItemType ) ) {
70- selectedReports = data
71- . filter ( ( { keyForList} ) => ! ! keyForList && selectedTransactions [ keyForList ] ?. isSelected )
72- . map ( ( { reportID, action = CONST . SEARCH . ACTION_TYPES . VIEW , amount : total = CONST . DEFAULT_NUMBER_ID , policyID} ) => ( { reportID, action, total, policyID} ) ) ;
73- }
71+ if ( data . length && data . every ( isTransactionListItemType ) ) {
72+ selectedReports = data
73+ . filter ( ( { keyForList} ) => ! ! keyForList && selectedTransactions [ keyForList ] ?. isSelected )
74+ . map ( ( { reportID, action = CONST . SEARCH . ACTION_TYPES . VIEW , amount : total = CONST . DEFAULT_NUMBER_ID , policyID} ) => ( { reportID, action, total, policyID} ) ) ;
75+ }
7476
75- setSearchContextData ( ( prevState ) => ( {
76- ...prevState ,
77- selectedTransactions,
78- shouldTurnOffSelectionMode : false ,
79- selectedReports,
80- } ) ) ;
81- } ,
82- [ searchContextData . selectedTransactionIDs . length ] ,
83- ) ;
77+ setSearchContextData ( ( prevState ) => ( {
78+ ...prevState ,
79+ selectedTransactions,
80+ shouldTurnOffSelectionMode : false ,
81+ selectedReports,
82+ } ) ) ;
83+ } , [ ] ) ;
8484
8585 const clearSelectedTransactions : SearchContext [ 'clearSelectedTransactions' ] = useCallback (
8686 ( searchHashOrClearIDsFlag , shouldTurnOffSelectionMode = false ) => {
0 commit comments