Skip to content

Commit 905e58e

Browse files
authored
Merge pull request Expensify#89715 from software-mansion-labs/fix/add-copilot-selected-duplicate
[CP Staging] fix: Selected user appears twice in Add Copilot list
2 parents cc193d3 + 90cc1ca commit 905e58e

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/hooks/useSearchSelector.base.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ type UseSearchSelectorConfig = {
7979

8080
/** Whether to allow name-only options */
8181
shouldAllowNameOnlyOptions?: boolean;
82+
83+
/** Whether to keep selected options in availableOptions instead of filtering them out */
84+
shouldKeepSelectedInAvailableOptions?: boolean;
8285
};
8386

8487
type ContactState = {
@@ -170,6 +173,7 @@ function useSearchSelectorBase({
170173
includeSelfDM = false,
171174
recentAttendees,
172175
shouldAllowNameOnlyOptions = false,
176+
shouldKeepSelectedInAvailableOptions = false,
173177
}: UseSearchSelectorConfig): UseSearchSelectorReturn {
174178
const {options: defaultOptions, areOptionsInitialized} = useOptionsList({
175179
shouldInitialize,
@@ -352,17 +356,17 @@ function useSearchSelectorBase({
352356
: null,
353357
};
354358

355-
const unselectedRecentReports = searchOptions.recentReports.filter((option) => !option.isSelected);
359+
const filteredRecentReports = shouldKeepSelectedInAvailableOptions ? searchOptions.recentReports : searchOptions.recentReports.filter((option) => !option.isSelected);
356360

357361
// Filter out people who appear in recent reports from personal details (recents take priority)
358-
const recentReportLogins = new Set(unselectedRecentReports.map((option) => option.login).filter(Boolean));
359-
const unselectedPersonalDetails = searchOptions.personalDetails.filter((option) => !option.isSelected && !recentReportLogins.has(option.login));
362+
const recentReportLogins = new Set(filteredRecentReports.map((option) => option.login).filter(Boolean));
363+
const filteredPersonalDetails = searchOptions.personalDetails.filter((option) => (shouldKeepSelectedInAvailableOptions || !option.isSelected) && !recentReportLogins.has(option.login));
360364

361365
const availableOptions = {
362366
...searchOptions,
363-
personalDetails: unselectedPersonalDetails,
364-
recentReports: unselectedRecentReports,
365-
userToInvite: searchOptions.userToInvite?.isSelected ? null : searchOptions.userToInvite,
367+
personalDetails: filteredPersonalDetails,
368+
recentReports: filteredRecentReports,
369+
userToInvite: !shouldKeepSelectedInAvailableOptions && searchOptions.userToInvite?.isSelected ? null : searchOptions.userToInvite,
366370
};
367371

368372
/**

src/pages/settings/Security/AddDelegate/AddDelegatePage.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ function AddDelegatePage() {
3333
{} as Record<string, boolean>,
3434
) ?? {};
3535

36-
const {searchTerm, debouncedSearchTerm, setSearchTerm, searchOptions, areOptionsInitialized, setSelectedOptions, onListEndReached} = useSearchSelector({
36+
const {searchTerm, debouncedSearchTerm, setSearchTerm, availableOptions, areOptionsInitialized, setSelectedOptions, onListEndReached} = useSearchSelector({
3737
selectionMode: CONST.SEARCH_SELECTOR.SELECTION_MODE_SINGLE,
3838
searchContext: CONST.SEARCH_SELECTOR.SEARCH_CONTEXT_GENERAL,
3939
includeUserToInvite: true,
4040
excludeLogins: {...CONST.EXPENSIFY_EMAILS_OBJECT, ...existingDelegates},
4141
includeRecentReports: true,
4242
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
43+
shouldKeepSelectedInAvailableOptions: true,
4344
});
4445

4546
const handleSelectRow = (option: OptionData) => {
@@ -48,29 +49,29 @@ function AddDelegatePage() {
4849
};
4950

5051
const headerMessage = getHeaderMessage(
51-
(searchOptions.recentReports?.length || 0) + (searchOptions.personalDetails?.length || 0) !== 0,
52-
!!searchOptions.userToInvite,
52+
(availableOptions.recentReports?.length || 0) + (availableOptions.personalDetails?.length || 0) !== 0,
53+
!!availableOptions.userToInvite,
5354
debouncedSearchTerm,
5455
countryCode,
5556
);
5657
const sectionsList = [
5758
{
5859
title: translate('common.recents'),
5960
sectionIndex: 0,
60-
data: searchOptions.recentReports,
61+
data: availableOptions.recentReports,
6162
},
6263
{
6364
title: translate('common.contacts'),
6465
sectionIndex: 1,
65-
data: searchOptions.personalDetails,
66+
data: availableOptions.personalDetails,
6667
},
6768
];
6869

69-
if (searchOptions.userToInvite) {
70+
if (availableOptions.userToInvite) {
7071
sectionsList.push({
7172
sectionIndex: 2,
7273
title: '',
73-
data: [searchOptions.userToInvite],
74+
data: [availableOptions.userToInvite],
7475
});
7576
}
7677

0 commit comments

Comments
 (0)