-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionsView.js
executable file
·386 lines (329 loc) · 16.4 KB
/
ReportActionsView.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
import React from 'react';
import {
Keyboard,
} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import * as Report from '../../../libs/actions/Report';
import reportActionPropTypes from './reportActionPropTypes';
import * as CollectionUtils from '../../../libs/CollectionUtils';
import Visibility from '../../../libs/Visibility';
import Timing from '../../../libs/actions/Timing';
import CONST from '../../../CONST';
import compose from '../../../libs/compose';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import {withDrawerPropTypes} from '../../../components/withDrawerState';
import * as ReportScrollManager from '../../../libs/ReportScrollManager';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import ReportActionComposeFocusManager from '../../../libs/ReportActionComposeFocusManager';
import * as ReportActionContextMenu from './ContextMenu/ReportActionContextMenu';
import PopoverReportActionContextMenu from './ContextMenu/PopoverReportActionContextMenu';
import Performance from '../../../libs/Performance';
import {withNetwork} from '../../../components/OnyxProvider';
import * as EmojiPickerAction from '../../../libs/actions/EmojiPickerAction';
import FloatingMessageCounter from './FloatingMessageCounter';
import networkPropTypes from '../../../components/networkPropTypes';
import ReportActionsList from './ReportActionsList';
import CopySelectionHelper from '../../../components/CopySelectionHelper';
import EmojiPicker from '../../../components/EmojiPicker/EmojiPicker';
import * as ReportActionsUtils from '../../../libs/ReportActionsUtils';
import * as ReportUtils from '../../../libs/ReportUtils';
import reportPropTypes from '../../reportPropTypes';
const propTypes = {
/* Onyx Props */
/** The report currently being looked at */
report: reportPropTypes.isRequired,
/** Array of report actions for this report */
reportActions: PropTypes.objectOf(PropTypes.shape(reportActionPropTypes)),
/** The session of the logged in person */
session: PropTypes.shape({
/** Email of the logged in person */
email: PropTypes.string,
}),
/** Whether the composer is full size */
isComposerFullSize: PropTypes.bool.isRequired,
/** Information about the network */
network: networkPropTypes.isRequired,
...windowDimensionsPropTypes,
...withDrawerPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
reportActions: {},
session: {},
};
class ReportActionsView extends React.Component {
constructor(props) {
super(props);
this.didLayout = false;
this.unsubscribeVisibilityListener = null;
this.state = {
isFloatingMessageCounterVisible: false,
newMarkerSequenceNumber: ReportUtils.isUnread(props.report)
? props.report.lastReadSequenceNumber + 1
: 0,
};
this.currentScrollOffset = 0;
this.sortedReportActions = ReportActionsUtils.getSortedReportActions(props.reportActions);
this.mostRecentIOUReportSequenceNumber = ReportActionsUtils.getMostRecentIOUReportSequenceNumber(props.reportActions);
this.trackScroll = this.trackScroll.bind(this);
this.toggleFloatingMessageCounter = this.toggleFloatingMessageCounter.bind(this);
this.loadMoreChats = this.loadMoreChats.bind(this);
this.recordTimeToMeasureItemLayout = this.recordTimeToMeasureItemLayout.bind(this);
this.scrollToUnreadMsgAndMarkReportAsRead = this.scrollToUnreadMsgAndMarkReportAsRead.bind(this);
this.openReportIfNecessary = this.openReportIfNecessary.bind(this);
}
componentDidMount() {
this.unsubscribeVisibilityListener = Visibility.onVisibilityChange(() => {
if (!this.getIsReportFullyVisible()) {
return;
}
// If the app user becomes active and they have no unread actions we clear the new marker to sync their device
// e.g. they could have read these messages on another device and only just become active here
this.openReportIfNecessary();
this.setState({newMarkerSequenceNumber: 0});
});
Report.subscribeToReportTypingEvents(this.props.report.reportID);
this.keyboardEvent = Keyboard.addListener('keyboardDidShow', () => {
if (!ReportActionComposeFocusManager.isFocused()) {
return;
}
ReportScrollManager.scrollToBottom();
});
if (this.getIsReportFullyVisible()) {
this.openReportIfNecessary();
}
}
shouldComponentUpdate(nextProps, nextState) {
if (!_.isEqual(nextProps.reportActions, this.props.reportActions)) {
this.sortedReportActions = ReportActionsUtils.getSortedReportActions(nextProps.reportActions);
this.mostRecentIOUReportSequenceNumber = ReportActionsUtils.getMostRecentIOUReportSequenceNumber(nextProps.reportActions);
return true;
}
if (lodashGet(nextProps.network, 'isOffline') !== lodashGet(this.props.network, 'isOffline')) {
return true;
}
if (nextProps.report.isLoadingMoreReportActions !== this.props.report.isLoadingMoreReportActions) {
return true;
}
if (nextProps.report.lastReadSequenceNumber !== this.props.report.lastReadSequenceNumber) {
return true;
}
if (nextState.isFloatingMessageCounterVisible !== this.state.isFloatingMessageCounterVisible) {
return true;
}
if (nextState.newMarkerSequenceNumber !== this.state.newMarkerSequenceNumber) {
return true;
}
if (this.props.isSmallScreenWidth !== nextProps.isSmallScreenWidth) {
return true;
}
if (this.props.isDrawerOpen !== nextProps.isDrawerOpen) {
return true;
}
if (lodashGet(this.props.report, 'hasOutstandingIOU') !== lodashGet(nextProps.report, 'hasOutstandingIOU')) {
return true;
}
if (this.props.isComposerFullSize !== nextProps.isComposerFullSize) {
return true;
}
return !_.isEqual(lodashGet(this.props.report, 'icons', []), lodashGet(nextProps.report, 'icons', []));
}
componentDidUpdate(prevProps) {
const isReportFullyVisible = this.getIsReportFullyVisible();
// When returning from offline to online state we want to trigger a request to OpenReport which
// will fetch the reportActions data and mark the report as read. If the report is not fully visible
// then we call ReconnectToReport which only loads the reportActions data without marking the report as read.
const wasNetworkChangeDetected = lodashGet(prevProps.network, 'isOffline') && !lodashGet(this.props.network, 'isOffline');
if (wasNetworkChangeDetected) {
if (isReportFullyVisible) {
this.openReportIfNecessary();
} else {
Report.reconnect(this.props.report.reportID);
}
}
const previousLastSequenceNumber = lodashGet(CollectionUtils.lastItem(prevProps.reportActions), 'sequenceNumber');
const currentLastSequenceNumber = lodashGet(CollectionUtils.lastItem(this.props.reportActions), 'sequenceNumber');
const didNewReportActionAppear = previousLastSequenceNumber !== currentLastSequenceNumber;
if (didNewReportActionAppear) {
const lastAction = CollectionUtils.lastItem(this.props.reportActions);
const isLastActionFromCurrentUser = lodashGet(lastAction, 'actorEmail', '') === lodashGet(this.props.session, 'email', '');
// If a new comment is added and it's from the current user scroll to the bottom otherwise leave the user positioned where
// they are now in the list.
if (isLastActionFromCurrentUser) {
ReportScrollManager.scrollToBottom();
// If the current user sends a new message in the chat we clear the new marker since they have "read" the report
this.setState({newMarkerSequenceNumber: 0});
} else if (isReportFullyVisible) {
// We use the scroll position to determine whether the report should be marked as read and the new line indicator reset.
// If the user is scrolled up and no new line marker is set we will set it otherwise we will do nothing so the new marker
// stays in it's previous position.
if (this.currentScrollOffset === 0) {
Report.readNewestAction(this.props.report.reportID);
this.setState({newMarkerSequenceNumber: 0});
} else if (this.state.newMarkerSequenceNumber === 0) {
this.setState({newMarkerSequenceNumber: currentLastSequenceNumber});
}
} else if (this.state.newMarkerSequenceNumber === 0) {
// The report is not in view and we received a comment from another user while the new marker is not set
// so we will set the new marker now.
this.setState({newMarkerSequenceNumber: currentLastSequenceNumber});
}
}
// If the report was previously hidden by the side bar, or the view is expanded from mobile to desktop layout
// we update the new marker position, mark the report as read, and fetch new report actions
const didSidebarClose = prevProps.isDrawerOpen && !this.props.isDrawerOpen;
const didScreenSizeIncrease = prevProps.isSmallScreenWidth && !this.props.isSmallScreenWidth;
const didReportBecomeVisible = isReportFullyVisible && (didSidebarClose || didScreenSizeIncrease);
if (didReportBecomeVisible) {
this.setState({
newMarkerSequenceNumber: !ReportUtils.isUnread(this.props.report)
? 0
: this.props.report.lastReadSequenceNumber + 1,
});
this.openReportIfNecessary();
}
// When the user navigates to the LHN the ReportActionsView doesn't unmount and just remains hidden.
// The next time we navigate to the same report (e.g. by swiping or tapping the LHN row) we want the new marker to clear.
const didSidebarOpen = !prevProps.isDrawerOpen && this.props.isDrawerOpen;
const didUserNavigateToSidebarAfterReadingReport = didSidebarOpen && !ReportUtils.isUnread(this.props.report);
if (didUserNavigateToSidebarAfterReadingReport) {
this.setState({newMarkerSequenceNumber: 0});
}
// Checks to see if a report comment has been manually "marked as unread". All other times when the lastReadSequenceNumber
// changes it will be because we marked the entire report as read.
const didManuallyMarkReportAsUnread = (prevProps.report.lastReadSequenceNumber !== this.props.report.lastReadSequenceNumber)
&& ReportUtils.isUnread(this.props.report);
if (didManuallyMarkReportAsUnread) {
this.setState({newMarkerSequenceNumber: this.props.report.lastReadSequenceNumber + 1});
}
}
componentWillUnmount() {
if (this.keyboardEvent) {
this.keyboardEvent.remove();
}
if (this.unsubscribeVisibilityListener) {
this.unsubscribeVisibilityListener();
}
Report.unsubscribeFromReportChannel(this.props.report.reportID);
}
/**
* @returns {Boolean}
*/
getIsReportFullyVisible() {
const isSidebarCoveringReportView = this.props.isSmallScreenWidth && this.props.isDrawerOpen;
return Visibility.isVisible() && !isSidebarCoveringReportView;
}
// If the report is optimistic (AKA not yet created) we don't need to call openReport again
openReportIfNecessary() {
if (this.props.report.isOptimisticReport) {
return;
}
Report.openReport(this.props.report.reportID);
}
/**
* Retrieves the next set of report actions for the chat once we are nearing the end of what we are currently
* displaying.
*/
loadMoreChats() {
// Only fetch more if we are not already fetching so that we don't initiate duplicate requests.
if (this.props.report.isLoadingMoreReportActions) {
return;
}
const minSequenceNumber = _.chain(this.props.reportActions)
.pluck('sequenceNumber')
.min()
.value();
if (minSequenceNumber === 0) {
return;
}
// Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments, unless we're near the beginning, in which
// case just get everything starting from 0.
const oldestActionSequenceNumber = Math.max(minSequenceNumber - CONST.REPORT.ACTIONS.LIMIT, 0);
Report.readOldestAction(this.props.report.reportID, oldestActionSequenceNumber);
}
scrollToUnreadMsgAndMarkReportAsRead() {
ReportScrollManager.scrollToIndex({animated: true, index: this.props.report.maxSequenceNumber - this.state.newMarkerSequenceNumber}, false);
Report.readNewestAction(this.props.report.reportID);
}
/**
* Show/hide the new floating message counter when user is scrolling back/forth in the history of messages.
*/
toggleFloatingMessageCounter() {
if (this.currentScrollOffset < -200 && !this.state.isFloatingMessageCounterVisible) {
this.setState({isFloatingMessageCounterVisible: true});
}
if (this.currentScrollOffset > -200 && this.state.isFloatingMessageCounterVisible) {
this.setState({isFloatingMessageCounterVisible: false});
}
}
/**
* keeps track of the Scroll offset of the main messages list
*
* @param {Object} {nativeEvent}
*/
trackScroll({nativeEvent}) {
this.currentScrollOffset = -nativeEvent.contentOffset.y;
this.toggleFloatingMessageCounter();
}
/**
* Runs when the FlatList finishes laying out
*/
recordTimeToMeasureItemLayout() {
if (this.didLayout) {
return;
}
this.didLayout = true;
Timing.end(CONST.TIMING.SWITCH_REPORT, CONST.TIMING.COLD);
// Capture the init measurement only once not per each chat switch as the value gets overwritten
if (!ReportActionsView.initMeasured) {
Performance.markEnd(CONST.TIMING.REPORT_INITIAL_RENDER);
ReportActionsView.initMeasured = true;
} else {
Performance.markEnd(CONST.TIMING.SWITCH_REPORT);
}
}
render() {
// Comments have not loaded at all yet do nothing
if (!_.size(this.props.reportActions)) {
return null;
}
return (
<>
{!this.props.isComposerFullSize && (
<>
<FloatingMessageCounter
isActive={this.state.isFloatingMessageCounterVisible && this.state.newMarkerSequenceNumber > 0}
onClick={this.scrollToUnreadMsgAndMarkReportAsRead}
/>
<ReportActionsList
report={this.props.report}
onScroll={this.trackScroll}
onLayout={this.recordTimeToMeasureItemLayout}
sortedReportActions={this.sortedReportActions}
mostRecentIOUReportSequenceNumber={this.mostRecentIOUReportSequenceNumber}
isLoadingMoreReportActions={this.props.report.isLoadingMoreReportActions}
loadMoreChats={this.loadMoreChats}
newMarkerSequenceNumber={this.state.newMarkerSequenceNumber}
/>
<PopoverReportActionContextMenu
ref={ReportActionContextMenu.contextMenuRef}
isArchivedRoom={ReportUtils.isArchivedRoom(this.props.report)}
/>
</>
)}
<EmojiPicker ref={EmojiPickerAction.emojiPickerRef} />
<CopySelectionHelper />
</>
);
}
}
ReportActionsView.propTypes = propTypes;
ReportActionsView.defaultProps = defaultProps;
export default compose(
Performance.withRenderTrace({id: '<ReportActionsView> rendering'}),
withWindowDimensions,
withLocalize,
withNetwork(),
)(ReportActionsView);