-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NewChatPage.tsx
executable file
·315 lines (282 loc) · 13 KB
/
NewChatPage.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import isEmpty from 'lodash/isEmpty';
import reject from 'lodash/reject';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import KeyboardAvoidingView from '@components/KeyboardAvoidingView';
import OfflineIndicator from '@components/OfflineIndicator';
import {useOptionsList} from '@components/OptionListContextProvider';
import {PressableWithFeedback} from '@components/Pressable';
import ReferralProgramCTA from '@components/ReferralProgramCTA';
import SelectCircle from '@components/SelectCircle';
import SelectionList from '@components/SelectionList';
import type {ListItem} from '@components/SelectionList/types';
import UserListItem from '@components/SelectionList/UserListItem';
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useScreenWrapperTranstionStatus from '@hooks/useScreenWrapperTransitionStatus';
import useStyledSafeAreaInsets from '@hooks/useStyledSafeAreaInsets';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import type {OptionData} from '@libs/ReportUtils';
import variables from '@styles/variables';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {SelectedParticipant} from '@src/types/onyx/NewGroupChatDraft';
type NewChatPageProps = {
isGroupChat?: boolean;
};
const excludedGroupEmails = CONST.EXPENSIFY_EMAILS.filter((value) => value !== CONST.EMAIL.CONCIERGE);
function useOptions({isGroupChat}: NewChatPageProps) {
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
const [selectedOptions, setSelectedOptions] = useState<Array<ListItem & OptionData>>([]);
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [newGroupDraft] = useOnyx(ONYXKEYS.NEW_GROUP_CHAT_DRAFT);
const personalData = useCurrentUserPersonalDetails();
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const {didScreenTransitionEnd} = useScreenWrapperTranstionStatus();
const {options: listOptions, areOptionsInitialized} = useOptionsList({
shouldInitialize: didScreenTransitionEnd,
});
const options = useMemo(() => {
const filteredOptions = OptionsListUtils.getFilteredOptions(
listOptions.reports ?? [],
listOptions.personalDetails ?? [],
betas ?? [],
debouncedSearchTerm,
selectedOptions,
isGroupChat ? excludedGroupEmails : [],
false,
true,
false,
{},
[],
false,
{},
[],
true,
);
const maxParticipantsReached = selectedOptions.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const headerMessage = OptionsListUtils.getHeaderMessage(
filteredOptions.personalDetails.length + filteredOptions.recentReports.length !== 0,
Boolean(filteredOptions.userToInvite),
debouncedSearchTerm.trim(),
maxParticipantsReached,
selectedOptions.some((participant) => participant?.searchText?.toLowerCase?.().includes(debouncedSearchTerm.trim().toLowerCase())),
);
return {...filteredOptions, headerMessage, maxParticipantsReached};
}, [betas, debouncedSearchTerm, isGroupChat, listOptions.personalDetails, listOptions.reports, selectedOptions]);
useEffect(() => {
if (!debouncedSearchTerm.length || options.maxParticipantsReached) {
return;
}
Report.searchInServer(debouncedSearchTerm);
}, [debouncedSearchTerm, options.maxParticipantsReached]);
useEffect(() => {
if (!newGroupDraft?.participants) {
return;
}
const selectedParticipants = newGroupDraft.participants.filter((participant) => participant.accountID !== personalData.accountID);
const newSelectedOptions = selectedParticipants.map((participant): OptionData => {
const baseOption = OptionsListUtils.getParticipantsOption({accountID: participant.accountID, login: participant.login, reportID: ''}, personalDetails);
return {...baseOption, reportID: baseOption.reportID ?? '', isSelected: true};
});
setSelectedOptions(newSelectedOptions);
}, [newGroupDraft, personalData, personalDetails]);
return {...options, searchTerm, debouncedSearchTerm, setSearchTerm, areOptionsInitialized: areOptionsInitialized && didScreenTransitionEnd, selectedOptions, setSelectedOptions};
}
function NewChatPage({isGroupChat}: NewChatPageProps) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const {isSmallScreenWidth} = useWindowDimensions();
const styles = useThemeStyles();
const personalData = useCurrentUserPersonalDetails();
const {insets} = useStyledSafeAreaInsets();
const [isSearchingForReports] = useOnyx(ONYXKEYS.IS_SEARCHING_FOR_REPORTS, {initWithStoredValues: false});
const {
headerMessage,
maxParticipantsReached,
searchTerm,
debouncedSearchTerm,
setSearchTerm,
selectedOptions,
setSelectedOptions,
recentReports,
personalDetails,
userToInvite,
areOptionsInitialized,
} = useOptions({
isGroupChat,
});
const [sections, firstKeyForList] = useMemo(() => {
const sectionsList: OptionsListUtils.CategorySection[] = [];
let firstKey = '';
const formatResults = OptionsListUtils.formatSectionsFromSearchTerm(debouncedSearchTerm, selectedOptions, recentReports, personalDetails, maxParticipantsReached);
sectionsList.push(formatResults.section);
if (!firstKey) {
firstKey = OptionsListUtils.getFirstKeyForList(formatResults.section.data);
}
if (maxParticipantsReached) {
return [sectionsList, firstKey];
}
sectionsList.push({
title: translate('common.recents'),
data: recentReports,
shouldShow: !isEmpty(recentReports),
});
if (!firstKey) {
firstKey = OptionsListUtils.getFirstKeyForList(recentReports);
}
sectionsList.push({
title: translate('common.contacts'),
data: personalDetails,
shouldShow: !isEmpty(personalDetails),
});
if (!firstKey) {
firstKey = OptionsListUtils.getFirstKeyForList(personalDetails);
}
if (userToInvite) {
sectionsList.push({
title: undefined,
data: [userToInvite],
shouldShow: true,
});
if (!firstKey) {
firstKey = OptionsListUtils.getFirstKeyForList([userToInvite]);
}
}
return [sectionsList, firstKey];
}, [debouncedSearchTerm, selectedOptions, recentReports, personalDetails, maxParticipantsReached, translate, userToInvite]);
/**
* Creates a new 1:1 chat with the option and the current user,
* or navigates to the existing chat if one with those participants already exists.
*/
const createChat = useCallback(
(option?: OptionsListUtils.Option) => {
let login = '';
if (option?.login) {
login = option.login;
} else if (selectedOptions.length === 1) {
login = selectedOptions[0].login ?? '';
}
if (!login) {
Log.warn('Tried to create chat with empty login');
return;
}
Report.navigateToAndOpenReport([login]);
},
[selectedOptions],
);
const itemRightSideComponent = useCallback(
(item: ListItem & OptionsListUtils.Option) => {
/**
* Removes a selected option from list if already selected. If not already selected add this option to the list.
* @param option
*/
function toggleOption(option: ListItem & Partial<OptionData>) {
const isOptionInList = !!option.isSelected;
let newSelectedOptions;
if (isOptionInList) {
newSelectedOptions = reject(selectedOptions, (selectedOption) => selectedOption.login === option.login);
} else {
newSelectedOptions = [...selectedOptions, {...option, isSelected: true, selected: true, reportID: option.reportID ?? ''}];
}
setSelectedOptions(newSelectedOptions);
}
if (item.isSelected) {
return (
<PressableWithFeedback
onPress={() => toggleOption(item)}
disabled={item.isDisabled}
role={CONST.ACCESSIBILITY_ROLE.CHECKBOX}
accessibilityLabel={CONST.ACCESSIBILITY_ROLE.CHECKBOX}
style={[styles.flexRow, styles.alignItemsCenter, styles.ml3]}
>
<SelectCircle isChecked={item.isSelected} />
</PressableWithFeedback>
);
}
return (
<Button
onPress={() => toggleOption(item)}
style={[styles.pl2]}
text={translate('newChatPage.addToGroup')}
small
/>
);
},
[selectedOptions, setSelectedOptions, styles, translate],
);
const createGroup = useCallback(() => {
if (!personalData || !personalData.login || !personalData.accountID) {
return;
}
const selectedParticipants: SelectedParticipant[] = selectedOptions.map((option: OptionData) => ({login: option.login ?? '', accountID: option.accountID ?? -1}));
const logins = [...selectedParticipants, {login: personalData.login, accountID: personalData.accountID}];
Report.setGroupDraft({participants: logins});
Navigation.navigate(ROUTES.NEW_CHAT_CONFIRM);
}, [selectedOptions, personalData]);
const footerContent = useMemo(
() => (
<>
<ReferralProgramCTA
referralContentType={CONST.REFERRAL_PROGRAM.CONTENT_TYPES.START_CHAT}
style={selectedOptions.length ? styles.mb5 : undefined}
/>
{!!selectedOptions.length && (
<Button
success
text={translate('common.next')}
onPress={createGroup}
pressOnEnter
/>
)}
</>
),
[createGroup, selectedOptions.length, styles.mb5, translate],
);
return (
<View
style={styles.flex1}
testID={NewChatPage.displayName}
>
<KeyboardAvoidingView
style={styles.flex1}
behavior="padding"
// Offset is needed as KeyboardAvoidingView in nested inside of TabNavigator instead of wrapping whole screen.
// This is because when wrapping whole screen the screen was freezing when changing Tabs.
keyboardVerticalOffset={variables.contentHeaderHeight + (insets?.top ?? 0) + variables.tabSelectorButtonHeight + variables.tabSelectorButtonPadding}
>
<SelectionList<OptionsListUtils.Option & ListItem>
ListItem={UserListItem}
sections={areOptionsInitialized ? sections : CONST.EMPTY_ARRAY}
textInputValue={searchTerm}
textInputHint={isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : ''}
onChangeText={setSearchTerm}
textInputLabel={translate('optionsSelector.nameEmailOrPhoneNumber')}
headerMessage={headerMessage}
onSelectRow={createChat}
onConfirm={(e, option) => (selectedOptions.length > 0 ? createGroup() : createChat(option))}
rightHandSideComponent={itemRightSideComponent}
footerContent={footerContent}
showLoadingPlaceholder={!areOptionsInitialized}
shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()}
isLoadingNewOptions={!!isSearchingForReports}
initiallyFocusedOptionKey={firstKeyForList}
/>
{isSmallScreenWidth && <OfflineIndicator />}
</KeyboardAvoidingView>
</View>
);
}
NewChatPage.displayName = 'NewChatPage';
export default NewChatPage;