@@ -12,6 +12,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
1212import useThemeStyles from '@hooks/useThemeStyles' ;
1313import useWindowDimensions from '@hooks/useWindowDimensions' ;
1414import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus' ;
15+ import memoize from '@libs/memoize' ;
1516import type { Option , Section } from '@libs/OptionsListUtils' ;
1617import { filterAndOrderOptions , getValidOptions } from '@libs/OptionsListUtils' ;
1718import type { OptionData } from '@libs/ReportUtils' ;
@@ -22,6 +23,8 @@ function getSelectedOptionData(option: Option) {
2223 return { ...option , reportID : `${ option . reportID } ` , selected : true } ;
2324}
2425
26+ const memoizedGetValidOptions = memoize ( getValidOptions , { maxSize : 5 , monitoringName : 'UserSelectPopup.getValidOptions' } ) ;
27+
2528type UserSelectPopupProps = {
2629 /** The currently selected users */
2730 value : string [ ] ;
@@ -63,28 +66,29 @@ function UserSelectPopup({value, closeOverlay, onChange}: UserSelectPopupProps)
6366
6467 const cleanSearchTerm = searchTerm . trim ( ) . toLowerCase ( ) ;
6568
66- // Get a list of all options/personal details and filter them by the current search term
67- const listData = useMemo ( ( ) => {
68- const optionsList = getValidOptions (
69+ const optionsList = useMemo ( ( ) => {
70+ return memoizedGetValidOptions (
6971 {
7072 reports : options . reports ,
7173 personalDetails : options . personalDetails ,
7274 } ,
7375 {
74- selectedOptions,
7576 excludeLogins : CONST . EXPENSIFY_EMAILS_OBJECT ,
76- includeSelectedOptions : true ,
7777 includeCurrentUser : true ,
7878 } ,
7979 ) ;
80+ } , [ options . reports , options . personalDetails ] ) ;
8081
81- const { personalDetails : filteredOptionsList , recentReports} = filterAndOrderOptions ( optionsList , cleanSearchTerm , {
82+ const filteredOptions = useMemo ( ( ) => {
83+ return filterAndOrderOptions ( optionsList , cleanSearchTerm , {
8284 excludeLogins : CONST . EXPENSIFY_EMAILS_OBJECT ,
8385 maxRecentReportsToShow : CONST . IOU . MAX_RECENT_REPORTS_TO_SHOW ,
8486 canInviteUser : false ,
8587 } ) ;
88+ } , [ optionsList , cleanSearchTerm ] ) ;
8689
87- const personalDetailList = filteredOptionsList
90+ const listData = useMemo ( ( ) => {
91+ const personalDetailList = filteredOptions . personalDetails
8892 . map ( ( participant ) => ( {
8993 ...participant ,
9094 isSelected : selectedOptions . some ( ( selectedOption ) => selectedOption . accountID === participant . accountID ) ,
@@ -100,8 +104,16 @@ function UserSelectPopup({value, closeOverlay, onChange}: UserSelectPopupProps)
100104 return 0 ;
101105 } ) ;
102106
103- return [ ...( personalDetailList ?? [ ] ) , ...( recentReports ?? [ ] ) ] ;
104- } , [ cleanSearchTerm , options . personalDetails , options . reports , selectedOptions , accountID ] ) ;
107+ const recentReportsList = filteredOptions . recentReports . map ( ( report ) => {
108+ const isSelected = selectedOptions . some ( ( selectedOption ) => selectedOption . reportID === report . reportID ) ;
109+ return {
110+ ...report ,
111+ isSelected,
112+ } ;
113+ } ) ;
114+
115+ return [ ...personalDetailList , ...recentReportsList ] ;
116+ } , [ filteredOptions , selectedOptions , accountID ] ) ;
105117
106118 const { sections, headerMessage} = useMemo ( ( ) => {
107119 const newSections : Section [ ] = [
0 commit comments