-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
SidebarLinks.js
270 lines (241 loc) · 10.7 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
/* 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 withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import * as App from '../../../libs/actions/App';
import withCurrentUserPersonalDetails from '../../../components/withCurrentUserPersonalDetails';
import withWindowDimensions from '../../../components/withWindowDimensions';
import LHNOptionsList from '../../../components/LHNOptionsList/LHNOptionsList';
import SidebarUtils from '../../../libs/SidebarUtils';
import OfflineWithFeedback from '../../../components/OfflineWithFeedback';
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';
import KeyboardShortcut from '../../../libs/KeyboardShortcut';
import onyxSubscribe from '../../../libs/onyxSubscribe';
import personalDetailsPropType from '../../personalDetailsPropType';
import * as ReportActionContextMenu from '../report/ContextMenu/ReportActionContextMenu';
import withCurrentReportID from '../../../components/withCurrentReportID';
import OptionRowLHNData from '../../../components/LHNOptionsList/OptionRowLHNData';
const basePropTypes = {
/** Toggles the navigation menu open and closed */
onLinkClick: PropTypes.func.isRequired,
/** Safe area insets required for mobile devices margins */
insets: safeAreaInsetPropTypes.isRequired,
/** Whether we are viewing below the responsive breakpoint */
isSmallScreenWidth: PropTypes.bool.isRequired,
};
const propTypes = {
...basePropTypes,
optionListItems: PropTypes.arrayOf(PropTypes.string).isRequired,
isLoading: PropTypes.bool.isRequired,
currentUserPersonalDetails: personalDetailsPropType,
priorityMode: PropTypes.oneOf(_.values(CONST.PRIORITY_MODE)),
/** The top most report id */
currentReportID: PropTypes.string,
/* Onyx Props */
report: PropTypes.shape({
/** reportID (only present when there is a matching report) */
reportID: PropTypes.string,
}),
...withLocalizePropTypes,
};
const defaultProps = {
currentUserPersonalDetails: {
avatar: '',
},
priorityMode: CONST.PRIORITY_MODE.DEFAULT,
currentReportID: '',
report: {},
};
class SidebarLinks extends React.PureComponent {
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();
SidebarUtils.setIsSidebarLoadedReady();
this.isSidebarLoaded = true;
let modal = {};
this.unsubscribeOnyxModal = onyxSubscribe({
key: ONYXKEYS.MODAL,
callback: (modalArg) => {
modal = modalArg;
},
});
const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ESCAPE;
this.unsubscribeEscapeKey = KeyboardShortcut.subscribe(
shortcutConfig.shortcutKey,
() => {
if (modal.willAlertModalBecomeVisible) {
return;
}
Navigation.dismissModal();
},
shortcutConfig.descriptionKey,
shortcutConfig.modifiers,
true,
true,
);
ReportActionContextMenu.hideContextMenu(false);
}
componentWillUnmount() {
SidebarUtils.resetIsSidebarLoadedReadyPromise();
if (this.unsubscribeEscapeKey) {
this.unsubscribeEscapeKey();
}
if (this.unsubscribeOnyxModal) {
this.unsubscribeOnyxModal();
}
}
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) {
// Prevent opening Report page when clicking LHN row quickly after clicking FAB icon
// or when clicking the active LHN row
// or when continuously clicking different LHNs, only apply to small screen
// since getTopmostReportId always returns on other devices
if (this.props.isCreateMenuOpen || this.props.currentReportID === option.reportID || (this.props.isSmallScreenWidth && Navigation.getTopmostReportId())) {
return;
}
Navigation.navigate(ROUTES.getReportRoute(option.reportID));
this.props.onLinkClick();
}
render() {
const viewMode = this.props.priorityMode === CONST.PRIORITY_MODE.GSD ? CONST.OPTION_MODE.COMPACT : CONST.OPTION_MODE.DEFAULT;
return (
<View 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={CONST.ACCESSIBILITY_ROLE.TEXT}
shouldShowEnvironmentBadge
/>
<Tooltip text={this.props.translate('common.search')}>
<PressableWithoutFeedback
accessibilityLabel={this.props.translate('sidebarScreen.buttonSearch')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.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={CONST.ACCESSIBILITY_ROLE.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.accountID)}
tooltipText={this.props.translate('common.settings')}
/>
</OfflineWithFeedback>
)}
</PressableWithoutFeedback>
</View>
{this.props.isLoading ? (
<>
{lodashGet(this.props.report, 'reportID') && (
<OptionRowLHNData
reportID={this.props.currentReportID}
viewMode={viewMode}
shouldDisableFocusOptions={this.props.isSmallScreenWidth}
onSelectRow={this.showReportPage}
/>
)}
<OptionsListSkeletonView shouldAnimate />
</>
) : (
<LHNOptionsList
contentContainerStyles={[styles.sidebarListContainer, {paddingBottom: StyleUtils.getSafeAreaMargins(this.props.insets).marginBottom}]}
data={this.props.optionListItems}
onSelectRow={this.showReportPage}
shouldDisableFocusOptions={this.props.isSmallScreenWidth}
optionMode={viewMode}
/>
)}
</View>
);
}
}
SidebarLinks.propTypes = propTypes;
SidebarLinks.defaultProps = defaultProps;
export default compose(
withLocalize,
withCurrentUserPersonalDetails,
withWindowDimensions,
withCurrentReportID,
withOnyx({
report: {
key: ({currentReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${currentReportID}`,
},
}),
)(SidebarLinks);
export {basePropTypes};