diff --git a/src/components/SelectionList/hooks/useFlattenedSections.ts b/src/components/SelectionList/hooks/useFlattenedSections.ts index 6ea493e2d92c..53fbc3ed231b 100644 --- a/src/components/SelectionList/hooks/useFlattenedSections.ts +++ b/src/components/SelectionList/hooks/useFlattenedSections.ts @@ -46,7 +46,9 @@ function useFlattenedSections(sections: Array 0) { disabledIndices.push(data.length); data.push({ type: CONST.SECTION_LIST_ITEM_TYPE.HEADER, @@ -55,7 +57,6 @@ function useFlattenedSections(sections: Array - account?.delegatedAccess?.delegates?.reduce( - (prev, {email}) => { - // eslint-disable-next-line no-param-reassign - prev[email] = true; - return prev; - }, - {} as Record, - ) ?? {}, - [account?.delegatedAccess?.delegates], - ); + const existingDelegates = + account?.delegatedAccess?.delegates?.reduce( + (prev, {email}) => { + // eslint-disable-next-line no-param-reassign + prev[email] = true; + return prev; + }, + {} as Record, + ) ?? {}; const {searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, areOptionsInitialized, toggleSelection} = useSearchSelector({ selectionMode: CONST.SEARCH_SELECTOR.SELECTION_MODE_SINGLE, @@ -48,51 +44,45 @@ function AddDelegatePage() { }, }); - const headerMessage = useMemo(() => { - return getHeaderMessage( - (availableOptions.recentReports?.length || 0) + (availableOptions.personalDetails?.length || 0) !== 0, - !!availableOptions.userToInvite, - debouncedSearchTerm, - countryCode, - ); - }, [availableOptions.recentReports?.length, availableOptions.personalDetails?.length, availableOptions.userToInvite, debouncedSearchTerm, countryCode]); - - const sections = useMemo(() => { - const sectionsList = []; - - sectionsList.push({ + const headerMessage = getHeaderMessage( + (availableOptions.recentReports?.length || 0) + (availableOptions.personalDetails?.length || 0) !== 0, + !!availableOptions.userToInvite, + debouncedSearchTerm, + countryCode, + ); + const sectionsList = [ + { title: translate('common.recents'), + sectionIndex: 0, data: availableOptions.recentReports, - shouldShow: availableOptions.recentReports?.length > 0, - }); - - sectionsList.push({ + }, + { title: translate('common.contacts'), + sectionIndex: 1, data: availableOptions.personalDetails, - shouldShow: availableOptions.personalDetails?.length > 0, - }); + }, + ]; - if (availableOptions.userToInvite) { - sectionsList.push({ - title: undefined, - data: [availableOptions.userToInvite], - shouldShow: true, - }); - } + if (availableOptions.userToInvite) { + sectionsList.push({ + sectionIndex: 2, + title: '', + data: [availableOptions.userToInvite], + }); + } - return sectionsList.map((section) => ({ - ...section, - data: section.data.map((option) => ({ - ...option, - text: option.text ?? '', - alternateText: option.alternateText ?? undefined, - keyForList: option.keyForList ?? '', - isDisabled: option.isDisabled ?? undefined, - login: option.login ?? undefined, - shouldShowSubscript: option.shouldShowSubscript ?? undefined, - })), - })); - }, [availableOptions.recentReports, availableOptions.personalDetails, availableOptions.userToInvite, translate]); + const sections = sectionsList.map((section) => ({ + ...section, + data: section.data.map((option, index) => ({ + ...option, + text: option.text ?? '', + alternateText: option.alternateText ?? undefined, + keyForList: `${option.keyForList}-${index}`, + isDisabled: option.isDisabled ?? undefined, + login: option.login ?? undefined, + shouldShowSubscript: option.shouldShowSubscript ?? undefined, + })), + })); useEffect(() => { searchInServer(debouncedSearchTerm); @@ -114,12 +104,16 @@ function AddDelegatePage() { ListItem={UserListItem} onSelectRow={toggleSelection} shouldSingleExecuteRowSelect - onChangeText={setSearchTerm} - textInputValue={searchTerm} - headerMessage={headerMessage} - textInputLabel={translate('selectionList.nameEmailOrPhoneNumber')} + textInputOptions={{ + value: searchTerm, + onChangeText: setSearchTerm, + headerMessage, + label: translate('selectionList.nameEmailOrPhoneNumber'), + }} showLoadingPlaceholder={!areOptionsInitialized} isLoadingNewOptions={!!isSearchingForReports} + disableMaintainingScrollPosition + shouldShowTextInput />