Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/hooks/useSearchSelector.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ type UseSearchSelectorReturn = {
/** Current search term */
searchTerm: string;

/** Debounced search term */
debouncedSearchTerm: string;

/** Function to update search term */
setSearchTerm: (value: string) => void;

Expand Down Expand Up @@ -201,7 +204,7 @@ function useSearchSelectorBase({
maxElements: maxResults,
maxRecentReportElements: maxRecentReportsToShow,
includeUserToInvite,
loginsToExclude: excludeLogins,
excludeLogins,
});
case CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_SHARE_DESTINATION:
return getValidOptions(optionsWithContacts, draftComments, {
Expand Down Expand Up @@ -328,6 +331,7 @@ function useSearchSelectorBase({

return {
searchTerm,
debouncedSearchTerm,
setSearchTerm,
searchOptions,
availableOptions,
Expand Down
126 changes: 36 additions & 90 deletions src/pages/settings/Security/AddDelegate/AddDelegatePage.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import React, {useEffect, useMemo} from 'react';
import {View} from 'react-native';
import DelegateNoAccessWrapper from '@components/DelegateNoAccessWrapper';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {useBetas} from '@components/OnyxListItemProvider';
import {useOptionsList} from '@components/OptionListContextProvider';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionListWithSections';
import UserListItem from '@components/SelectionListWithSections/UserListItem';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useSearchSelector from '@hooks/useSearchSelector';
import useThemeStyles from '@hooks/useThemeStyles';
import {searchInServer} from '@libs/actions/Report';
import memoize from '@libs/memoize';
import Navigation from '@libs/Navigation/Navigation';
import {filterAndOrderOptions, getHeaderMessage, getValidOptions} from '@libs/OptionsListUtils';
import {getHeaderMessage} from '@libs/OptionsListUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {Participant} from '@src/types/onyx/IOU';

const memoizedGetValidOptions = memoize(getValidOptions, {maxSize: 5, monitoringName: 'AddDelegatePage.getValidOptions'});

function useOptions() {
const betas = useBetas();
const [isLoading, setIsLoading] = useState(true);
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
const {options: optionsList, areOptionsInitialized} = useOptionsList();
function AddDelegatePage() {
const {translate} = useLocalize();
const styles = useThemeStyles();
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false, canBeMissing: true});
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
const [countryCode] = useOnyx(ONYXKEYS.COUNTRY_CODE, {canBeMissing: false});
const [draftComments] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {canBeMissing: true});

const existingDelegates = useMemo(
() =>
account?.delegatedAccess?.delegates?.reduce(
Expand All @@ -39,85 +31,45 @@ function useOptions() {
return prev;
},
{} as Record<string, boolean>,
),
) ?? {},
[account?.delegatedAccess?.delegates],
);

const defaultOptions = useMemo(() => {
const {recentReports, personalDetails, userToInvite, currentUserOption} = memoizedGetValidOptions(
{
reports: optionsList.reports,
personalDetails: optionsList.personalDetails,
},
draftComments,
{
betas,
excludeLogins: {...CONST.EXPENSIFY_EMAILS_OBJECT, ...existingDelegates},
},
countryCode,
);

const headerMessage = getHeaderMessage((recentReports?.length || 0) + (personalDetails?.length || 0) !== 0, !!userToInvite, '', false, countryCode);

if (isLoading) {
// eslint-disable-next-line react-compiler/react-compiler
setIsLoading(false);
}

return {
userToInvite,
recentReports,
personalDetails,
currentUserOption,
headerMessage,
};
}, [optionsList.reports, optionsList.personalDetails, draftComments, betas, existingDelegates, isLoading, countryCode]);

const options = useMemo(() => {
const filteredOptions = filterAndOrderOptions(defaultOptions, debouncedSearchValue.trim(), countryCode, {
excludeLogins: {...CONST.EXPENSIFY_EMAILS_OBJECT, ...existingDelegates},
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
});
const headerMessage = getHeaderMessage(
(filteredOptions.recentReports?.length || 0) + (filteredOptions.personalDetails?.length || 0) !== 0,
!!filteredOptions.userToInvite,
debouncedSearchValue,
);

return {
...filteredOptions,
headerMessage,
};
}, [debouncedSearchValue, defaultOptions, existingDelegates, countryCode]);

return {...options, searchValue, debouncedSearchValue, setSearchValue, areOptionsInitialized};
}
function AddDelegatePage() {
const {translate} = useLocalize();
const styles = useThemeStyles();
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false, canBeMissing: true});
const {userToInvite, recentReports, personalDetails, searchValue, debouncedSearchValue, setSearchValue, headerMessage, areOptionsInitialized} = useOptions();
const [selectedOption, setSelectedOption] = useState<string | undefined>(undefined);
const {searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, areOptionsInitialized, toggleSelection} = useSearchSelector({
selectionMode: CONST.SEARCH_SELECTOR.SELECTION_MODE_SINGLE,
searchContext: CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_GENERAL,
includeUserToInvite: true,
excludeLogins: {...CONST.EXPENSIFY_EMAILS_OBJECT, ...existingDelegates},
includeRecentReports: true,
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
onSingleSelect: (option) => {
Navigation.navigate(ROUTES.SETTINGS_DELEGATE_ROLE.getRoute(option.login ?? ''));
},
});

const headerMessage = useMemo(() => {
return getHeaderMessage((availableOptions.recentReports?.length || 0) + (availableOptions.personalDetails?.length || 0) !== 0, !!availableOptions.userToInvite, debouncedSearchTerm);
}, [availableOptions, debouncedSearchTerm]);

const sections = useMemo(() => {
const sectionsList = [];

sectionsList.push({
title: translate('common.recents'),
data: recentReports,
shouldShow: recentReports?.length > 0,
data: availableOptions.recentReports,
shouldShow: availableOptions.recentReports?.length > 0,
});

sectionsList.push({
title: translate('common.contacts'),
data: personalDetails,
shouldShow: personalDetails?.length > 0,
data: availableOptions.personalDetails,
shouldShow: availableOptions.personalDetails?.length > 0,
});

if (userToInvite) {
if (availableOptions.userToInvite) {
sectionsList.push({
title: undefined,
data: [userToInvite],
data: [availableOptions.userToInvite],
shouldShow: true,
});
}
Expand All @@ -132,19 +84,13 @@ function AddDelegatePage() {
isDisabled: option.isDisabled ?? undefined,
login: option.login ?? undefined,
shouldShowSubscript: option.shouldShowSubscript ?? undefined,
isSelected: option.login === selectedOption,
})),
}));
}, [personalDetails, recentReports, translate, userToInvite, selectedOption]);

const onSelectRow = useCallback((option: Participant) => {
setSelectedOption(option?.login);
Navigation.navigate(ROUTES.SETTINGS_DELEGATE_ROLE.getRoute(option?.login ?? ''));
}, []);
}, [availableOptions, translate]);

useEffect(() => {
searchInServer(debouncedSearchValue);
}, [debouncedSearchValue]);
searchInServer(debouncedSearchTerm);
}, [debouncedSearchTerm]);

return (
<ScreenWrapper
Expand All @@ -160,10 +106,10 @@ function AddDelegatePage() {
<SelectionList
sections={areOptionsInitialized ? sections : []}
ListItem={UserListItem}
onSelectRow={onSelectRow}
onSelectRow={toggleSelection}
shouldSingleExecuteRowSelect
onChangeText={setSearchValue}
textInputValue={searchValue}
onChangeText={setSearchTerm}
textInputValue={searchTerm}
headerMessage={headerMessage}
textInputLabel={translate('selectionList.nameEmailOrPhoneNumber')}
showLoadingPlaceholder={!areOptionsInitialized}
Expand Down
Loading
Loading