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
15 changes: 15 additions & 0 deletions src/hooks/useSearchSelector/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ type UseSearchSelectorConfig = {

/** Whether to keep selected options in availableOptions instead of filtering them out */
shouldKeepSelectedInAvailableOptions?: boolean;

/** Whether to separate selected options that are not in availableOptions.personalDetails (e.g. non-existing users invited by email) */
shouldSeparateNonExistingSelectedOptions?: boolean;
};

type ContactState = {
Expand Down Expand Up @@ -126,6 +129,9 @@ type UseSearchSelectorReturn = {
/** Currently selected options used for list display. This prop can be used in selection list to display selected options that are filtered by search term */
selectedOptionsForDisplay: OptionData[];

/** Selected options that are not present in availableOptions.personalDetails (e.g. non-existing users invited by email). Only populated when shouldSeparateNonExistingSelectedOptions is true */
selectedNonExistingOptions?: OptionData[];

/** Function to set selected options */
setSelectedOptions: (options: OptionData[]) => void;

Expand Down Expand Up @@ -174,6 +180,7 @@ function useSearchSelectorBase({
recentAttendees,
shouldAllowNameOnlyOptions = false,
shouldKeepSelectedInAvailableOptions = false,
shouldSeparateNonExistingSelectedOptions = false,
}: UseSearchSelectorConfig): UseSearchSelectorReturn {
const {options: defaultOptions, areOptionsInitialized} = useOptionsList({
shouldInitialize,
Expand Down Expand Up @@ -422,6 +429,13 @@ function useSearchSelectorBase({
);
});

const selectedNonExistingOptions = shouldSeparateNonExistingSelectedOptions
? (() => {
const personalDetailLogins = new Set(filteredPersonalDetails.map((option) => option.login).filter(Boolean));
return selectedOptionsForDisplay.filter((option) => !personalDetailLogins.has(option.login));
})()
: [];

return {
searchTerm,
debouncedSearchTerm,
Expand All @@ -435,6 +449,7 @@ function useSearchSelectorBase({
contactState: undefined,
onListEndReached,
selectedOptionsForDisplay,
selectedNonExistingOptions,
};
}

Expand Down
21 changes: 13 additions & 8 deletions src/pages/workspace/DynamicWorkspaceInvitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {DYNAMIC_ROUTES} from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {InvitedEmailsToAccountIDs} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import getEmptyArray from '@src/types/utils/getEmptyArray';
import AccessOrNotFoundWrapper from './AccessOrNotFoundWrapper';
import withPolicyAndFullscreenLoading from './withPolicyAndFullscreenLoading';
import type {WithPolicyAndFullscreenLoadingProps} from './withPolicyAndFullscreenLoading';
Expand Down Expand Up @@ -106,7 +107,7 @@ function DynamicWorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
setSearchTerm,
availableOptions,
selectedOptions,
selectedOptionsForDisplay,
selectedNonExistingOptions = getEmptyArray<OptionData>(),
toggleSelection,
areOptionsInitialized,
onListEndReached,
Expand All @@ -120,6 +121,8 @@ function DynamicWorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
includeRecentReports: false,
shouldInitialize: didScreenTransitionEnd,
initialSelected: initiallySelectedOptions,
shouldKeepSelectedInAvailableOptions: true,
shouldSeparateNonExistingSelectedOptions: true,
});

const sections: Array<Section<OptionData>> = useMemo(() => {
Expand All @@ -129,16 +132,16 @@ function DynamicWorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
return [];
}

// Selected options section
if (selectedOptionsForDisplay.length > 0) {
// Selected non-existing users section (top)
if (selectedNonExistingOptions.length > 0) {
sectionsArr.push({
title: undefined,
data: selectedOptionsForDisplay,
data: selectedNonExistingOptions,
sectionIndex: 0,
});
}

// Contacts section
// Contacts section (includes both selected and unselected items)
if (availableOptions.personalDetails.length > 0) {
sectionsArr.push({
title: translate('common.contacts'),
Expand All @@ -147,8 +150,8 @@ function DynamicWorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
});
}

// User to invite section
if (availableOptions.userToInvite) {
// User to invite section (hide if already selected and shown in the top section)
if (availableOptions.userToInvite && !availableOptions.userToInvite.isSelected) {
sectionsArr.push({
title: undefined,
data: [availableOptions.userToInvite],
Expand All @@ -157,7 +160,7 @@ function DynamicWorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
}

return sectionsArr;
}, [areOptionsInitialized, selectedOptionsForDisplay, availableOptions.personalDetails, availableOptions.userToInvite, translate]);
}, [areOptionsInitialized, selectedNonExistingOptions, availableOptions.personalDetails, availableOptions.userToInvite, translate]);

const handleToggleSelection = useCallback(
(option: OptionData) => {
Expand Down Expand Up @@ -265,6 +268,8 @@ function DynamicWorkspaceInvitePage({route, policy}: WorkspaceInvitePageProps) {
onSelectRow={handleToggleSelection}
shouldShowTextInput
textInputOptions={textInputOptions}
shouldUpdateFocusedIndex
shouldPreventAutoScrollOnSelect
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MelvinBot shouldUpdateFocusedIndex is missing

confirmButtonOptions={{
onConfirm: inviteUser,
isDisabled: !selectedOptions.length,
Expand Down
Loading
Loading