-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReportActionsView.js
executable file
·379 lines (318 loc) · 14.9 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
import {useIsFocused} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useContext, useEffect, useMemo, useRef} from 'react';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import networkPropTypes from '@components/networkPropTypes';
import {withNetwork} from '@components/OnyxProvider';
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import withWindowDimensions, {windowDimensionsPropTypes} from '@components/withWindowDimensions';
import useCopySelectionHelper from '@hooks/useCopySelectionHelper';
import useInitialValue from '@hooks/useInitialValue';
import usePrevious from '@hooks/usePrevious';
import compose from '@libs/compose';
import getIsReportFullyVisible from '@libs/getIsReportFullyVisible';
import Performance from '@libs/Performance';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import {isUserCreatedPolicyRoom} from '@libs/ReportUtils';
import {didUserLogInDuringSession} from '@libs/SessionUtils';
import {ReactionListContext} from '@pages/home/ReportScreenContext';
import reportPropTypes from '@pages/reportPropTypes';
import * as Report from '@userActions/Report';
import Timing from '@userActions/Timing';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import PopoverReactionList from './ReactionList/PopoverReactionList';
import reportActionPropTypes from './reportActionPropTypes';
import ReportActionsList from './ReportActionsList';
const propTypes = {
/** The report currently being looked at */
report: reportPropTypes.isRequired,
/** Array of report actions for this report */
reportActions: PropTypes.arrayOf(PropTypes.shape(reportActionPropTypes)),
/** The report metadata loading states */
isLoadingInitialReportActions: PropTypes.bool,
/** The report actions are loading more data */
isLoadingOlderReportActions: PropTypes.bool,
/** The report actions are loading newer data */
isLoadingNewerReportActions: PropTypes.bool,
/** Whether the composer is full size */
/* eslint-disable-next-line react/no-unused-prop-types */
isComposerFullSize: PropTypes.bool.isRequired,
/** Information about the network */
network: networkPropTypes.isRequired,
/** The policy object for the current route */
policy: PropTypes.shape({
/** The name of the policy */
name: PropTypes.string,
/** The URL for the policy avatar */
avatar: PropTypes.string,
}),
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user authToken */
authToken: PropTypes.string,
}),
...windowDimensionsPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
reportActions: [],
policy: null,
isLoadingInitialReportActions: false,
isLoadingOlderReportActions: false,
isLoadingNewerReportActions: false,
session: {
authTokenType: '',
},
};
function ReportActionsView(props) {
useCopySelectionHelper();
const reactionListRef = useContext(ReactionListContext);
const didLayout = useRef(false);
const didSubscribeToReportTypingEvents = useRef(false);
const isFirstRender = useRef(true);
const hasCachedActions = useInitialValue(() => _.size(props.reportActions) > 0);
const mostRecentIOUReportActionID = useInitialValue(() => ReportActionsUtils.getMostRecentIOURequestActionID(props.reportActions));
const prevNetworkRef = useRef(props.network);
const prevAuthTokenType = usePrevious(props.session.authTokenType);
const prevIsSmallScreenWidthRef = useRef(props.isSmallScreenWidth);
const isFocused = useIsFocused();
const reportID = props.report.reportID;
/**
* @returns {Boolean}
*/
const isReportFullyVisible = useMemo(() => getIsReportFullyVisible(isFocused), [isFocused]);
const openReportIfNecessary = () => {
// If the report is optimistic (AKA not yet created) we don't need to call openReport again
if (props.report.isOptimisticReport) {
return;
}
Report.openReport(reportID);
};
useEffect(() => {
openReportIfNecessary();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
const prevNetwork = prevNetworkRef.current;
// 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(prevNetwork, 'isOffline') && !lodashGet(props.network, 'isOffline');
if (wasNetworkChangeDetected) {
if (isReportFullyVisible) {
openReportIfNecessary();
} else {
Report.reconnect(reportID);
}
}
// update ref with current network state
prevNetworkRef.current = props.network;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.network, props.report, isReportFullyVisible]);
useEffect(() => {
const wasLoginChangedDetected = prevAuthTokenType === 'anonymousAccount' && !props.session.authTokenType;
if (wasLoginChangedDetected && didUserLogInDuringSession() && isUserCreatedPolicyRoom(props.report)) {
if (isReportFullyVisible) {
openReportIfNecessary();
} else {
Report.reconnect(reportID);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.session, props.report, isReportFullyVisible]);
useEffect(() => {
const prevIsSmallScreenWidth = prevIsSmallScreenWidthRef.current;
// If 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 didScreenSizeIncrease = prevIsSmallScreenWidth && !props.isSmallScreenWidth;
const didReportBecomeVisible = isReportFullyVisible && didScreenSizeIncrease;
if (didReportBecomeVisible) {
openReportIfNecessary();
}
// update ref with current state
prevIsSmallScreenWidthRef.current = props.isSmallScreenWidth;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.isSmallScreenWidth, props.report, props.reportActions, isReportFullyVisible]);
useEffect(() => {
// Ensures subscription event succeeds when the report/workspace room is created optimistically.
// Check if the optimistic `OpenReport` or `AddWorkspaceRoom` has succeeded by confirming
// any `pendingFields.createChat` or `pendingFields.addWorkspaceRoom` fields are set to null.
// Existing reports created will have empty fields for `pendingFields`.
const didCreateReportSuccessfully = !props.report.pendingFields || (!props.report.pendingFields.addWorkspaceRoom && !props.report.pendingFields.createChat);
if (!didSubscribeToReportTypingEvents.current && didCreateReportSuccessfully) {
Report.subscribeToReportTypingEvents(reportID);
didSubscribeToReportTypingEvents.current = true;
}
}, [props.report, didSubscribeToReportTypingEvents, reportID]);
/**
* Retrieves the next set of report actions for the chat once we are nearing the end of what we are currently
* displaying.
*/
const loadOlderChats = () => {
// Only fetch more if we are neither already fetching (so that we don't initiate duplicate requests) nor offline.
if (props.network.isOffline || props.isLoadingOlderReportActions) {
return;
}
const oldestReportAction = _.last(props.reportActions);
// Don't load more chats if we're already at the beginning of the chat history
if (oldestReportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) {
return;
}
// Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments
Report.getOlderActions(reportID, oldestReportAction.reportActionID);
};
/**
* Retrieves the next set of report actions for the chat once we are nearing the end of what we are currently
* displaying.
*/
const loadNewerChats = useMemo(
() =>
_.throttle(({distanceFromStart}) => {
if (props.isLoadingNewerReportActions || props.isLoadingInitialReportActions) {
return;
}
// Ideally, we wouldn't need to use the 'distanceFromStart' variable. However, due to the low value set for 'maxToRenderPerBatch',
// the component undergoes frequent re-renders. This frequent re-rendering triggers the 'onStartReached' callback multiple times.
//
// To mitigate this issue, we use 'CONST.CHAT_HEADER_LOADER_HEIGHT' as a threshold. This ensures that 'onStartReached' is not
// triggered unnecessarily when the chat is initially opened or when the user has reached the end of the list but hasn't scrolled further.
//
// Additionally, we use throttling on the 'onStartReached' callback to further reduce the frequency of its invocation.
// This should be removed once the issue of frequent re-renders is resolved.
//
// onStartReached is triggered during the first render. Since we use OpenReport on the first render and are confident about the message ordering, we can safely skip this call
if (isFirstRender.current || distanceFromStart <= CONST.CHAT_HEADER_LOADER_HEIGHT) {
isFirstRender.current = false;
return;
}
const newestReportAction = _.first(props.reportActions);
Report.getNewerActions(reportID, newestReportAction.reportActionID);
}, 500),
[props.isLoadingNewerReportActions, props.isLoadingInitialReportActions, props.reportActions, reportID],
);
/**
* Runs when the FlatList finishes laying out
*/
const recordTimeToMeasureItemLayout = () => {
if (didLayout.current) {
return;
}
didLayout.current = true;
Timing.end(CONST.TIMING.SWITCH_REPORT, hasCachedActions ? CONST.TIMING.WARM : 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);
}
};
// Comments have not loaded at all yet do nothing
if (!_.size(props.reportActions)) {
return null;
}
return (
<>
<ReportActionsList
report={props.report}
onLayout={recordTimeToMeasureItemLayout}
sortedReportActions={props.reportActions}
mostRecentIOUReportActionID={mostRecentIOUReportActionID}
loadOlderChats={loadOlderChats}
loadNewerChats={loadNewerChats}
isLoadingInitialReportActions={props.isLoadingInitialReportActions}
isLoadingOlderReportActions={props.isLoadingOlderReportActions}
isLoadingNewerReportActions={props.isLoadingNewerReportActions}
policy={props.policy}
/>
<PopoverReactionList ref={reactionListRef} />
</>
);
}
ReportActionsView.propTypes = propTypes;
ReportActionsView.defaultProps = defaultProps;
ReportActionsView.displayName = 'ReportActionsView';
function arePropsEqual(oldProps, newProps) {
if (!_.isEqual(oldProps.reportActions, newProps.reportActions)) {
return false;
}
if (!_.isEqual(oldProps.report.pendingFields, newProps.report.pendingFields)) {
return false;
}
if (!_.isEqual(oldProps.report.errorFields, newProps.report.errorFields)) {
return false;
}
if (lodashGet(oldProps.network, 'isOffline') !== lodashGet(newProps.network, 'isOffline')) {
return false;
}
if (lodashGet(oldProps.session, 'authTokenType') !== lodashGet(newProps.session, 'authTokenType')) {
return false;
}
if (oldProps.isLoadingInitialReportActions !== newProps.isLoadingInitialReportActions) {
return false;
}
if (oldProps.isLoadingOlderReportActions !== newProps.isLoadingOlderReportActions) {
return false;
}
if (oldProps.isLoadingNewerReportActions !== newProps.isLoadingNewerReportActions) {
return false;
}
if (oldProps.report.lastReadTime !== newProps.report.lastReadTime) {
return false;
}
if (newProps.isSmallScreenWidth !== oldProps.isSmallScreenWidth) {
return false;
}
if (lodashGet(newProps.report, 'hasOutstandingIOU') !== lodashGet(oldProps.report, 'hasOutstandingIOU')) {
return false;
}
if (newProps.isComposerFullSize !== oldProps.isComposerFullSize) {
return false;
}
if (lodashGet(newProps.report, 'statusNum') !== lodashGet(oldProps.report, 'statusNum') || lodashGet(newProps.report, 'stateNum') !== lodashGet(oldProps.report, 'stateNum')) {
return false;
}
if (lodashGet(newProps, 'policy.avatar') !== lodashGet(oldProps, 'policy.avatar')) {
return false;
}
if (lodashGet(newProps, 'policy.name') !== lodashGet(oldProps, 'policy.name')) {
return false;
}
if (lodashGet(newProps, 'report.reportName') !== lodashGet(oldProps, 'report.reportName')) {
return false;
}
if (lodashGet(newProps, 'report.description') !== lodashGet(oldProps, 'report.description')) {
return false;
}
if (lodashGet(newProps, 'report.managerID') !== lodashGet(oldProps, 'report.managerID')) {
return false;
}
if (lodashGet(newProps, 'report.total') !== lodashGet(oldProps, 'report.total')) {
return false;
}
if (lodashGet(newProps, 'report.nonReimbursableTotal') !== lodashGet(oldProps, 'report.nonReimbursableTotal')) {
return false;
}
if (lodashGet(newProps, 'report.writeCapability') !== lodashGet(oldProps, 'report.writeCapability')) {
return false;
}
if (lodashGet(newProps, 'report.participantAccountIDs', 0) !== lodashGet(oldProps, 'report.participantAccountIDs', 0)) {
return false;
}
return _.isEqual(lodashGet(newProps.report, 'icons', []), lodashGet(oldProps.report, 'icons', []));
}
const MemoizedReportActionsView = React.memo(ReportActionsView, arePropsEqual);
export default compose(
Performance.withRenderTrace({id: '<ReportActionsView> rendering'}),
withWindowDimensions,
withLocalize,
withNetwork(),
withOnyx({
session: {
key: ONYXKEYS.SESSION,
},
}),
)(MemoizedReportActionsView);