-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.tsx
200 lines (172 loc) · 7.58 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import type {StackScreenProps} from '@react-navigation/stack';
import isEmpty from 'lodash/isEmpty';
import React, {useEffect, useMemo, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {useOptionsList} from '@components/OptionListContextProvider';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import UserListItem from '@components/SelectionList/UserListItem';
import useDebouncedState from '@hooks/useDebouncedState';
import useDismissedReferralBanners from '@hooks/useDismissedReferralBanners';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import type {MaybePhraseKey} from '@libs/Localize';
import Navigation from '@libs/Navigation/Navigation';
import type {RootStackParamList} from '@libs/Navigation/types';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import Performance from '@libs/Performance';
import type {OptionData} from '@libs/ReportUtils';
import * as Report from '@userActions/Report';
import Timing from '@userActions/Timing';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import ChatFinderPageFooter from './ChatFinderPageFooter';
type ChatFinderPageOnyxProps = {
/** Beta features list */
betas: OnyxEntry<OnyxTypes.Beta[]>;
/** Whether or not we are searching for reports on the server */
isSearchingForReports: OnyxEntry<boolean>;
};
type ChatFinderPageProps = ChatFinderPageOnyxProps & StackScreenProps<RootStackParamList, typeof SCREENS.LEFT_MODAL.CHAT_FINDER>;
type ChatFinderPageSectionItem = {
data: OptionData[];
shouldShow: boolean;
};
type ChatFinderPageSectionList = ChatFinderPageSectionItem[];
const setPerformanceTimersEnd = () => {
Timing.end(CONST.TIMING.CHAT_FINDER_RENDER);
Performance.markEnd(CONST.TIMING.CHAT_FINDER_RENDER);
};
const ChatFinderPageFooterInstance = <ChatFinderPageFooter />;
function ChatFinderPage({betas, isSearchingForReports, navigation}: ChatFinderPageProps) {
const [isScreenTransitionEnd, setIsScreenTransitionEnd] = useState(false);
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const {options, areOptionsInitialized} = useOptionsList({
shouldInitialize: isScreenTransitionEnd,
});
const offlineMessage: MaybePhraseKey = isOffline ? [`${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}`, {isTranslated: true}] : '';
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');
useEffect(() => {
Timing.start(CONST.TIMING.CHAT_FINDER_RENDER);
Performance.markStart(CONST.TIMING.CHAT_FINDER_RENDER);
}, []);
useEffect(() => {
Report.searchInServer(debouncedSearchValue.trim());
}, [debouncedSearchValue]);
const searchOptions = useMemo(() => {
if (!areOptionsInitialized || !isScreenTransitionEnd) {
return {
recentReports: [],
personalDetails: [],
userToInvite: null,
currentUserOption: null,
categoryOptions: [],
tagOptions: [],
taxRatesOptions: [],
headerMessage: '',
};
}
const optionList = OptionsListUtils.getSearchOptions(options, '', betas ?? []);
const header = OptionsListUtils.getHeaderMessage(optionList.recentReports.length + optionList.personalDetails.length !== 0, Boolean(optionList.userToInvite), '');
return {...optionList, headerMessage: header};
}, [areOptionsInitialized, betas, isScreenTransitionEnd, options]);
const filteredOptions = useMemo(() => {
if (debouncedSearchValue.trim() === '') {
return {
recentReports: [],
personalDetails: [],
userToInvite: null,
headerMessage: '',
};
}
const newOptions = OptionsListUtils.filterOptions(searchOptions, debouncedSearchValue, betas);
const header = OptionsListUtils.getHeaderMessage(newOptions.recentReports.length + Number(!!newOptions.userToInvite) > 0, false, debouncedSearchValue);
return {
recentReports: newOptions.recentReports,
personalDetails: newOptions.personalDetails,
userToInvite: newOptions.userToInvite,
headerMessage: header,
};
}, [debouncedSearchValue, searchOptions, betas]);
const {recentReports, personalDetails: localPersonalDetails, userToInvite, headerMessage} = debouncedSearchValue.trim() !== '' ? filteredOptions : searchOptions;
const sections = useMemo((): ChatFinderPageSectionList => {
const newSections: ChatFinderPageSectionList = [];
if (recentReports?.length > 0) {
newSections.push({
data: recentReports.map((report) => ({...report, isBold: report.isUnread})),
shouldShow: true,
});
}
if (localPersonalDetails.length > 0) {
newSections.push({
data: localPersonalDetails,
shouldShow: true,
});
}
if (!isEmpty(userToInvite)) {
newSections.push({
data: [userToInvite],
shouldShow: true,
});
}
return newSections;
}, [localPersonalDetails, recentReports, userToInvite]);
const selectReport = (option: OptionData) => {
if (!option) {
return;
}
if (option.reportID) {
setSearchValue('');
Navigation.dismissModal(option.reportID);
} else {
Report.navigateToAndOpenReport(option.login ? [option.login] : []);
}
};
const handleScreenTransitionEnd = () => {
setIsScreenTransitionEnd(true);
};
const {isDismissed} = useDismissedReferralBanners({referralContentType: CONST.REFERRAL_PROGRAM.CONTENT_TYPES.REFER_FRIEND});
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={ChatFinderPage.displayName}
onEntryTransitionEnd={handleScreenTransitionEnd}
shouldEnableMaxHeight
navigation={navigation}
>
<HeaderWithBackButton
title={translate('common.find')}
onBackButtonPress={Navigation.goBack}
/>
<SelectionList<OptionData>
sections={areOptionsInitialized ? sections : CONST.EMPTY_ARRAY}
ListItem={UserListItem}
textInputValue={searchValue}
textInputLabel={translate('selectionList.nameEmailOrPhoneNumber')}
textInputHint={offlineMessage}
onChangeText={setSearchValue}
headerMessage={headerMessage}
onLayout={setPerformanceTimersEnd}
onSelectRow={selectReport}
showLoadingPlaceholder={!areOptionsInitialized || !isScreenTransitionEnd}
footerContent={!isDismissed && ChatFinderPageFooterInstance}
isLoadingNewOptions={!!isSearchingForReports}
/>
</ScreenWrapper>
);
}
ChatFinderPage.displayName = 'ChatFinderPage';
export default withOnyx<ChatFinderPageProps, ChatFinderPageOnyxProps>({
betas: {
key: ONYXKEYS.BETAS,
},
isSearchingForReports: {
key: ONYXKEYS.IS_SEARCHING_FOR_REPORTS,
initWithStoredValues: false,
},
})(ChatFinderPage);