-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SidebarLinks.js
339 lines (308 loc) · 13.5 KB
/
SidebarLinks.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
/* eslint-disable rulesdir/onyx-props-must-have-default */
import lodashGet from 'lodash/get';
import React from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import styles from '../../../styles/styles';
import * as StyleUtils from '../../../styles/StyleUtils';
import ONYXKEYS from '../../../ONYXKEYS';
import safeAreaInsetPropTypes from '../../safeAreaInsetPropTypes';
import compose from '../../../libs/compose';
import Navigation from '../../../libs/Navigation/Navigation';
import ROUTES from '../../../ROUTES';
import Icon from '../../../components/Icon';
import * as Expensicons from '../../../components/Icon/Expensicons';
import AvatarWithIndicator from '../../../components/AvatarWithIndicator';
import Tooltip from '../../../components/Tooltip';
import CONST from '../../../CONST';
import participantPropTypes from '../../../components/participantPropTypes';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import * as App from '../../../libs/actions/App';
import withCurrentUserPersonalDetails from '../../../components/withCurrentUserPersonalDetails';
import withWindowDimensions from '../../../components/withWindowDimensions';
import reportActionPropTypes from '../report/reportActionPropTypes';
import LHNOptionsList from '../../../components/LHNOptionsList/LHNOptionsList';
import SidebarUtils from '../../../libs/SidebarUtils';
import reportPropTypes from '../../reportPropTypes';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
import withNavigationFocus from '../../../components/withNavigationFocus';
import withCurrentReportId from '../../../components/withCurrentReportId';
import withNavigation, {withNavigationPropTypes} from '../../../components/withNavigation';
import Header from '../../../components/Header';
import defaultTheme from '../../../styles/themes/default';
import OptionsListSkeletonView from '../../../components/OptionsListSkeletonView';
import variables from '../../../styles/variables';
import LogoComponent from '../../../../assets/images/expensify-wordmark.svg';
import PressableWithoutFeedback from '../../../components/Pressable/PressableWithoutFeedback';
import * as Session from '../../../libs/actions/Session';
import Button from '../../../components/Button';
import * as UserUtils from '../../../libs/UserUtils';
const propTypes = {
/** Toggles the navigation menu open and closed */
onLinkClick: PropTypes.func.isRequired,
/** Safe area insets required for mobile devices margins */
insets: safeAreaInsetPropTypes.isRequired,
/* Onyx Props */
/** List of reports */
// eslint-disable-next-line react/no-unused-prop-types
chatReports: PropTypes.objectOf(reportPropTypes),
/** All report actions for all reports */
// eslint-disable-next-line react/no-unused-prop-types
reportActions: PropTypes.objectOf(PropTypes.arrayOf(PropTypes.shape(reportActionPropTypes))),
/** List of users' personal details */
personalDetails: PropTypes.objectOf(participantPropTypes),
/** The personal details of the person who is logged in */
currentUserPersonalDetails: PropTypes.shape({
/** Display name of the current user */
displayName: PropTypes.string,
/** Avatar URL or SVG of the current user */
avatar: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
/** Login email of the current user */
login: PropTypes.string,
}),
/** Current reportID from the route in react navigation state object */
reportIDFromRoute: PropTypes.string,
/** Whether we are viewing below the responsive breakpoint */
isSmallScreenWidth: PropTypes.bool.isRequired,
/** The chat priority mode */
priorityMode: PropTypes.string,
...withLocalizePropTypes,
...withNavigationPropTypes,
};
const defaultProps = {
chatReports: {},
reportActions: {},
personalDetails: {},
currentUserPersonalDetails: {
avatar: '',
},
reportIDFromRoute: '',
priorityMode: CONST.PRIORITY_MODE.DEFAULT,
};
class SidebarLinks extends React.Component {
constructor(props) {
super(props);
this.showSearchPage = this.showSearchPage.bind(this);
this.showSettingsPage = this.showSettingsPage.bind(this);
this.showReportPage = this.showReportPage.bind(this);
if (this.props.isSmallScreenWidth) {
App.confirmReadyToOpenApp();
}
}
componentDidMount() {
App.setSidebarLoaded();
this.isSidebarLoaded = true;
}
showSearchPage() {
if (this.props.isCreateMenuOpen) {
// Prevent opening Search page when click Search icon quickly after clicking FAB icon
return;
}
Navigation.navigate(ROUTES.SEARCH);
}
showSettingsPage() {
if (this.props.isCreateMenuOpen) {
// Prevent opening Settings page when click profile avatar quickly after clicking FAB icon
return;
}
Navigation.navigate(ROUTES.SETTINGS);
}
/**
* Show Report page with selected report id
*
* @param {Object} option
* @param {String} option.reportID
*/
showReportPage(option) {
if (this.props.isCreateMenuOpen) {
// Prevent opening Report page when click LHN row quickly after clicking FAB icon
return;
}
Navigation.navigate(ROUTES.getReportRoute(option.reportID));
this.props.onLinkClick();
}
render() {
const isLoading = _.isEmpty(this.props.personalDetails) || _.isEmpty(this.props.chatReports);
const optionListItems = SidebarUtils.getOrderedReportIDs(this.props.reportIDFromRoute);
const skeletonPlaceholder = <OptionsListSkeletonView shouldAnimate />;
return (
<View
accessibilityElementsHidden={!this.props.isFocused}
accessibilityLabel={this.props.translate('sidebarScreen.listOfChats')}
style={[styles.flex1, styles.h100]}
>
<View
style={[styles.flexRow, styles.ph5, styles.pv3, styles.justifyContentBetween, styles.alignItemsCenter]}
nativeID="drag-area"
>
<Header
title={
<LogoComponent
fill={defaultTheme.textLight}
width={variables.lhnLogoWidth}
height={variables.lhnLogoHeight}
/>
}
accessibilityRole="text"
shouldShowEnvironmentBadge
/>
<Tooltip text={this.props.translate('common.search')}>
<PressableWithoutFeedback
accessibilityLabel={this.props.translate('sidebarScreen.buttonSearch')}
accessibilityRole="button"
style={[styles.flexRow, styles.ph5]}
onPress={Session.checkIfActionIsAllowed(this.showSearchPage)}
>
<Icon src={Expensicons.MagnifyingGlass} />
</PressableWithoutFeedback>
</Tooltip>
<PressableWithoutFeedback
accessibilityLabel={this.props.translate('sidebarScreen.buttonMySettings')}
accessibilityRole="button"
onPress={Session.checkIfActionIsAllowed(this.showSettingsPage)}
>
{Session.isAnonymousUser() ? (
<View style={styles.signInButtonAvatar}>
<Button
medium
success
text={this.props.translate('common.signIn')}
onPress={() => Session.signOutAndRedirectToSignIn()}
/>
</View>
) : (
<OfflineWithFeedback pendingAction={lodashGet(this.props.currentUserPersonalDetails, 'pendingFields.avatar', null)}>
<AvatarWithIndicator
source={UserUtils.getAvatar(this.props.currentUserPersonalDetails.avatar, this.props.currentUserPersonalDetails.login)}
tooltipText={this.props.translate('common.settings')}
/>
</OfflineWithFeedback>
)}
</PressableWithoutFeedback>
</View>
{isLoading ? (
skeletonPlaceholder
) : (
<LHNOptionsList
contentContainerStyles={[styles.sidebarListContainer, {paddingBottom: StyleUtils.getSafeAreaMargins(this.props.insets).marginBottom}]}
data={optionListItems}
focusedIndex={_.findIndex(optionListItems, (option) => option.toString() === this.props.currentReportId)}
onSelectRow={this.showReportPage}
shouldDisableFocusOptions={this.props.isSmallScreenWidth}
optionMode={this.props.priorityMode === CONST.PRIORITY_MODE.GSD ? CONST.OPTION_MODE.COMPACT : CONST.OPTION_MODE.DEFAULT}
/>
)}
</View>
);
}
}
SidebarLinks.propTypes = propTypes;
SidebarLinks.defaultProps = defaultProps;
/**
* 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,
participants: report.participants,
hasDraft: report.hasDraft,
isPinned: report.isPinned,
errorFields: {
addWorkspaceRoom: report.errorFields && report.errorFields.addWorkspaceRoom,
},
lastReadTime: report.lastReadTime,
lastMentionedTime: report.lastMentionedTime,
lastMessageText: report.lastMessageText,
lastVisibleActionCreated: report.lastVisibleActionCreated,
iouReportID: report.iouReportID,
hasOutstandingIOU: report.hasOutstandingIOU,
statusNum: report.statusNum,
stateNum: report.stateNum,
chatType: report.chatType,
policyID: report.policyID,
reportName: report.reportName,
};
/**
* @param {Object} [personalDetails]
* @returns {Object|undefined}
*/
const personalDetailsSelector = (personalDetails) =>
_.reduce(
personalDetails,
(finalPersonalDetails, personalData, login) => {
// It's OK to do param-reassignment in _.reduce() because we absolutely know the starting state of finalPersonalDetails
// eslint-disable-next-line no-param-reassign
finalPersonalDetails[login] = {
login: personalData.login,
displayName: personalData.displayName,
firstName: personalData.firstName,
avatar: UserUtils.getAvatar(personalData.avatar, personalData.login),
};
return finalPersonalDetails;
},
{},
);
/**
* @param {Object} [reportActions]
* @returns {Object|undefined}
*/
const reportActionsSelector = (reportActions) =>
reportActions &&
_.map(reportActions, (reportAction) => ({
errors: reportAction.errors,
}));
/**
* @param {Object} [policy]
* @returns {Object|undefined}
*/
const policySelector = (policy) =>
policy && {
type: policy.type,
name: policy.name,
avatar: policy.avatar,
};
export default compose(
withLocalize,
withCurrentUserPersonalDetails,
withNavigationFocus,
withWindowDimensions,
withCurrentReportId,
withNavigation,
withOnyx({
// Note: It is very important that the keys subscribed to here are the same
// keys that are subscribed to at the top of SidebarUtils.js. If there was a key missing from here and data was updated
// for that key, then there would be no re-render and the options wouldn't reflect the new data because SidebarUtils.getOrderedReportIDs() wouldn't be triggered.
// This could be changed if each OptionRowLHN used withOnyx() to connect to the Onyx keys, but if you had 10,000 reports
// with 10,000 withOnyx() connections, it would have unknown performance implications.
chatReports: {
key: ONYXKEYS.COLLECTION.REPORT,
selector: chatReportSelector,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
selector: personalDetailsSelector,
},
priorityMode: {
key: ONYXKEYS.NVP_PRIORITY_MODE,
},
betas: {
key: ONYXKEYS.BETAS,
},
reportActions: {
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
selector: reportActionsSelector,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
selector: policySelector,
},
preferredLocale: {
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
},
}),
)(SidebarLinks);