-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionItem.js
428 lines (394 loc) · 21 KB
/
ReportActionItem.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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import React, {Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import CONST from '../../../CONST';
import ONYXKEYS from '../../../ONYXKEYS';
import reportActionPropTypes from './reportActionPropTypes';
import * as StyleUtils from '../../../styles/StyleUtils';
import PressableWithSecondaryInteraction from '../../../components/PressableWithSecondaryInteraction';
import Hoverable from '../../../components/Hoverable';
import ReportActionItemSingle from './ReportActionItemSingle';
import ReportActionItemGrouped from './ReportActionItemGrouped';
import MoneyRequestAction from '../../../components/ReportActionItem/MoneyRequestAction';
import ReportActionItemMessage from './ReportActionItemMessage';
import UnreadActionIndicator from '../../../components/UnreadActionIndicator';
import ReportActionItemMessageEdit from './ReportActionItemMessageEdit';
import ReportActionItemCreated from './ReportActionItemCreated';
import ReportActionItemThread from './ReportActionItemThread';
import compose from '../../../libs/compose';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import ControlSelection from '../../../libs/ControlSelection';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';
import MiniReportActionContextMenu from './ContextMenu/MiniReportActionContextMenu';
import * as ReportActionContextMenu from './ContextMenu/ReportActionContextMenu';
import * as ContextMenuActions from './ContextMenu/ContextMenuActions';
import {withBlockedFromConcierge, withNetwork, withPersonalDetails, withReportActionsDrafts} from '../../../components/OnyxProvider';
import RenameAction from '../../../components/ReportActionItem/RenameAction';
import InlineSystemMessage from '../../../components/InlineSystemMessage';
import styles from '../../../styles/styles';
import SelectionScraper from '../../../libs/SelectionScraper';
import * as User from '../../../libs/actions/User';
import * as ReportUtils from '../../../libs/ReportUtils';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
import * as ReportActions from '../../../libs/actions/ReportActions';
import reportPropTypes from '../../reportPropTypes';
import {ShowContextMenuContext} from '../../../components/ShowContextMenuContext';
import focusTextInputAfterAnimation from '../../../libs/focusTextInputAfterAnimation';
import ChronosOOOListActions from '../../../components/ReportActionItem/ChronosOOOListActions';
import ReportActionItemReactions from '../../../components/Reactions/ReportActionItemReactions';
import * as Report from '../../../libs/actions/Report';
import withLocalize from '../../../components/withLocalize';
import Icon from '../../../components/Icon';
import * as Expensicons from '../../../components/Icon/Expensicons';
import Text from '../../../components/Text';
import DisplayNames from '../../../components/DisplayNames';
import personalDetailsPropType from '../../personalDetailsPropType';
import ReportActionItemDraft from './ReportActionItemDraft';
import TaskPreview from '../../../components/ReportActionItem/TaskPreview';
import * as ReportActionUtils from '../../../libs/ReportActionsUtils';
import Permissions from '../../../libs/Permissions';
const propTypes = {
/** Report for this action */
report: reportPropTypes.isRequired,
/** All the data of the action item */
action: PropTypes.shape(reportActionPropTypes).isRequired,
/** Should the comment have the appearance of being grouped with the previous comment? */
displayAsGroup: PropTypes.bool.isRequired,
/** Is this the most recent IOU Action? */
isMostRecentIOUReportAction: PropTypes.bool.isRequired,
/** Whether there is an outstanding amount in IOU */
hasOutstandingIOU: PropTypes.bool,
/** Should we display the new marker on top of the comment? */
shouldDisplayNewMarker: PropTypes.bool.isRequired,
/** Determines if the avatar is displayed as a subscript (positioned lower than normal) */
shouldShowSubscriptAvatar: PropTypes.bool,
/** Position index of the report action in the overall report FlatList view */
index: PropTypes.number.isRequired,
/** Draft message - if this is set the comment is in 'edit' mode */
draftMessage: PropTypes.string,
/** Stores user's preferred skin tone */
preferredSkinTone: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/** All of the personalDetails */
personalDetails: PropTypes.objectOf(personalDetailsPropType),
/** List of betas available to current user */
betas: PropTypes.arrayOf(PropTypes.string),
...windowDimensionsPropTypes,
};
const defaultProps = {
draftMessage: '',
hasOutstandingIOU: false,
preferredSkinTone: CONST.EMOJI_DEFAULT_SKIN_TONE,
personalDetails: {},
shouldShowSubscriptAvatar: false,
betas: [],
};
class ReportActionItem extends Component {
constructor(props) {
super(props);
this.popoverAnchor = undefined;
this.state = {
isContextMenuActive: ReportActionContextMenu.isActiveReportAction(props.action.reportActionID),
};
this.checkIfContextMenuActive = this.checkIfContextMenuActive.bind(this);
this.showPopover = this.showPopover.bind(this);
this.renderItemContent = this.renderItemContent.bind(this);
this.toggleReaction = this.toggleReaction.bind(this);
}
shouldComponentUpdate(nextProps, nextState) {
return (
this.props.displayAsGroup !== nextProps.displayAsGroup ||
this.props.draftMessage !== nextProps.draftMessage ||
this.props.isMostRecentIOUReportAction !== nextProps.isMostRecentIOUReportAction ||
this.props.hasOutstandingIOU !== nextProps.hasOutstandingIOU ||
this.props.shouldDisplayNewMarker !== nextProps.shouldDisplayNewMarker ||
!_.isEqual(this.props.action, nextProps.action) ||
this.state.isContextMenuActive !== nextState.isContextMenuActive ||
lodashGet(this.props.report, 'statusNum') !== lodashGet(nextProps.report, 'statusNum') ||
lodashGet(this.props.report, 'stateNum') !== lodashGet(nextProps.report, 'stateNum') ||
this.props.translate !== nextProps.translate
);
}
componentDidUpdate(prevProps) {
if (prevProps.draftMessage || !this.props.draftMessage) {
return;
}
// Only focus the input when user edits a message, skip it for existing drafts being edited of the report.
// There is an animation when the comment is hidden and the edit form is shown, and there can be bugs on different mobile platforms
// if the input is given focus in the middle of that animation which can prevent the keyboard from opening.
focusTextInputAfterAnimation(this.textInput, 100);
}
checkIfContextMenuActive() {
this.setState({isContextMenuActive: ReportActionContextMenu.isActiveReportAction(this.props.action.reportActionID)});
}
/**
* Show the ReportActionContextMenu modal popover.
*
* @param {Object} [event] - A press event.
*/
showPopover(event) {
// Block menu on the message being Edited or if the report action item has errors
if (this.props.draftMessage || !_.isEmpty(this.props.action.errors)) {
return;
}
this.setState({isContextMenuActive: true});
const selection = SelectionScraper.getCurrentSelection();
ReportActionContextMenu.showContextMenu(
ContextMenuActions.CONTEXT_MENU_TYPES.REPORT_ACTION,
event,
selection,
this.popoverAnchor,
this.props.report.reportID,
this.props.action,
this.props.draftMessage,
undefined,
this.checkIfContextMenuActive,
ReportUtils.isArchivedRoom(this.props.report),
ReportUtils.chatIncludesChronos(this.props.report),
this.props.action.childReportID,
);
}
toggleReaction(emoji) {
Report.toggleEmojiReaction(this.props.report.reportID, this.props.action, emoji);
}
/**
* Get the content of ReportActionItem
* @param {Boolean} hovered whether the ReportActionItem is hovered
* @returns {Object} child component(s)
*/
renderItemContent(hovered = false) {
let children;
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU) {
// There is no single iouReport for bill splits, so only 1:1 requests require an iouReportID
const iouReportID = this.props.action.originalMessage.IOUReportID ? this.props.action.originalMessage.IOUReportID.toString() : '0';
children = (
<MoneyRequestAction
chatReportID={this.props.report.reportID}
requestReportID={iouReportID}
action={this.props.action}
isMostRecentIOUReportAction={this.props.isMostRecentIOUReportAction}
isHovered={hovered}
contextMenuAnchor={this.popoverAnchor}
checkIfContextMenuActive={this.checkIfContextMenuActive}
/>
);
} else if (ReportActionUtils.isCreatedTaskReportAction(this.props.action)) {
children = (
<TaskPreview
taskReportID={this.props.action.originalMessage.taskReportID.toString()}
action={this.props.action}
isHovered={hovered}
/>
);
} else {
const message = _.last(lodashGet(this.props.action, 'message', [{}]));
const isAttachment = _.has(this.props.action, 'isAttachment') ? this.props.action.isAttachment : ReportUtils.isReportMessageAttachment(message);
children = (
<ShowContextMenuContext.Provider
value={{
anchor: this.popoverAnchor,
report: this.props.report,
action: this.props.action,
checkIfContextMenuActive: this.checkIfContextMenuActive,
}}
>
{!this.props.draftMessage ? (
<ReportActionItemMessage
action={this.props.action}
style={[
!this.props.displayAsGroup && isAttachment ? styles.mt2 : undefined,
_.contains(_.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG), this.props.action.actionName) ? styles.colorMuted : undefined,
]}
/>
) : (
<ReportActionItemMessageEdit
action={this.props.action}
draftMessage={this.props.draftMessage}
reportID={this.props.report.reportID}
index={this.props.index}
ref={(el) => (this.textInput = el)}
report={this.props.report}
// Avoid defining within component due to an existing Onyx bug
preferredSkinTone={this.props.preferredSkinTone}
shouldDisableEmojiPicker={
(ReportUtils.chatIncludesConcierge(this.props.report) && User.isBlockedFromConcierge(this.props.blockedFromConcierge)) ||
ReportUtils.isArchivedRoom(this.props.report)
}
/>
)}
</ShowContextMenuContext.Provider>
);
}
const reactions = _.get(this.props, ['action', 'message', 0, 'reactions'], []);
const hasReactions = reactions.length > 0;
const shouldDisplayThreadReplies =
this.props.action.childCommenterCount && Permissions.canUseThreads(this.props.betas) && !ReportUtils.isThreadFirstChat(this.props.action, this.props.report.reportID);
const oldestFourEmails = lodashGet(this.props.action, 'childOldestFourEmails', '').split(',');
return (
<>
{children}
{hasReactions && (
<View style={this.props.draftMessage ? styles.chatItemReactionsDraftRight : {}}>
<ReportActionItemReactions
reactions={reactions}
toggleReaction={this.toggleReaction}
/>
</View>
)}
{shouldDisplayThreadReplies && (
<ReportActionItemThread
childReportID={`${this.props.action.childReportID}`}
numberOfReplies={this.props.action.childVisibleActionCount || 0}
mostRecentReply={`${this.props.action.childLastVisibleActionCreated}`}
icons={ReportUtils.getIconsForParticipants(oldestFourEmails, this.props.personalDetails)}
/>
)}
</>
);
}
/**
* Get ReportActionItem with a proper wrapper
* @param {Boolean} hovered whether the ReportActionItem is hovered
* @param {Boolean} isWhisper whether the ReportActionItem is a whisper
* @returns {Object} report action item
*/
renderReportActionItem(hovered, isWhisper) {
const content = this.renderItemContent(hovered || this.state.isContextMenuActive);
if (this.props.draftMessage) {
return <ReportActionItemDraft>{content}</ReportActionItemDraft>;
}
if (!this.props.displayAsGroup) {
return (
<ReportActionItemSingle
action={this.props.action}
showHeader={!this.props.draftMessage}
wrapperStyles={[styles.chatItem, isWhisper ? styles.pt1 : {}]}
shouldShowSubscriptAvatar={this.props.shouldShowSubscriptAvatar}
report={this.props.report}
>
{content}
</ReportActionItemSingle>
);
}
return <ReportActionItemGrouped wrapperStyles={[styles.chatItem, isWhisper ? styles.pt1 : {}]}>{content}</ReportActionItemGrouped>;
}
render() {
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) {
return <ReportActionItemCreated reportID={this.props.report.reportID} />;
}
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.RENAMED) {
return <RenameAction action={this.props.action} />;
}
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.CHRONOSOOOLIST) {
return (
<ChronosOOOListActions
action={this.props.action}
reportID={this.props.report.reportID}
/>
);
}
const hasErrors = !_.isEmpty(this.props.action.errors);
const whisperedTo = lodashGet(this.props.action, 'whisperedTo', []);
const isWhisper = whisperedTo.length > 0;
const isMultipleParticipant = whisperedTo.length > 1;
const isWhisperOnlyVisibleByUser = isWhisper && ReportUtils.isCurrentUserTheOnlyParticipant(whisperedTo);
const whisperedToPersonalDetails = isWhisper ? _.filter(this.props.personalDetails, (details) => _.includes(whisperedTo, details.login)) : [];
const displayNamesWithTooltips = isWhisper ? ReportUtils.getDisplayNamesWithTooltips(whisperedToPersonalDetails, isMultipleParticipant) : [];
return (
<PressableWithSecondaryInteraction
pointerEvents={this.props.action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE ? 'none' : 'auto'}
ref={(el) => (this.popoverAnchor = el)}
onPressIn={() => this.props.isSmallScreenWidth && DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onSecondaryInteraction={this.showPopover}
preventDefaultContextMenu={!this.props.draftMessage && !hasErrors}
withoutFocusOnSecondaryInteraction
>
<Hoverable>
{(hovered) => (
<View accessibilityLabel={this.props.translate('accessibilityHints.chatMessage')}>
{this.props.shouldDisplayNewMarker && <UnreadActionIndicator reportActionID={this.props.action.reportActionID} />}
<View
style={StyleUtils.getReportActionItemStyle(
hovered || isWhisper || this.state.isContextMenuActive || this.props.draftMessage,
(this.props.network.isOffline && this.props.action.isLoading) || this.props.action.error,
)}
>
<OfflineWithFeedback
onClose={() => ReportActions.clearReportActionErrors(this.props.report.reportID, this.props.action)}
pendingAction={this.props.draftMessage ? null : this.props.action.pendingAction}
errors={this.props.action.errors}
errorRowStyles={[styles.ml10, styles.mr2]}
needsOffscreenAlphaCompositing={this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU}
>
{isWhisper && (
<View style={[styles.flexRow, styles.pl5, styles.pt2]}>
<View style={[styles.pl6, styles.mr3]}>
<Icon
src={Expensicons.Eye}
small
/>
</View>
<Text style={[styles.chatItemMessageHeaderTimestamp]}>
{this.props.translate('reportActionContextMenu.onlyVisible')}
</Text>
<DisplayNames
fullTitle={ReportUtils.getWhisperDisplayNames(whisperedTo)}
displayNamesWithTooltips={displayNamesWithTooltips}
tooltipEnabled
numberOfLines={1}
textStyles={[styles.chatItemMessageHeaderTimestamp]}
shouldUseFullTitle={isWhisperOnlyVisibleByUser}
/>
</View>
)}
{this.renderReportActionItem(hovered, isWhisper)}
</OfflineWithFeedback>
</View>
<MiniReportActionContextMenu
reportID={this.props.report.reportID}
reportAction={this.props.action}
isArchivedRoom={ReportUtils.isArchivedRoom(this.props.report)}
displayAsGroup={this.props.displayAsGroup}
isVisible={hovered && !this.props.draftMessage && !hasErrors}
draftMessage={this.props.draftMessage}
isChronosReport={ReportUtils.chatIncludesChronos(this.props.report)}
childReportActionID={this.props.action.childReportActionID}
/>
</View>
)}
</Hoverable>
<View style={styles.reportActionSystemMessageContainer}>
<InlineSystemMessage message={this.props.action.error} />
</View>
</PressableWithSecondaryInteraction>
);
}
}
ReportActionItem.propTypes = propTypes;
ReportActionItem.defaultProps = defaultProps;
export default compose(
withWindowDimensions,
withLocalize,
withNetwork(),
withPersonalDetails(),
withBlockedFromConcierge({propName: 'blockedFromConcierge'}),
withReportActionsDrafts({
propName: 'draftMessage',
transformValue: (drafts, props) => {
const draftKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${props.report.reportID}_${props.action.reportActionID}`;
return lodashGet(drafts, draftKey, '');
},
}),
withOnyx({
preferredSkinTone: {
key: ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(ReportActionItem);