|
1 | | -import React from 'react'; |
2 | | -import ShareTabParticipantsSelector from '@components/Share/ShareTabParticipantsSelector'; |
| 1 | +import React, {useEffect, useMemo} from 'react'; |
| 2 | +import {useOnyx} from 'react-native-onyx'; |
| 3 | +import {useOptionsList} from '@components/OptionListContextProvider'; |
| 4 | +import SelectionList from '@components/SelectionList'; |
| 5 | +import InviteMemberListItem from '@components/SelectionList/InviteMemberListItem'; |
| 6 | +import useDebouncedState from '@hooks/useDebouncedState'; |
| 7 | +import useFastSearchFromOptions from '@hooks/useFastSearchFromOptions'; |
| 8 | +import useLocalize from '@hooks/useLocalize'; |
| 9 | +import useNetwork from '@hooks/useNetwork'; |
| 10 | +import useScreenWrapperTranstionStatus from '@hooks/useScreenWrapperTransitionStatus'; |
| 11 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 12 | +import {getOptimisticChatReport, saveReportDraft, searchInServer} from '@libs/actions/Report'; |
| 13 | +import {saveUnknownUserDetails} from '@libs/actions/Share'; |
| 14 | +import Navigation from '@libs/Navigation/Navigation'; |
| 15 | +import {combineOrderingOfReportsAndPersonalDetails, getHeaderMessage, getSearchOptions} from '@libs/OptionsListUtils'; |
| 16 | +import type {OptionData} from '@libs/ReportUtils'; |
| 17 | +import StringUtils from '@libs/StringUtils'; |
| 18 | +import CONST from '@src/CONST'; |
| 19 | +import ONYXKEYS from '@src/ONYXKEYS'; |
3 | 20 | import ROUTES from '@src/ROUTES'; |
4 | 21 |
|
| 22 | +const defaultListOptions = { |
| 23 | + userToInvite: null, |
| 24 | + recentReports: [], |
| 25 | + personalDetails: [], |
| 26 | + currentUserOption: null, |
| 27 | + categoryOptions: [], |
| 28 | +}; |
| 29 | + |
5 | 30 | function ShareTab() { |
6 | | - return <ShareTabParticipantsSelector detailsPageRouteObject={ROUTES.SHARE_DETAILS} />; |
| 31 | + const styles = useThemeStyles(); |
| 32 | + const {translate} = useLocalize(); |
| 33 | + const {isOffline} = useNetwork(); |
| 34 | + const [textInputValue, debouncedTextInputValue, setTextInputValue] = useDebouncedState(''); |
| 35 | + const [betas] = useOnyx(ONYXKEYS.BETAS); |
| 36 | + |
| 37 | + const {options, areOptionsInitialized} = useOptionsList(); |
| 38 | + const {didScreenTransitionEnd} = useScreenWrapperTranstionStatus(); |
| 39 | + const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false}); |
| 40 | + |
| 41 | + const offlineMessage: string = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : ''; |
| 42 | + const showLoadingPlaceholder = useMemo(() => !areOptionsInitialized || !didScreenTransitionEnd, [areOptionsInitialized, didScreenTransitionEnd]); |
| 43 | + |
| 44 | + const searchOptions = useMemo(() => { |
| 45 | + if (!areOptionsInitialized) { |
| 46 | + return defaultListOptions; |
| 47 | + } |
| 48 | + return getSearchOptions(options, betas ?? [], false); |
| 49 | + }, [areOptionsInitialized, betas, options]); |
| 50 | + |
| 51 | + const filterOptions = useFastSearchFromOptions(searchOptions, {includeUserToInvite: true}); |
| 52 | + |
| 53 | + const recentReportsOptions = useMemo(() => { |
| 54 | + if (textInputValue.trim() === '') { |
| 55 | + return searchOptions.recentReports.slice(0, 20); |
| 56 | + } |
| 57 | + const filteredOptions = filterOptions(textInputValue); |
| 58 | + const orderedOptions = combineOrderingOfReportsAndPersonalDetails(filteredOptions, textInputValue, { |
| 59 | + sortByReportTypeInSearch: true, |
| 60 | + preferChatroomsOverThreads: true, |
| 61 | + }); |
| 62 | + |
| 63 | + const reportOptions: OptionData[] = [...orderedOptions.recentReports, ...orderedOptions.personalDetails]; |
| 64 | + if (filteredOptions.userToInvite) { |
| 65 | + reportOptions.push(filteredOptions.userToInvite); |
| 66 | + } |
| 67 | + return reportOptions.slice(0, 20); |
| 68 | + }, [filterOptions, searchOptions.recentReports, textInputValue]); |
| 69 | + |
| 70 | + useEffect(() => { |
| 71 | + searchInServer(debouncedTextInputValue.trim()); |
| 72 | + }, [debouncedTextInputValue]); |
| 73 | + |
| 74 | + const styledRecentReports = recentReportsOptions.map((item) => ({ |
| 75 | + ...item, |
| 76 | + pressableStyle: styles.br2, |
| 77 | + text: StringUtils.lineBreaksToSpaces(item.text), |
| 78 | + wrapperStyle: [styles.pr3, styles.pl3], |
| 79 | + })); |
| 80 | + |
| 81 | + const [sections, header] = useMemo(() => { |
| 82 | + const newSections = []; |
| 83 | + newSections.push({title: textInputValue.trim() === '' ? translate('search.recentChats') : undefined, data: styledRecentReports}); |
| 84 | + const headerMessage = getHeaderMessage(styledRecentReports.length !== 0, false, textInputValue.trim(), false); |
| 85 | + return [newSections, headerMessage]; |
| 86 | + }, [textInputValue, styledRecentReports, translate]); |
| 87 | + |
| 88 | + const onSelectRow = (item: OptionData) => { |
| 89 | + let reportID = item?.reportID ?? CONST.DEFAULT_NUMBER_ID; |
| 90 | + const accountID = item?.accountID; |
| 91 | + if (accountID && !reportID) { |
| 92 | + saveUnknownUserDetails(item); |
| 93 | + const optimisticReport = getOptimisticChatReport(accountID); |
| 94 | + reportID = optimisticReport.reportID; |
| 95 | + |
| 96 | + saveReportDraft(reportID, optimisticReport).then(() => { |
| 97 | + Navigation.navigate(ROUTES.SHARE_DETAILS.getRoute(reportID.toString())); |
| 98 | + }); |
| 99 | + } else { |
| 100 | + Navigation.navigate(ROUTES.SHARE_DETAILS.getRoute(reportID.toString())); |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + return ( |
| 105 | + <SelectionList |
| 106 | + sections={areOptionsInitialized ? sections : CONST.EMPTY_ARRAY} |
| 107 | + textInputValue={textInputValue} |
| 108 | + textInputLabel={translate('selectionList.nameEmailOrPhoneNumber')} |
| 109 | + textInputHint={offlineMessage} |
| 110 | + onChangeText={setTextInputValue} |
| 111 | + headerMessage={header} |
| 112 | + sectionListStyle={[styles.ph2, styles.pb2, styles.overscrollBehaviorContain]} |
| 113 | + ListItem={InviteMemberListItem} |
| 114 | + showLoadingPlaceholder={showLoadingPlaceholder} |
| 115 | + shouldSingleExecuteRowSelect |
| 116 | + onSelectRow={onSelectRow} |
| 117 | + isLoadingNewOptions={!!isSearchingForReports} |
| 118 | + /> |
| 119 | + ); |
7 | 120 | } |
8 | 121 |
|
9 | 122 | export default ShareTab; |
0 commit comments