-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionItem.js
322 lines (298 loc) · 15.7 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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import React, {Component} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
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 IOUAction from '../../../components/ReportActionItem/IOUAction';
import ReportActionItemMessage from './ReportActionItemMessage';
import UnreadActionIndicator from '../../../components/UnreadActionIndicator';
import ReportActionItemMessageEdit from './ReportActionItemMessageEdit';
import ReportActionItemCreated from './ReportActionItemCreated';
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, 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';
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,
/** 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,
...windowDimensionsPropTypes,
};
const defaultProps = {
draftMessage: '',
hasOutstandingIOU: false,
};
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;
}
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
if (this.props.draftMessage) {
return;
}
this.setState({isContextMenuActive: true});
// Newline characters need to be removed here because getCurrentSelection() returns html mixed with newlines, and when
// <br> tags are converted later to markdown, it creates duplicate newline characters. This means that when the content
// is pasted, there are extra newlines in the content that we want to avoid.
const selection = SelectionScraper.getCurrentSelection().replace(/<br>\n/g, '<br>');
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),
);
}
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) {
children = (
<IOUAction
chatReportID={this.props.report.reportID}
action={this.props.action}
isMostRecentIOUReportAction={this.props.isMostRecentIOUReportAction}
isHovered={hovered}
contextMenuAnchor={this.popoverAnchor}
checkIfContextMenuActive={this.checkIfContextMenuActive}
/>
);
} 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,
reportID: this.props.report.reportID,
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}
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;
return (
<>
{children}
{hasReactions && (
<ReportActionItemReactions
reactions={reactions}
toggleReaction={this.toggleReaction}
/>
)}
</>
);
}
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} />;
}
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}
preventDefaultContentMenu={!this.props.draftMessage}
withoutFocusOnSecondaryInteraction
>
<Hoverable>
{hovered => (
<View accessibilityLabel={this.props.translate('accessibilityHints.chatMessage')}>
{this.props.shouldDisplayNewMarker && (
<UnreadActionIndicator reportActionID={this.props.action.reportActionID} />
)}
<View
style={StyleUtils.getReportActionItemStyle(
hovered
|| this.state.isContextMenuActive
|| this.props.draftMessage,
(this.props.network.isOffline && this.props.action.isLoading) || this.props.action.error,
)}
>
<OfflineWithFeedback
onClose={() => {
if (this.props.action.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
ReportActions.deleteOptimisticReportAction(this.props.report.reportID, this.props.action.reportActionID);
} else {
ReportActions.clearReportActionErrors(this.props.report.reportID, this.props.action.reportActionID);
}
}}
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}
>
{!this.props.displayAsGroup
? (
<ReportActionItemSingle action={this.props.action} showHeader={!this.props.draftMessage}>
{this.renderItemContent(hovered || this.state.isContextMenuActive)}
</ReportActionItemSingle>
)
: (
<ReportActionItemGrouped>
{this.renderItemContent(hovered || this.state.isContextMenuActive)}
</ReportActionItemGrouped>
)}
</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
}
draftMessage={this.props.draftMessage}
isChronosReport={ReportUtils.chatIncludesChronos(this.props.report)}
/>
</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(),
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, '');
},
}),
)(ReportActionItem);