-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SidebarLinks.js
285 lines (250 loc) · 10.9 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
import React from 'react';
import {View, TouchableOpacity} from 'react-native';
import _ from 'underscore';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import styles, {getSafeAreaMargins} from '../../../styles/styles';
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 Header from '../../../components/Header';
import OptionsList from '../../../components/OptionsList';
import {MagnifyingGlass} from '../../../components/Icon/Expensicons';
import AvatarWithIndicator from '../../../components/AvatarWithIndicator';
import {getSidebarOptions, getDefaultAvatar} from '../../../libs/OptionsListUtils';
import KeyboardSpacer from '../../../components/KeyboardSpacer';
import Tooltip from '../../../components/Tooltip';
import CONST from '../../../CONST';
import {participantPropTypes} from './optionPropTypes';
import themeColors from '../../../styles/themes/default';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import * as App from '../../../libs/actions/App';
const propTypes = {
/** Toggles the navigation menu open and closed */
onLinkClick: PropTypes.func.isRequired,
/** Navigates to settings and hides sidebar */
onAvatarClick: PropTypes.func.isRequired,
/** Safe area insets required for mobile devices margins */
insets: SafeAreaInsetPropTypes.isRequired,
/* Onyx Props */
/** List of reports */
reports: PropTypes.objectOf(PropTypes.shape({
reportID: PropTypes.number,
reportName: PropTypes.string,
unreadActionCount: PropTypes.number,
})),
/** List of draft comments. We don't know the shape, since the keys include the report numbers */
draftComments: PropTypes.objectOf(PropTypes.string),
/** List of users' personal details */
personalDetails: PropTypes.objectOf(participantPropTypes),
/** The personal details of the person who is logged in */
myPersonalDetails: PropTypes.shape({
/** Display name of the current user from their personal details */
displayName: PropTypes.string,
/** Avatar URL of the current user from their personal details */
avatar: PropTypes.string,
}),
/** Information about the network */
network: PropTypes.shape({
/** Is the network currently offline or not */
isOffline: PropTypes.bool,
}),
/** Currently viewed reportID */
currentlyViewedReportID: PropTypes.string,
/** Whether we are viewing below the responsive breakpoint */
isSmallScreenWidth: PropTypes.bool.isRequired,
/** The chat priority mode */
priorityMode: PropTypes.string,
/** Whether we have the necessary report data to load the sidebar */
initialReportDataLoaded: PropTypes.bool,
// Whether we are syncing app data
isSyncingData: PropTypes.bool,
...withLocalizePropTypes,
};
const defaultProps = {
reports: {},
draftComments: {},
personalDetails: {},
myPersonalDetails: {
avatar: getDefaultAvatar(),
},
network: null,
currentlyViewedReportID: '',
priorityMode: CONST.PRIORITY_MODE.DEFAULT,
initialReportDataLoaded: false,
isSyncingData: false,
};
class SidebarLinks extends React.Component {
shouldComponentUpdate(nextProps) {
// don't need to limit draft comment flashing for small screen widths
if (nextProps.isSmallScreenWidth) {
return true;
}
const didActiveReportChange = this.props.currentlyViewedReportID !== nextProps.currentlyViewedReportID;
if (didActiveReportChange) {
return true;
}
const previousDraftComments = this.props.draftComments;
const nextDraftComments = nextProps.draftComments;
const previousDraftReports = Object.keys(previousDraftComments);
const nextDraftReports = Object.keys(nextDraftComments);
const reportsWithNewDraftComments = nextDraftReports.filter((report) => {
const isNewDraftComment = !previousDraftReports.includes(report);
const wasNonEmptyDraftComment = previousDraftComments[report] === '';
const hasDraftCommentChanged = previousDraftComments[report] !== nextDraftComments[report];
return isNewDraftComment || (hasDraftCommentChanged && wasNonEmptyDraftComment);
});
const reportsWithRemovedDraftComments = previousDraftReports.filter((report) => {
const isRemovedDraftComment = !nextDraftReports.includes(report);
const isEmptyDraftComment = nextDraftComments[report] === '';
const hasDraftCommentChanged = previousDraftComments[report] !== nextDraftComments[report];
return isRemovedDraftComment || (hasDraftCommentChanged && isEmptyDraftComment);
});
const reportsWithEditedDraftComments = nextDraftReports.filter((report) => {
const didDraftCommentExistPreviously = previousDraftReports.includes(report);
const hasDraftCommentChanged = previousDraftComments[report] !== nextDraftComments[report];
const isNotANewDraftComment = !reportsWithNewDraftComments.includes(report);
const isNotARemovedDraftComment = !reportsWithRemovedDraftComments.includes(report);
return didDraftCommentExistPreviously && hasDraftCommentChanged && isNotANewDraftComment && isNotARemovedDraftComment;
});
const allReportsWithDraftCommentChanges = [
...reportsWithNewDraftComments,
...reportsWithRemovedDraftComments,
...reportsWithEditedDraftComments,
];
const activeReportID = this.props.currentlyViewedReportID;
const reportKey = `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${activeReportID}`;
if (allReportsWithDraftCommentChanges.length === 1 && allReportsWithDraftCommentChanges.includes(reportKey)) {
return false;
}
return true;
}
showSearchPage() {
Navigation.navigate(ROUTES.SEARCH);
}
render() {
// Wait until the reports and personalDetails are actually loaded before displaying the LHN
if (!this.props.initialReportDataLoaded || _.isEmpty(this.props.personalDetails)) {
return null;
}
const activeReportID = parseInt(this.props.currentlyViewedReportID, 10);
const {recentReports} = getSidebarOptions(
this.props.reports,
this.props.personalDetails,
this.props.draftComments,
activeReportID,
this.props.priorityMode,
this.props.betas,
);
const sections = [{
title: '',
indexOffset: 0,
data: recentReports,
shouldShow: true,
}];
return (
<View style={[styles.flex1, styles.h100]}>
<View
style={[
styles.flexRow,
styles.ph5,
styles.pv3,
styles.justifyContentBetween,
styles.alignItemsCenter,
]}
nativeID="drag-area"
>
<Header
textSize="large"
title={this.props.translate('sidebarScreen.headerChat')}
accessibilityLabel={this.props.translate('sidebarScreen.headerChat')}
accessibilityRole="text"
shouldShowEnvironmentBadge
/>
<Tooltip text={this.props.translate('common.search')}>
<TouchableOpacity
accessibilityLabel={this.props.translate('sidebarScreen.buttonSearch')}
accessibilityRole="button"
style={[styles.flexRow, styles.ph5]}
onPress={this.showSearchPage}
>
<Icon src={MagnifyingGlass} />
</TouchableOpacity>
</Tooltip>
<TouchableOpacity
accessibilityLabel={this.props.translate('sidebarScreen.buttonMySettings')}
accessibilityRole="button"
onPress={this.props.onAvatarClick}
>
<AvatarWithIndicator
source={this.props.myPersonalDetails.avatar}
isActive={this.props.network && !this.props.network.isOffline}
isSyncing={this.props.network && !this.props.network.isOffline && this.props.isSyncingData}
/>
</TouchableOpacity>
</View>
<OptionsList
contentContainerStyles={[
styles.sidebarListContainer,
{paddingBottom: getSafeAreaMargins(this.props.insets).marginBottom},
]}
sections={sections}
focusedIndex={_.findIndex(recentReports, (
option => option.reportID === activeReportID
))}
onSelectRow={(option) => {
Navigation.navigate(ROUTES.getReportRoute(option.reportID));
this.props.onLinkClick();
}}
optionBackgroundColor={themeColors.sidebar}
hideSectionHeaders
showTitleTooltip
disableFocusOptions={this.props.isSmallScreenWidth}
optionMode={this.props.priorityMode === CONST.PRIORITY_MODE.GSD ? 'compact' : 'default'}
onLayout={App.setSidebarLoaded}
/>
<KeyboardSpacer />
</View>
);
}
}
SidebarLinks.propTypes = propTypes;
SidebarLinks.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
draftComments: {
key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS,
},
myPersonalDetails: {
key: ONYXKEYS.MY_PERSONAL_DETAILS,
},
network: {
key: ONYXKEYS.NETWORK,
},
currentlyViewedReportID: {
key: ONYXKEYS.CURRENTLY_VIEWED_REPORTID,
},
priorityMode: {
key: ONYXKEYS.NVP_PRIORITY_MODE,
},
initialReportDataLoaded: {
key: ONYXKEYS.INITIAL_REPORT_DATA_LOADED,
},
isSyncingData: {
key: ONYXKEYS.IS_LOADING_AFTER_RECONNECT,
},
betas: {
key: ONYXKEYS.BETAS,
},
}),
)(SidebarLinks);