-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionsList.js
192 lines (169 loc) · 8.68 KB
/
ReportActionsList.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
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useState} from 'react';
import Animated, {useSharedValue, useAnimatedStyle, withTiming} from 'react-native-reanimated';
import _ from 'underscore';
import InvertedFlatList from '../../../components/InvertedFlatList';
import withDrawerState, {withDrawerPropTypes} from '../../../components/withDrawerState';
import compose from '../../../libs/compose';
import * as ReportScrollManager from '../../../libs/ReportScrollManager';
import styles from '../../../styles/styles';
import * as ReportUtils from '../../../libs/ReportUtils';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import {withNetwork, withPersonalDetails} from '../../../components/OnyxProvider';
import ReportActionItem from './ReportActionItem';
import ReportActionItemParentAction from './ReportActionItemParentAction';
import ReportActionsSkeletonView from '../../../components/ReportActionsSkeletonView';
import variables from '../../../styles/variables';
import participantPropTypes from '../../../components/participantPropTypes';
import * as ReportActionsUtils from '../../../libs/ReportActionsUtils';
import reportActionPropTypes from './reportActionPropTypes';
import CONST from '../../../CONST';
import reportPropTypes from '../../reportPropTypes';
import networkPropTypes from '../../../components/networkPropTypes';
import withLocalize from '../../../components/withLocalize';
const propTypes = {
/** Position of the "New" line marker */
newMarkerReportActionID: PropTypes.string,
/** Personal details of all the users */
personalDetails: PropTypes.objectOf(participantPropTypes),
/** The report currently being looked at */
report: reportPropTypes.isRequired,
/** Sorted actions prepared for display */
sortedReportActions: PropTypes.arrayOf(PropTypes.shape(reportActionPropTypes)).isRequired,
/** The ID of the most recent IOU report action connected with the shown report */
mostRecentIOUReportActionID: PropTypes.string,
/** Are we loading more report actions? */
isLoadingMoreReportActions: PropTypes.bool,
/** Callback executed on list layout */
onLayout: PropTypes.func.isRequired,
/** Callback executed on scroll */
onScroll: PropTypes.func.isRequired,
/** Function to load more chats */
loadMoreChats: PropTypes.func.isRequired,
/** Information about the network */
network: networkPropTypes.isRequired,
...withDrawerPropTypes,
...windowDimensionsPropTypes,
};
const defaultProps = {
newMarkerReportActionID: '',
personalDetails: {},
mostRecentIOUReportActionID: '',
isLoadingMoreReportActions: false,
};
/**
* Create a unique key for each action in the FlatList.
* We use the reportActionID that is a string representation of a random 64-bit int, which should be
* random enough to avoid collisions
* @param {Object} item
* @param {Object} item.action
* @return {String}
*/
function keyExtractor(item) {
return item.reportActionID;
}
const ReportActionsList = (props) => {
const opacity = useSharedValue(0);
const animatedStyles = useAnimatedStyle(() => ({
opacity: withTiming(opacity.value, {duration: 100}),
}));
useEffect(() => {
opacity.value = 1;
}, [opacity]);
const [skeletonViewHeight, setSkeletonViewHeight] = useState(0);
const windowHeight = props.windowHeight;
/**
* Calculates the ideal number of report actions to render in the first render, based on the screen height and on
* the height of the smallest report action possible.
* @return {Number}
*/
const calculateInitialNumToRender = useCallback(() => {
const minimumReportActionHeight = styles.chatItem.paddingTop + styles.chatItem.paddingBottom + variables.fontSizeNormalHeight;
const availableHeight = windowHeight - (CONST.CHAT_FOOTER_MIN_HEIGHT + variables.contentHeaderHeight);
return Math.ceil(availableHeight / minimumReportActionHeight);
}, [windowHeight]);
const report = props.report;
const hasOutstandingIOU = props.report.hasOutstandingIOU;
const newMarkerReportActionID = props.newMarkerReportActionID;
const sortedReportActions = props.sortedReportActions;
const mostRecentIOUReportActionID = props.mostRecentIOUReportActionID;
/**
* @param {Object} args
* @param {Number} args.index
* @returns {React.Component}
*/
const renderItem = useCallback(
({item: reportAction, index}) => {
// When the new indicator should not be displayed we explicitly set it to null
const shouldDisplayNewMarker = reportAction.reportActionID === newMarkerReportActionID;
const shouldDisplayParentAction = reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED && ReportUtils.isThread(report);
return shouldDisplayParentAction ? (
<ReportActionItemParentAction
reportID={report.reportID}
parentReportID={`${report.parentReportID}`}
/>
) : (
<ReportActionItem
report={report}
action={reportAction}
displayAsGroup={ReportActionsUtils.isConsecutiveActionMadeByPreviousActor(sortedReportActions, index)}
shouldDisplayNewMarker={shouldDisplayNewMarker}
shouldShowSubscriptAvatar={ReportUtils.isPolicyExpenseChat(report) && reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU}
isMostRecentIOUReportAction={reportAction.reportActionID === mostRecentIOUReportActionID}
hasOutstandingIOU={hasOutstandingIOU}
index={index}
/>
);
},
[report, hasOutstandingIOU, newMarkerReportActionID, sortedReportActions, mostRecentIOUReportActionID],
);
// Native mobile does not render updates flatlist the changes even though component did update called.
// To notify there something changes we can use extraData prop to flatlist
const extraData = [!props.isDrawerOpen && props.isSmallScreenWidth ? props.newMarkerReportActionID : undefined, ReportUtils.isArchivedRoom(props.report)];
const shouldShowReportRecipientLocalTime = ReportUtils.canShowReportRecipientLocalTime(props.personalDetails, props.report);
return (
<Animated.View style={[animatedStyles, styles.flex1]}>
<InvertedFlatList
accessibilityLabel={props.translate('sidebarScreen.listOfChatMessages')}
ref={ReportScrollManager.flatListRef}
data={props.sortedReportActions}
renderItem={renderItem}
contentContainerStyle={[styles.chatContentScrollView, shouldShowReportRecipientLocalTime && styles.pt0]}
keyExtractor={keyExtractor}
initialRowHeight={32}
initialNumToRender={calculateInitialNumToRender()}
onEndReached={props.loadMoreChats}
onEndReachedThreshold={0.75}
ListFooterComponent={() => {
if (props.report.isLoadingMoreReportActions) {
return <ReportActionsSkeletonView containerHeight={CONST.CHAT_SKELETON_VIEW.AVERAGE_ROW_HEIGHT * 3} />;
}
// Make sure the oldest report action loaded is not the first. This is so we do not show the
// skeleton view above the created action in a newly generated optimistic chat or one with not
// that many comments.
const lastReportAction = _.last(props.sortedReportActions) || {};
if (props.report.isLoadingReportActions && lastReportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED) {
return (
<ReportActionsSkeletonView
containerHeight={skeletonViewHeight}
animate={!props.network.isOffline}
/>
);
}
return null;
}}
keyboardShouldPersistTaps="handled"
onLayout={(event) => {
setSkeletonViewHeight(event.nativeEvent.layout.height);
props.onLayout(event);
}}
onScroll={props.onScroll}
extraData={extraData}
/>
</Animated.View>
);
};
ReportActionsList.propTypes = propTypes;
ReportActionsList.defaultProps = defaultProps;
ReportActionsList.displayName = 'ReportActionsList';
export default compose(withDrawerState, withWindowDimensions, withLocalize, withPersonalDetails(), withNetwork())(ReportActionsList);