-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathSidebarLinksData.js
226 lines (207 loc) · 8.33 KB
/
SidebarLinksData.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
import {deepEqual} from 'fast-equals';
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useMemo, useRef} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import withCurrentReportID from '@components/withCurrentReportID';
import withNavigationFocus from '@components/withNavigationFocus';
import useLocalize from '@hooks/useLocalize';
import compose from '@libs/compose';
import * as SessionUtils from '@libs/SessionUtils';
import SidebarUtils from '@libs/SidebarUtils';
import reportPropTypes from '@pages/reportPropTypes';
import useThemeStyles from '@styles/useThemeStyles';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import SidebarLinks, {basePropTypes} from './SidebarLinks';
const propTypes = {
...basePropTypes,
/* Onyx Props */
/** List of reports */
chatReports: PropTypes.objectOf(reportPropTypes),
/** All report actions for all reports */
allReportActions: PropTypes.objectOf(
PropTypes.arrayOf(
PropTypes.shape({
error: PropTypes.string,
message: PropTypes.arrayOf(
PropTypes.shape({
moderationDecision: PropTypes.shape({
decision: PropTypes.string,
}),
}),
),
}),
),
),
/** Whether the reports are loading. When false it means they are ready to be used. */
isLoadingReportData: PropTypes.bool,
/** The chat priority mode */
priorityMode: PropTypes.string,
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),
/** The policies which the user has access to */
// eslint-disable-next-line react/forbid-prop-types
policies: PropTypes.object,
};
const defaultProps = {
chatReports: {},
allReportActions: {},
isLoadingReportData: true,
priorityMode: CONST.PRIORITY_MODE.DEFAULT,
betas: [],
policies: {},
};
function SidebarLinksData({isFocused, allReportActions, betas, chatReports, currentReportID, insets, isLoadingReportData, onLinkClick, policies, priorityMode}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const reportIDsRef = useRef(null);
const isLoading = SessionUtils.didUserLogInDuringSession() && isLoadingReportData;
const optionListItems = useMemo(() => {
const reportIDs = SidebarUtils.getOrderedReportIDs(null, chatReports, betas, policies, priorityMode, allReportActions);
if (deepEqual(reportIDsRef.current, reportIDs)) {
return reportIDsRef.current;
}
// We need to update existing reports only once while loading because they are updated several times during loading and causes this regression: https://github.com/Expensify/App/issues/24596#issuecomment-1681679531
if (!isLoading || !reportIDsRef.current) {
reportIDsRef.current = reportIDs;
}
return reportIDsRef.current || [];
}, [allReportActions, betas, chatReports, policies, priorityMode, isLoading]);
// We need to make sure the current report is in the list of reports, but we do not want
// to have to re-generate the list every time the currentReportID changes. To do that
// we first generate the list as if there was no current report, then here we check if
// the current report is missing from the list, which should very rarely happen. In this
// case we re-generate the list a 2nd time with the current report included.
const optionListItemsWithCurrentReport = useMemo(() => {
if (currentReportID && !_.contains(optionListItems, currentReportID)) {
return SidebarUtils.getOrderedReportIDs(currentReportID, chatReports, betas, policies, priorityMode, allReportActions);
}
return optionListItems;
}, [currentReportID, optionListItems, chatReports, betas, policies, priorityMode, allReportActions]);
const currentReportIDRef = useRef(currentReportID);
currentReportIDRef.current = currentReportID;
const isActiveReport = useCallback((reportID) => currentReportIDRef.current === reportID, []);
return (
<View
accessibilityElementsHidden={!isFocused}
accessibilityLabel={translate('sidebarScreen.listOfChats')}
style={[styles.flex1, styles.h100]}
>
<SidebarLinks
// Forwarded props:
onLinkClick={onLinkClick}
insets={insets}
priorityMode={priorityMode}
// Data props:
isActiveReport={isActiveReport}
isLoading={isLoading}
optionListItems={optionListItemsWithCurrentReport}
/>
</View>
);
}
SidebarLinksData.propTypes = propTypes;
SidebarLinksData.defaultProps = defaultProps;
SidebarLinksData.displayName = 'SidebarLinksData';
/**
* This function (and the few below it), narrow down the data from Onyx to just the properties that we want to trigger a re-render of the component. This helps minimize re-rendering
* and makes the entire component more performant because it's not re-rendering when a bunch of properties change which aren't ever used in the UI.
* @param {Object} [report]
* @returns {Object|undefined}
*/
const chatReportSelector = (report) =>
report && {
reportID: report.reportID,
participantAccountIDs: report.participantAccountIDs,
hasDraft: report.hasDraft,
isPinned: report.isPinned,
isHidden: report.isHidden,
errorFields: {
addWorkspaceRoom: report.errorFields && report.errorFields.addWorkspaceRoom,
},
lastMessageText: report.lastMessageText,
lastVisibleActionCreated: report.lastVisibleActionCreated,
iouReportID: report.iouReportID,
total: report.total,
nonReimbursableTotal: report.nonReimbursableTotal,
hasOutstandingIOU: report.hasOutstandingIOU,
hasOutstandingChildRequest: report.hasOutstandingChildRequest,
isWaitingOnBankAccount: report.isWaitingOnBankAccount,
statusNum: report.statusNum,
stateNum: report.stateNum,
chatType: report.chatType,
type: report.type,
policyID: report.policyID,
visibility: report.visibility,
lastReadTime: report.lastReadTime,
// Needed for name sorting:
reportName: report.reportName,
policyName: report.policyName,
oldPolicyName: report.oldPolicyName,
// Other less obvious properites considered for sorting:
ownerAccountID: report.ownerAccountID,
currency: report.currency,
managerID: report.managerID,
// Other important less obivous properties for filtering:
parentReportActionID: report.parentReportActionID,
parentReportID: report.parentReportID,
};
/**
* @param {Object} [reportActions]
* @returns {Object|undefined}
*/
const reportActionsSelector = (reportActions) =>
reportActions &&
_.map(reportActions, (reportAction) => ({
errors: lodashGet(reportAction, 'errors', []),
message: [
{
moderationDecision: {decision: lodashGet(reportAction, 'message[0].moderationDecision.decision')},
},
],
}));
/**
* @param {Object} [policy]
* @returns {Object|undefined}
*/
const policySelector = (policy) =>
policy && {
type: policy.type,
name: policy.name,
avatar: policy.avatar,
};
export default compose(
withCurrentReportID,
withNavigationFocus,
withOnyx({
chatReports: {
key: ONYXKEYS.COLLECTION.REPORT,
selector: chatReportSelector,
initialValue: {},
},
isLoadingReportData: {
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
},
priorityMode: {
key: ONYXKEYS.NVP_PRIORITY_MODE,
initialValue: CONST.PRIORITY_MODE.DEFAULT,
},
betas: {
key: ONYXKEYS.BETAS,
initialValue: [],
},
allReportActions: {
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
selector: reportActionsSelector,
initialValue: {},
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
selector: policySelector,
initialValue: {},
},
}),
)(SidebarLinksData);