11import { useRoute } from '@react-navigation/native' ;
2- import React , { useContext , useLayoutEffect , useRef , useState } from 'react' ;
2+ import React , { useContext , useEffect , useEffectEvent , useLayoutEffect , useRef , useState } from 'react' ;
33import { View } from 'react-native' ;
44// eslint-disable-next-line no-restricted-imports
55import type { NativeScrollEvent , NativeSyntheticEvent , ScrollView as RNScrollView } from 'react-native' ;
@@ -40,10 +40,10 @@ type SectionParams = {
4040 reportCounts : NonNullable < ReturnType < typeof todosReportCountsSelector > > ;
4141 areAllSectionsExpanded : boolean ;
4242 onItemPress : ( query : string ) => void ;
43- onExpanded : ( isExpanded : boolean ) => void ;
43+ onCollapsed : ( isCollapsed : boolean ) => void ;
4444} ;
4545
46- function Section ( { section, hash, activeItemIndex, sectionStartIndex, reportCounts, areAllSectionsExpanded, onItemPress, onExpanded } : SectionParams ) {
46+ function Section ( { section, hash, activeItemIndex, sectionStartIndex, reportCounts, areAllSectionsExpanded, onItemPress, onCollapsed } : SectionParams ) {
4747 const { translate} = useLocalize ( ) ;
4848 const expensifyIcons = useMemoizedLazyExpensifyIcons ( [
4949 'Basket' ,
@@ -64,14 +64,26 @@ function Section({section, hash, activeItemIndex, sectionStartIndex, reportCount
6464
6565 const [ isExpanded , setIsExpanded ] = useState ( true ) ;
6666
67+ const onUnmount = useEffectEvent ( ( ) => {
68+ if ( isExpanded ) {
69+ return ;
70+ }
71+ // When the section is removed/unmounted while collapsed,
72+ // notify the parent that the section is no longer collapsed.
73+ onCollapsed ( false ) ;
74+ } ) ;
75+
76+ useEffect ( ( ) => {
77+ return ( ) => onUnmount ( ) ;
78+ } , [ ] ) ;
79+
6780 return (
6881 < SearchTypeMenuAccordion
6982 isExpanded = { isExpanded }
7083 onSectionHeaderPress = { ( ) => {
7184 setIsExpanded ( ( prevIsExpanded ) => {
72- const newValue = ! prevIsExpanded ;
73- onExpanded ( newValue ) ;
74- return newValue ;
85+ onCollapsed ( prevIsExpanded ) ;
86+ return ! prevIsExpanded ;
7587 } ) ;
7688 } }
7789 title = { translate ( section . translationPath ) }
@@ -150,12 +162,11 @@ function SearchTypeMenuWide({queryJSON}: SearchTypeMenuProps) {
150162
151163 const areSuggestedSearchesLoading = ! isOffline && ! isSearchDataLoaded && ! isLoadingOnyxValue ( isSearchDataLoadedResult ) ;
152164
153- const numberOfSections = nonExpenseReportsSections . length + 1 ;
154- const [ expandedSectionCount , setExpandedSectionCount ] = useState ( numberOfSections ) ;
155- const areAllSectionsExpanded = expandedSectionCount === numberOfSections ;
165+ const [ collapsedSectionCount , setCollapsedSectionCount ] = useState ( 0 ) ;
166+ const areAllSectionsExpanded = collapsedSectionCount === 0 ;
156167
157- const updateExpandedCount = ( isExpanded : boolean ) => {
158- setExpandedSectionCount ( ( prevExpandedCount ) => prevExpandedCount + ( isExpanded ? 1 : - 1 ) ) ;
168+ const updateCollapsedCount = ( isCollapsed : boolean ) => {
169+ setCollapsedSectionCount ( ( prevCollapsedCount ) => prevCollapsedCount + ( isCollapsed ? 1 : - 1 ) ) ;
159170 } ;
160171
161172 return (
@@ -169,7 +180,7 @@ function SearchTypeMenuWide({queryJSON}: SearchTypeMenuProps) {
169180 < Section
170181 section = { expenseReportsSection }
171182 onItemPress = { handleTypeMenuItemPress }
172- onExpanded = { updateExpandedCount }
183+ onCollapsed = { updateCollapsedCount }
173184 hash = { hash }
174185 sectionStartIndex = { 0 }
175186 activeItemIndex = { activeItemIndex }
@@ -186,7 +197,7 @@ function SearchTypeMenuWide({queryJSON}: SearchTypeMenuProps) {
186197 key = { section . translationPath }
187198 section = { section }
188199 onItemPress = { handleTypeMenuItemPress }
189- onExpanded = { updateExpandedCount }
200+ onCollapsed = { updateCollapsedCount }
190201 hash = { hash }
191202 sectionStartIndex = { sectionStartIndices . at ( index + ( expenseReportsSection ? 1 : 0 ) ) ?? 0 }
192203 activeItemIndex = { activeItemIndex }
0 commit comments