-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46962 from software-mansion-labs/kicu/46035-from-…
…to-filters [Search] Add From & To advanced filters
- Loading branch information
Showing
16 changed files
with
410 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
src/components/Search/SearchFiltersParticipantsSelector.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
import React, {useCallback, useEffect, useMemo, useState} from 'react'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import Button from '@components/Button'; | ||
import {usePersonalDetails} from '@components/OnyxProvider'; | ||
import {useOptionsList} from '@components/OptionListContextProvider'; | ||
import SelectionList from '@components/SelectionList'; | ||
import InviteMemberListItem from '@components/SelectionList/InviteMemberListItem'; | ||
import useDebouncedState from '@hooks/useDebouncedState'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useScreenWrapperTranstionStatus from '@hooks/useScreenWrapperTransitionStatus'; | ||
import * as DeviceCapabilities from '@libs/DeviceCapabilities'; | ||
import * as OptionsListUtils from '@libs/OptionsListUtils'; | ||
import type {Option} from '@libs/OptionsListUtils'; | ||
import type {OptionData} from '@libs/ReportUtils'; | ||
import Navigation from '@navigation/Navigation'; | ||
import * as Report from '@userActions/Report'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
|
||
const defaultListOptions = { | ||
userToInvite: null, | ||
recentReports: [], | ||
personalDetails: [], | ||
currentUserOption: null, | ||
headerMessage: '', | ||
categoryOptions: [], | ||
tagOptions: [], | ||
taxRatesOptions: [], | ||
}; | ||
|
||
function getSelectedOptionData(option: Option): OptionData { | ||
return {...option, selected: true, reportID: option.reportID ?? '-1'}; | ||
} | ||
|
||
type SearchFiltersParticipantsSelectorProps = { | ||
initialAccountIDs: string[]; | ||
onFiltersUpdate: (accountIDs: string[]) => void; | ||
}; | ||
|
||
function SearchFiltersParticipantsSelector({initialAccountIDs, onFiltersUpdate}: SearchFiltersParticipantsSelectorProps) { | ||
const {translate} = useLocalize(); | ||
const personalDetails = usePersonalDetails(); | ||
const {didScreenTransitionEnd} = useScreenWrapperTranstionStatus(); | ||
const {options, areOptionsInitialized} = useOptionsList({ | ||
shouldInitialize: didScreenTransitionEnd, | ||
}); | ||
|
||
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false}); | ||
const [selectedOptions, setSelectedOptions] = useState<OptionData[]>([]); | ||
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState(''); | ||
const cleanSearchTerm = useMemo(() => searchTerm.trim().toLowerCase(), [searchTerm]); | ||
|
||
const defaultOptions = useMemo(() => { | ||
if (!areOptionsInitialized) { | ||
return defaultListOptions; | ||
} | ||
|
||
return OptionsListUtils.getFilteredOptions( | ||
options.reports, | ||
options.personalDetails, | ||
undefined, | ||
'', | ||
selectedOptions, | ||
CONST.EXPENSIFY_EMAILS, | ||
false, | ||
true, | ||
false, | ||
{}, | ||
[], | ||
false, | ||
{}, | ||
[], | ||
true, | ||
false, | ||
false, | ||
0, | ||
undefined, | ||
true, | ||
); | ||
}, [areOptionsInitialized, options.personalDetails, options.reports, selectedOptions]); | ||
|
||
const chatOptions = useMemo(() => { | ||
return OptionsListUtils.filterOptions(defaultOptions, cleanSearchTerm, { | ||
selectedOptions, | ||
excludeLogins: CONST.EXPENSIFY_EMAILS, | ||
maxRecentReportsToShow: CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW, | ||
}); | ||
}, [defaultOptions, cleanSearchTerm, selectedOptions]); | ||
|
||
const {sections, headerMessage} = useMemo(() => { | ||
const newSections: OptionsListUtils.CategorySection[] = []; | ||
if (!areOptionsInitialized) { | ||
return {sections: [], headerMessage: undefined}; | ||
} | ||
|
||
const formattedResults = OptionsListUtils.formatSectionsFromSearchTerm( | ||
cleanSearchTerm, | ||
selectedOptions, | ||
chatOptions.recentReports, | ||
chatOptions.personalDetails, | ||
personalDetails, | ||
true, | ||
); | ||
|
||
const isCurrentUserSelected = selectedOptions.find((option) => option.accountID === chatOptions.currentUserOption?.accountID); | ||
|
||
newSections.push(formattedResults.section); | ||
|
||
if (chatOptions.currentUserOption && !isCurrentUserSelected) { | ||
newSections.push({ | ||
title: '', | ||
data: [chatOptions.currentUserOption], | ||
shouldShow: true, | ||
}); | ||
} | ||
|
||
newSections.push({ | ||
title: '', | ||
data: chatOptions.recentReports, | ||
shouldShow: chatOptions.recentReports.length > 0, | ||
}); | ||
|
||
newSections.push({ | ||
title: '', | ||
data: chatOptions.personalDetails, | ||
shouldShow: chatOptions.personalDetails.length > 0, | ||
}); | ||
|
||
const noResultsFound = chatOptions.personalDetails.length === 0 && chatOptions.recentReports.length === 0 && !chatOptions.currentUserOption; | ||
const message = noResultsFound ? translate('common.noResultsFound') : undefined; | ||
|
||
return { | ||
sections: newSections, | ||
headerMessage: message, | ||
}; | ||
}, [areOptionsInitialized, cleanSearchTerm, selectedOptions, chatOptions.recentReports, chatOptions.personalDetails, chatOptions.currentUserOption, personalDetails, translate]); | ||
|
||
// This effect handles setting initial selectedOptions based on accountIDs saved in onyx form | ||
useEffect(() => { | ||
if (!initialAccountIDs || initialAccountIDs.length === 0 || !personalDetails) { | ||
return; | ||
} | ||
|
||
const preSelectedOptions = initialAccountIDs | ||
.map((accountID) => { | ||
const participant = personalDetails[accountID]; | ||
if (!participant) { | ||
return; | ||
} | ||
|
||
return getSelectedOptionData(participant); | ||
}) | ||
.filter((option): option is NonNullable<OptionData> => { | ||
return !!option; | ||
}); | ||
|
||
setSelectedOptions(preSelectedOptions); | ||
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- this should react only to changes in form data | ||
}, [initialAccountIDs, personalDetails]); | ||
|
||
useEffect(() => { | ||
Report.searchInServer(debouncedSearchTerm.trim()); | ||
}, [debouncedSearchTerm]); | ||
|
||
const handleParticipantSelection = useCallback( | ||
(option: Option) => { | ||
const foundOptionIndex = selectedOptions.findIndex((selectedOption: Option) => { | ||
if (selectedOption.accountID && selectedOption.accountID === option?.accountID) { | ||
return true; | ||
} | ||
|
||
if (selectedOption.reportID && selectedOption.reportID === option?.reportID) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
if (foundOptionIndex < 0) { | ||
setSelectedOptions([...selectedOptions, getSelectedOptionData(option)]); | ||
} else { | ||
const newSelectedOptions = [...selectedOptions.slice(0, foundOptionIndex), ...selectedOptions.slice(foundOptionIndex + 1)]; | ||
setSelectedOptions(newSelectedOptions); | ||
} | ||
}, | ||
[selectedOptions], | ||
); | ||
|
||
const footerContent = ( | ||
<Button | ||
success | ||
text={translate('common.save')} | ||
pressOnEnter | ||
onPress={() => { | ||
const selectedAccountIDs = selectedOptions.map((option) => (option.accountID ? option.accountID.toString() : undefined)).filter(Boolean) as string[]; | ||
onFiltersUpdate(selectedAccountIDs); | ||
|
||
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); | ||
}} | ||
large | ||
/> | ||
); | ||
|
||
const isLoadingNewOptions = !!isSearchingForReports; | ||
const showLoadingPlaceholder = !didScreenTransitionEnd || !areOptionsInitialized || !initialAccountIDs || !personalDetails; | ||
|
||
return ( | ||
<SelectionList | ||
canSelectMultiple | ||
sections={sections} | ||
ListItem={InviteMemberListItem} | ||
textInputLabel={translate('selectionList.nameEmailOrPhoneNumber')} | ||
headerMessage={headerMessage} | ||
textInputValue={searchTerm} | ||
footerContent={footerContent} | ||
showScrollIndicator | ||
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()} | ||
onChangeText={(value) => { | ||
setSearchTerm(value); | ||
}} | ||
onSelectRow={handleParticipantSelection} | ||
isLoadingNewOptions={isLoadingNewOptions} | ||
showLoadingPlaceholder={showLoadingPlaceholder} | ||
/> | ||
); | ||
} | ||
|
||
SearchFiltersParticipantsSelector.displayName = 'SearchFiltersParticipantsSelector'; | ||
|
||
export default SearchFiltersParticipantsSelector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.