-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathNewChatPage.js
executable file
·233 lines (204 loc) · 9.1 KB
/
NewChatPage.js
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
import _ from 'underscore';
import React, {useState, useEffect, useMemo} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import OptionsSelector from '../components/OptionsSelector';
import * as OptionsListUtils from '../libs/OptionsListUtils';
import * as ReportUtils from '../libs/ReportUtils';
import ONYXKEYS from '../ONYXKEYS';
import styles from '../styles/styles';
import * as Report from '../libs/actions/Report';
import CONST from '../CONST';
import withWindowDimensions, {windowDimensionsPropTypes} from '../components/withWindowDimensions';
import HeaderWithBackButton from '../components/HeaderWithBackButton';
import ScreenWrapper from '../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../components/withLocalize';
import * as Browser from '../libs/Browser';
import compose from '../libs/compose';
import personalDetailsPropType from './personalDetailsPropType';
import reportPropTypes from './reportPropTypes';
const propTypes = {
/** Whether screen is used to create group chat */
isGroupChat: PropTypes.bool,
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),
/** All of the personal details for everyone */
personalDetails: personalDetailsPropType,
/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes),
...windowDimensionsPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
isGroupChat: false,
betas: [],
personalDetails: {},
reports: {},
};
const excludedGroupEmails = _.without(CONST.EXPENSIFY_EMAILS, CONST.EMAIL.CONCIERGE);
function NewChatPage(props) {
const [searchTerm, setSearchTerm] = useState('');
const [filteredRecentReports, setFilteredRecentReports] = useState([]);
const [filteredPersonalDetails, setFilteredPersonalDetails] = useState([]);
const [filteredUserToInvite, setFilteredUserToInvite] = useState();
const [selectedOptions, setSelectedOptions] = useState([]);
const maxParticipantsReached = selectedOptions.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const headerMessage = OptionsListUtils.getHeaderMessage(
filteredPersonalDetails.length + filteredRecentReports.length !== 0,
Boolean(filteredUserToInvite),
searchTerm,
maxParticipantsReached,
);
const isOptionsDataReady = ReportUtils.isReportDataReady() && OptionsListUtils.isPersonalDetailsReady(props.personalDetails);
const sections = useMemo(() => {
const sectionsList = [];
let indexOffset = 0;
if (props.isGroupChat) {
sectionsList.push({
title: undefined,
data: selectedOptions,
shouldShow: !_.isEmpty(selectedOptions),
indexOffset,
});
indexOffset += selectedOptions.length;
if (maxParticipantsReached) {
return sectionsList;
}
}
// Filtering out selected users from the search results
const filterText = _.reduce(selectedOptions, (str, {login}) => `${str} ${login}`, '');
const recentReportsWithoutSelected = _.filter(filteredRecentReports, ({login}) => !filterText.includes(login));
const personalDetailsWithoutSelected = _.filter(filteredPersonalDetails, ({login}) => !filterText.includes(login));
const hasUnselectedUserToInvite = filteredUserToInvite && !filterText.includes(filteredUserToInvite.login);
sectionsList.push({
title: props.translate('common.recents'),
data: recentReportsWithoutSelected,
shouldShow: !_.isEmpty(recentReportsWithoutSelected),
indexOffset,
});
indexOffset += recentReportsWithoutSelected.length;
sectionsList.push({
title: props.translate('common.contacts'),
data: personalDetailsWithoutSelected,
shouldShow: !_.isEmpty(personalDetailsWithoutSelected),
indexOffset,
});
indexOffset += personalDetailsWithoutSelected.length;
if (hasUnselectedUserToInvite) {
sectionsList.push({
title: undefined,
data: [filteredUserToInvite],
shouldShow: true,
indexOffset,
});
}
return sectionsList;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [filteredPersonalDetails, filteredRecentReports, filteredUserToInvite, maxParticipantsReached, props.isGroupChat, selectedOptions]);
/**
* Removes a selected option from list if already selected. If not already selected add this option to the list.
* @param {Object} option
*/
function toggleOption(option) {
const isOptionInList = _.some(selectedOptions, (selectedOption) => selectedOption.login === option.login);
let newSelectedOptions;
if (isOptionInList) {
newSelectedOptions = _.reject(selectedOptions, (selectedOption) => selectedOption.login === option.login);
} else {
newSelectedOptions = [...selectedOptions, option];
}
const {recentReports, personalDetails, userToInvite} = OptionsListUtils.getNewChatOptions(props.reports, props.personalDetails, props.betas, searchTerm, [], excludedGroupEmails);
setSelectedOptions(newSelectedOptions);
setFilteredRecentReports(recentReports);
setFilteredPersonalDetails(personalDetails);
setFilteredUserToInvite(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.
*
* @param {Object} option
*/
function createChat(option) {
Report.navigateToAndOpenReport([option.login]);
}
/**
* Creates a new group chat with all the selected options and the current user,
* or navigates to the existing chat if one with those participants already exists.
*/
const createGroup = () => {
if (!props.isGroupChat) {
return;
}
const logins = _.pluck(selectedOptions, 'login');
if (logins.length < 1) {
return;
}
Report.navigateToAndOpenReport(logins);
};
useEffect(() => {
const {recentReports, personalDetails, userToInvite} = OptionsListUtils.getNewChatOptions(
props.reports,
props.personalDetails,
props.betas,
searchTerm,
[],
props.isGroupChat ? excludedGroupEmails : [],
);
setFilteredRecentReports(recentReports);
setFilteredPersonalDetails(personalDetails);
setFilteredUserToInvite(userToInvite);
// props.betas and props.isGroupChat are not added as dependencies since they don't change during the component lifecycle
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.reports, props.personalDetails, searchTerm]);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
>
{({didScreenTransitionEnd, safeAreaPaddingBottomStyle}) => (
<>
<HeaderWithBackButton title={props.isGroupChat ? props.translate('sidebarScreen.newGroup') : props.translate('sidebarScreen.newChat')} />
<View style={[styles.flex1, styles.w100, styles.pRelative, selectedOptions.length > 0 ? safeAreaPaddingBottomStyle : {}]}>
<OptionsSelector
canSelectMultipleOptions={props.isGroupChat}
sections={sections}
selectedOptions={selectedOptions}
value={searchTerm}
onSelectRow={(option) => (props.isGroupChat ? toggleOption(option) : createChat(option))}
onChangeText={setSearchTerm}
headerMessage={headerMessage}
boldStyle
shouldFocusOnSelectRow={props.isGroupChat && !Browser.isMobile()}
shouldShowConfirmButton={props.isGroupChat}
shouldShowOptions={didScreenTransitionEnd && isOptionsDataReady}
confirmButtonText={props.translate('newChatPage.createGroup')}
onConfirmSelection={createGroup}
textInputLabel={props.translate('optionsSelector.nameEmailOrPhoneNumber')}
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle}
/>
</View>
</>
)}
</ScreenWrapper>
);
}
NewChatPage.propTypes = propTypes;
NewChatPage.defaultProps = defaultProps;
NewChatPage.displayName = 'NewChatPage';
export default compose(
withLocalize,
withWindowDimensions,
withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(NewChatPage);