-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathReportActionItem.js
214 lines (194 loc) · 8.97 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
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 canUseTouchScreen from '../../../libs/canUseTouchscreen';
import MiniReportActionContextMenu from './ContextMenu/MiniReportActionContextMenu';
import * as ReportActionContextMenu from './ContextMenu/ReportActionContextMenu';
import * as ContextMenuActions from './ContextMenu/ContextMenuActions';
import {withReportActionsDrafts} from '../../../components/OnyxProvider';
import RenameAction from '../../../components/ReportActionItem/RenameAction';
const propTypes = {
/** The ID of the report this action is on. */
reportID: PropTypes.number.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 indicator on top of the comment? */
shouldDisplayNewIndicator: 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);
}
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.shouldDisplayNewIndicator !== nextProps.shouldDisplayNewIndicator
|| !_.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.
this.textInput.focus();
}
/**
* Show the ReportActionContextMenu modal popover.
*
* @param {Object} [event] - A press event.
* @param {string} [selection] - A copy text.
*/
showPopover(event, selection) {
// Block menu on the message being Edited
if (this.props.draftMessage) {
return;
}
ReportActionContextMenu.showContextMenu(
ContextMenuActions.CONTEXT_MENU_TYPES.REPORT_ACTION,
event,
selection,
this.popoverAnchor,
this.props.reportID,
this.props.action,
this.props.draftMessage,
this.checkIfContextMenuActive,
this.checkIfContextMenuActive,
);
}
checkIfContextMenuActive() {
this.setState({isContextMenuActive: ReportActionContextMenu.isActiveReportAction(this.props.action.reportActionID)});
}
render() {
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) {
return <ReportActionItemCreated reportID={this.props.reportID} />;
}
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.RENAMED) {
return <RenameAction action={this.props.action} />;
}
let children;
if (this.props.action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU) {
children = (
<IOUAction
chatReportID={this.props.reportID}
action={this.props.action}
isMostRecentIOUReportAction={this.props.isMostRecentIOUReportAction}
/>
);
} else {
children = !this.props.draftMessage
? <ReportActionItemMessage action={this.props.action} />
: (
<ReportActionItemMessageEdit
action={this.props.action}
draftMessage={this.props.draftMessage}
reportID={this.props.reportID}
index={this.props.index}
ref={el => this.textInput = el}
/>
);
}
return (
<PressableWithSecondaryInteraction
ref={el => this.popoverAnchor = el}
onPressIn={() => this.props.isSmallScreenWidth && canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onSecondaryInteraction={this.showPopover}
preventDefaultContentMenu={!this.props.draftMessage}
>
<Hoverable resetsOnClickOutside>
{hovered => (
<View>
{this.props.shouldDisplayNewIndicator && (
<UnreadActionIndicator />
)}
<View
style={StyleUtils.getReportActionItemStyle(
hovered
|| this.state.isContextMenuActive
|| this.props.draftMessage,
)}
>
{!this.props.displayAsGroup
? (
<ReportActionItemSingle action={this.props.action} showHeader={!this.props.draftMessage}>
{children}
</ReportActionItemSingle>
)
: (
<ReportActionItemGrouped>
{children}
</ReportActionItemGrouped>
)}
</View>
<MiniReportActionContextMenu
reportID={this.props.reportID}
reportAction={this.props.action}
displayAsGroup={this.props.displayAsGroup}
isVisible={
hovered
&& !this.state.isContextMenuActive
&& !this.props.draftMessage
}
draftMessage={this.props.draftMessage}
/>
</View>
)}
</Hoverable>
</PressableWithSecondaryInteraction>
);
}
}
ReportActionItem.propTypes = propTypes;
ReportActionItem.defaultProps = defaultProps;
export default compose(
withWindowDimensions,
withReportActionsDrafts({
propName: 'draftMessage',
transformValue: (drafts, props) => {
const draftKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${props.reportID}_${props.action.reportActionID}`;
return lodashGet(drafts, draftKey, '');
},
}),
)(ReportActionItem);