-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionCompose.js
466 lines (416 loc) · 19.8 KB
/
ReportActionCompose.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {withOnyx} from 'react-native-onyx';
import {useNavigation} from '@react-navigation/native';
import {useAnimatedRef} from 'react-native-reanimated';
import styles from '../../../../styles/styles';
import ONYXKEYS from '../../../../ONYXKEYS';
import * as Report from '../../../../libs/actions/Report';
import ReportTypingIndicator from '../ReportTypingIndicator';
import AttachmentModal from '../../../../components/AttachmentModal';
import compose from '../../../../libs/compose';
import willBlurTextInputOnTapOutsideFunc from '../../../../libs/willBlurTextInputOnTapOutside';
import canFocusInputOnScreenFocus from '../../../../libs/canFocusInputOnScreenFocus';
import CONST from '../../../../CONST';
import * as ReportUtils from '../../../../libs/ReportUtils';
import participantPropTypes from '../../../../components/participantPropTypes';
import ParticipantLocalTime from '../ParticipantLocalTime';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../../../components/withCurrentUserPersonalDetails';
import {withNetwork} from '../../../../components/OnyxProvider';
import * as User from '../../../../libs/actions/User';
import EmojiPickerButton from '../../../../components/EmojiPicker/EmojiPickerButton';
import * as DeviceCapabilities from '../../../../libs/DeviceCapabilities';
import OfflineIndicator from '../../../../components/OfflineIndicator';
import ExceededCommentLength from '../../../../components/ExceededCommentLength';
import ReportDropUI from '../ReportDropUI';
import reportPropTypes from '../../../reportPropTypes';
import OfflineWithFeedback from '../../../../components/OfflineWithFeedback';
import * as Welcome from '../../../../libs/actions/Welcome';
import SendButton from './SendButton';
import AttachmentPickerWithMenuItems from './AttachmentPickerWithMenuItems';
import ComposerWithSuggestions from './ComposerWithSuggestions';
import debouncedSaveReportComment from '../../../../libs/ComposerUtils/debouncedSaveReportComment';
import reportActionPropTypes from '../reportActionPropTypes';
import useLocalize from '../../../../hooks/useLocalize';
import getModalState from '../../../../libs/getModalState';
import useWindowDimensions from '../../../../hooks/useWindowDimensions';
import * as EmojiPickerActions from '../../../../libs/actions/EmojiPickerAction';
const propTypes = {
/** A method to call when the form is submitted */
onSubmit: PropTypes.func.isRequired,
/** The ID of the report actions will be created for */
reportID: PropTypes.string.isRequired,
/** Array of report actions for this report */
reportActions: PropTypes.arrayOf(PropTypes.shape(reportActionPropTypes)),
/** Personal details of all the users */
personalDetails: PropTypes.objectOf(participantPropTypes),
/** The report currently being looked at */
report: reportPropTypes,
/** Is composer full size */
isComposerFullSize: PropTypes.bool,
/** Whether user interactions should be disabled */
disabled: PropTypes.bool,
// The NVP describing a user's block status
blockedFromConcierge: PropTypes.shape({
// The date that the user will be unblocked
expiresAt: PropTypes.string,
}),
/** Whether the composer input should be shown */
shouldShowComposeInput: PropTypes.bool,
/** The type of action that's pending */
pendingAction: PropTypes.oneOf(['add', 'update', 'delete']),
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
modal: {},
report: {},
blockedFromConcierge: {},
personalDetails: {},
preferredSkinTone: CONST.EMOJI_DEFAULT_SKIN_TONE,
isComposerFullSize: false,
pendingAction: null,
shouldShowComposeInput: true,
...withCurrentUserPersonalDetailsDefaultProps,
};
// We want consistent auto focus behavior on input between native and mWeb so we have some auto focus management code that will
// prevent auto focus on existing chat for mobile device
const shouldFocusInputOnScreenFocus = canFocusInputOnScreenFocus();
const willBlurTextInputOnTapOutside = willBlurTextInputOnTapOutsideFunc();
function ReportActionCompose({
blockedFromConcierge,
currentUserPersonalDetails,
disabled,
isComposerFullSize,
network,
onSubmit,
pendingAction,
personalDetails,
report,
reportID,
reportActions,
shouldShowComposeInput,
isCommentEmpty: isCommentEmptyProp,
}) {
const {translate} = useLocalize();
const navigation = useNavigation();
const {isMediumScreenWidth, isSmallScreenWidth} = useWindowDimensions();
const animatedRef = useAnimatedRef();
const actionButtonRef = useRef(null);
/**
* Updates the Highlight state of the composer
*/
const [isFocused, setIsFocused] = useState(() => {
const initialModalState = getModalState();
return shouldFocusInputOnScreenFocus && shouldShowComposeInput && !initialModalState.isVisible && !initialModalState.willAlertModalBecomeVisible;
});
const [isFullComposerAvailable, setIsFullComposerAvailable] = useState(isComposerFullSize);
/**
* Updates the should clear state of the composer
*/
const [textInputShouldClear, setTextInputShouldClear] = useState(false);
const [isCommentEmpty, setIsCommentEmpty] = useState(isCommentEmptyProp);
/**
* Updates the visibility state of the menu
*/
const [isMenuVisible, setMenuVisibility] = useState(false);
const [isAttachmentPreviewActive, setIsAttachmentPreviewActive] = useState(false);
/**
* Updates the composer when the comment length is exceeded
* Shows red borders and prevents the comment from being sent
*/
const [hasExceededMaxCommentLength, setExceededMaxCommentLength] = useState(false);
const suggestionsRef = useRef(null);
const composerRef = useRef(null);
const reportParticipantIDs = useMemo(
() => _.without(lodashGet(report, 'participantAccountIDs', []), currentUserPersonalDetails.accountID),
[currentUserPersonalDetails.accountID, report],
);
const shouldShowReportRecipientLocalTime = useMemo(
() => ReportUtils.canShowReportRecipientLocalTime(personalDetails, report, currentUserPersonalDetails.accountID) && !isComposerFullSize,
[personalDetails, report, currentUserPersonalDetails.accountID, isComposerFullSize],
);
const isBlockedFromConcierge = useMemo(() => ReportUtils.chatIncludesConcierge(report) && User.isBlockedFromConcierge(blockedFromConcierge), [report, blockedFromConcierge]);
// If we are on a small width device then don't show last 3 items from conciergePlaceholderOptions
const conciergePlaceholderRandomIndex = useMemo(
() => _.random(translate('reportActionCompose.conciergePlaceholderOptions').length - (isSmallScreenWidth ? 4 : 1)),
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
);
// Placeholder to display in the chat input.
const inputPlaceholder = useMemo(() => {
if (ReportUtils.chatIncludesConcierge(report)) {
if (User.isBlockedFromConcierge(blockedFromConcierge)) {
return translate('reportActionCompose.blockedFromConcierge');
}
return translate('reportActionCompose.conciergePlaceholderOptions')[conciergePlaceholderRandomIndex];
}
return translate('reportActionCompose.writeSomething');
}, [report, blockedFromConcierge, translate, conciergePlaceholderRandomIndex]);
const focus = () => {
if (composerRef === null || composerRef.current === null) {
return;
}
composerRef.current.focus(true);
};
const isKeyboardVisibleWhenShowingModalRef = useRef(false);
const restoreKeyboardState = useCallback(() => {
if (!isKeyboardVisibleWhenShowingModalRef.current) {
return;
}
focus();
isKeyboardVisibleWhenShowingModalRef.current = false;
}, []);
const containerRef = useRef(null);
const measureContainer = useCallback((callback) => {
if (!containerRef.current) {
return;
}
containerRef.current.measureInWindow(callback);
}, []);
const onAddActionPressed = useCallback(() => {
if (!willBlurTextInputOnTapOutside) {
isKeyboardVisibleWhenShowingModalRef.current = composerRef.current.isFocused();
}
composerRef.current.blur();
}, []);
const updateShouldShowSuggestionMenuToFalse = useCallback(() => {
if (!suggestionsRef.current) {
return;
}
suggestionsRef.current.updateShouldShowSuggestionMenuToFalse(false);
}, []);
/**
* @param {Object} file
*/
const addAttachment = useCallback(
(file) => {
// Since we're submitting the form here which should clear the composer
// We don't really care about saving the draft the user was typing
// We need to make sure an empty draft gets saved instead
debouncedSaveReportComment.cancel();
const newComment = composerRef.current.prepareCommentAndResetComposer();
Report.addAttachment(reportID, file, newComment);
setTextInputShouldClear(false);
},
[reportID],
);
/**
* Event handler to update the state after the attachment preview is closed.
*/
const onAttachmentPreviewClose = useCallback(() => {
updateShouldShowSuggestionMenuToFalse();
setIsAttachmentPreviewActive(false);
restoreKeyboardState();
}, [updateShouldShowSuggestionMenuToFalse, restoreKeyboardState]);
/**
* Add a new comment to this chat
*
* @param {SyntheticEvent} [e]
*/
const submitForm = useCallback(
(e) => {
if (e) {
e.preventDefault();
}
// Since we're submitting the form here which should clear the composer
// We don't really care about saving the draft the user was typing
// We need to make sure an empty draft gets saved instead
debouncedSaveReportComment.cancel();
const newComment = composerRef.current.prepareCommentAndResetComposer();
if (!newComment) {
return;
}
onSubmit(newComment);
},
[onSubmit],
);
const isNextModalWillOpenRef = useRef(false);
const onTriggerAttachmentPicker = useCallback(() => {
// Set a flag to block suggestion calculation until we're finished using the file picker,
// which will stop any flickering as the file picker opens on non-native devices.
if (willBlurTextInputOnTapOutside) {
suggestionsRef.current.setShouldBlockSuggestionCalc(true);
}
isNextModalWillOpenRef.current = true;
}, []);
const onBlur = useCallback((e) => {
setIsFocused(false);
suggestionsRef.current.resetSuggestions();
if (e.relatedTarget && e.relatedTarget === actionButtonRef.current) {
isKeyboardVisibleWhenShowingModalRef.current = true;
}
}, []);
const onFocus = useCallback(() => {
setIsFocused(true);
}, []);
/**
* Used to show Popover menu on Workspace chat at first sign-in
* @returns {Boolean}
*/
const showPopoverMenu = useCallback(() => {
setMenuVisibility(true);
return true;
}, []);
useEffect(() => {
// Shows Popover Menu on Workspace Chat at first sign-in
if (!disabled) {
Welcome.show({
routes: lodashGet(navigation.getState(), 'routes', []),
showPopoverMenu,
});
}
return () => {
if (!EmojiPickerActions.isActive(report.reportID)) {
return;
}
EmojiPickerActions.hideEmojiPicker();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const reportRecipientAcountIDs = ReportUtils.getReportRecipientAccountIDs(report, currentUserPersonalDetails.accountID);
const reportRecipient = personalDetails[reportRecipientAcountIDs[0]];
const shouldUseFocusedColor = !isBlockedFromConcierge && !disabled && isFocused;
const hasReportRecipient = _.isObject(reportRecipient) && !_.isEmpty(reportRecipient);
const isSendDisabled = isCommentEmpty || isBlockedFromConcierge || disabled || hasExceededMaxCommentLength;
return (
<View
ref={containerRef}
style={[shouldShowReportRecipientLocalTime && !lodashGet(network, 'isOffline') && styles.chatItemComposeWithFirstRow, isComposerFullSize && styles.chatItemFullComposeRow]}
>
<OfflineWithFeedback
pendingAction={pendingAction}
style={isComposerFullSize ? styles.chatItemFullComposeRow : {}}
contentContainerStyle={isComposerFullSize ? styles.flex1 : {}}
>
{shouldShowReportRecipientLocalTime && hasReportRecipient && <ParticipantLocalTime participant={reportRecipient} />}
<View
style={[
shouldUseFocusedColor ? styles.chatItemComposeBoxFocusedColor : styles.chatItemComposeBoxColor,
styles.flexRow,
styles.chatItemComposeBox,
isComposerFullSize && styles.chatItemFullComposeBox,
hasExceededMaxCommentLength && styles.borderColorDanger,
]}
>
<AttachmentModal
headerTitle={translate('reportActionCompose.sendAttachment')}
onConfirm={addAttachment}
onModalShow={() => setIsAttachmentPreviewActive(true)}
onModalHide={onAttachmentPreviewClose}
>
{({displayFileInModal}) => (
<>
<AttachmentPickerWithMenuItems
displayFileInModal={displayFileInModal}
reportID={reportID}
report={report}
reportParticipantIDs={reportParticipantIDs}
isFullComposerAvailable={isFullComposerAvailable}
isComposerFullSize={isComposerFullSize}
updateShouldShowSuggestionMenuToFalse={updateShouldShowSuggestionMenuToFalse}
isBlockedFromConcierge={isBlockedFromConcierge}
disabled={disabled}
setMenuVisibility={setMenuVisibility}
isMenuVisible={isMenuVisible}
onTriggerAttachmentPicker={onTriggerAttachmentPicker}
onCanceledAttachmentPicker={restoreKeyboardState}
onMenuClosed={restoreKeyboardState}
onAddActionPressed={onAddActionPressed}
actionButtonRef={actionButtonRef}
/>
<ComposerWithSuggestions
ref={composerRef}
animatedRef={animatedRef}
suggestionsRef={suggestionsRef}
isNextModalWillOpenRef={isNextModalWillOpenRef}
reportID={reportID}
report={report}
reportActions={reportActions}
isMenuVisible={isMenuVisible}
inputPlaceholder={inputPlaceholder}
isComposerFullSize={isComposerFullSize}
displayFileInModal={displayFileInModal}
textInputShouldClear={textInputShouldClear}
setTextInputShouldClear={setTextInputShouldClear}
isBlockedFromConcierge={isBlockedFromConcierge}
disabled={disabled}
isFullComposerAvailable={isFullComposerAvailable}
setIsFullComposerAvailable={setIsFullComposerAvailable}
setIsCommentEmpty={setIsCommentEmpty}
submitForm={submitForm}
shouldShowReportRecipientLocalTime={shouldShowReportRecipientLocalTime}
shouldShowComposeInput={shouldShowComposeInput}
onFocus={onFocus}
onBlur={onBlur}
measureParentContainer={measureContainer}
/>
<ReportDropUI
onDrop={(e) => {
if (isAttachmentPreviewActive) {
return;
}
const data = lodashGet(e, ['dataTransfer', 'items', 0]);
displayFileInModal(data);
}}
/>
</>
)}
</AttachmentModal>
{DeviceCapabilities.canUseTouchScreen() && isMediumScreenWidth ? null : (
<EmojiPickerButton
isDisabled={isBlockedFromConcierge || disabled}
onModalHide={focus}
onEmojiSelected={(...args) => composerRef.current.replaceSelectionWithText(...args)}
emojiPickerID={report.reportID}
/>
)}
<SendButton
isDisabled={isSendDisabled}
setIsCommentEmpty={setIsCommentEmpty}
submitForm={submitForm}
animatedRef={animatedRef}
/>
</View>
<View
style={[
styles.flexRow,
styles.justifyContentBetween,
styles.alignItemsCenter,
(!isSmallScreenWidth || (isSmallScreenWidth && !network.isOffline)) && styles.chatItemComposeSecondaryRow,
]}
>
{!isSmallScreenWidth && <OfflineIndicator containerStyles={[styles.chatItemComposeSecondaryRow]} />}
<ReportTypingIndicator reportID={reportID} />
<ExceededCommentLength
reportID={reportID}
onExceededMaxCommentLength={setExceededMaxCommentLength}
/>
</View>
</OfflineWithFeedback>
</View>
);
}
ReportActionCompose.propTypes = propTypes;
ReportActionCompose.defaultProps = defaultProps;
export default compose(
withNetwork(),
withCurrentUserPersonalDetails,
withOnyx({
isCommentEmpty: {
key: ({reportID}) => `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`,
selector: (comment) => _.isEmpty(comment),
},
blockedFromConcierge: {
key: ONYXKEYS.NVP_BLOCKED_FROM_CONCIERGE,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
shouldShowComposeInput: {
key: ONYXKEYS.SHOULD_SHOW_COMPOSE_INPUT,
},
}),
)(ReportActionCompose);