-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
FloatingActionButtonAndPopover.js
278 lines (257 loc) · 10.1 KB
/
FloatingActionButtonAndPopover.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
import React, {useState, useEffect, useRef, useCallback, useImperativeHandle, forwardRef} from 'react';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import {View} from 'react-native';
import styles from '../../../../styles/styles';
import * as Expensicons from '../../../../components/Icon/Expensicons';
import Navigation from '../../../../libs/Navigation/Navigation';
import ROUTES from '../../../../ROUTES';
import NAVIGATORS from '../../../../NAVIGATORS';
import SCREENS from '../../../../SCREENS';
import Permissions from '../../../../libs/Permissions';
import * as Policy from '../../../../libs/actions/Policy';
import PopoverMenu from '../../../../components/PopoverMenu';
import CONST from '../../../../CONST';
import FloatingActionButton from '../../../../components/FloatingActionButton';
import compose from '../../../../libs/compose';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import withWindowDimensions from '../../../../components/withWindowDimensions';
import ONYXKEYS from '../../../../ONYXKEYS';
import withNavigation from '../../../../components/withNavigation';
import * as Welcome from '../../../../libs/actions/Welcome';
import withNavigationFocus from '../../../../components/withNavigationFocus';
import * as Task from '../../../../libs/actions/Task';
import * as Session from '../../../../libs/actions/Session';
import * as IOU from '../../../../libs/actions/IOU';
import usePrevious from '../../../../hooks/usePrevious';
import * as App from '../../../../libs/actions/App';
/**
* @param {Object} [policy]
* @returns {Object|undefined}
*/
const policySelector = (policy) =>
policy && {
type: policy.type,
role: policy.role,
isPolicyExpenseChatEnabled: policy.isPolicyExpenseChatEnabled,
pendingAction: policy.pendingAction,
};
const propTypes = {
...withLocalizePropTypes,
/* Callback function when the menu is shown */
onShowCreateMenu: PropTypes.func,
/* Callback function before the menu is hidden */
onHideCreateMenu: PropTypes.func,
/** The list of policies the user has access to. */
allPolicies: PropTypes.shape({
/** The policy name */
name: PropTypes.string,
}),
/* Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),
/** Indicated whether the report data is loading */
isLoading: PropTypes.bool,
/** Forwarded ref to FloatingActionButtonAndPopover */
innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
};
const defaultProps = {
onHideCreateMenu: () => {},
onShowCreateMenu: () => {},
allPolicies: {},
betas: [],
isLoading: false,
innerRef: null,
};
/**
* Responsible for rendering the {@link PopoverMenu}, and the accompanying
* FAB that can open or close the menu.
* @param {Object} props
* @returns {JSX.Element}
*/
function FloatingActionButtonAndPopover(props) {
const [isCreateMenuActive, setIsCreateMenuActive] = useState(false);
const isAnonymousUser = Session.isAnonymousUser();
const anchorRef = useRef(null);
const prevIsFocused = usePrevious(props.isFocused);
/**
* Check if LHN status changed from active to inactive.
* Used to close already opened FAB menu when open any other pages (i.e. Press Command + K on web).
*
* @param {Object} prevProps
* @return {Boolean}
*/
const didScreenBecomeInactive = useCallback(
() =>
// When any other page is opened over LHN
!props.isFocused && prevIsFocused,
[props.isFocused, prevIsFocused],
);
/**
* Method called when we click the floating action button
*/
const showCreateMenu = useCallback(
() => {
if (!props.isFocused && props.isSmallScreenWidth) {
return;
}
setIsCreateMenuActive(true);
props.onShowCreateMenu();
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[props.isFocused, props.isSmallScreenWidth],
);
/**
* Method called either when:
* - Pressing the floating action button to open the CreateMenu modal
* - Selecting an item on CreateMenu or closing it by clicking outside of the modal component
*/
const hideCreateMenu = useCallback(
() => {
if (!isCreateMenuActive) {
return;
}
props.onHideCreateMenu();
setIsCreateMenuActive(false);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[isCreateMenuActive],
);
/**
* Checks if user is anonymous. If true, shows the sign in modal, else,
* executes the callback.
*
* @param {Function} callback
*/
const interceptAnonymousUser = (callback) => {
if (isAnonymousUser) {
Session.signOutAndRedirectToSignIn();
} else {
callback();
}
};
useEffect(() => {
const navigationState = props.navigation.getState();
const routes = lodashGet(navigationState, 'routes', []);
const currentRoute = routes[navigationState.index];
if (currentRoute && ![NAVIGATORS.CENTRAL_PANE_NAVIGATOR, SCREENS.HOME].includes(currentRoute.name)) {
return;
}
Welcome.show({routes, showCreateMenu});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
if (!didScreenBecomeInactive()) {
return;
}
// Hide menu manually when other pages are opened using shortcut key
hideCreateMenu();
}, [didScreenBecomeInactive, hideCreateMenu]);
useImperativeHandle(props.innerRef, () => ({
hideCreateMenu() {
hideCreateMenu();
},
}));
return (
<View>
<PopoverMenu
onClose={hideCreateMenu}
isVisible={isCreateMenuActive}
anchorPosition={styles.createMenuPositionSidebar(props.windowHeight)}
onItemSelected={hideCreateMenu}
fromSidebarMediumScreen={!props.isSmallScreenWidth}
menuItems={[
{
icon: Expensicons.ChatBubble,
text: props.translate('sidebarScreen.fabNewChat'),
onSelected: () => interceptAnonymousUser(() => Navigation.navigate(ROUTES.NEW)),
},
...(Permissions.canUseIOUSend(props.betas)
? [
{
icon: Expensicons.Send,
text: props.translate('iou.sendMoney'),
onSelected: () => interceptAnonymousUser(() => IOU.startMoneyRequest(CONST.IOU.MONEY_REQUEST_TYPE.SEND)),
},
]
: []),
{
icon: Expensicons.MoneyCircle,
text: props.translate('iou.requestMoney'),
onSelected: () => interceptAnonymousUser(() => IOU.startMoneyRequest(CONST.IOU.MONEY_REQUEST_TYPE.REQUEST)),
},
{
icon: Expensicons.Heart,
text: props.translate('sidebarScreen.saveTheWorld'),
onSelected: () => interceptAnonymousUser(() => Navigation.navigate(ROUTES.TEACHERS_UNITE)),
},
...(Permissions.canUseTasks(props.betas)
? [
{
icon: Expensicons.Task,
text: props.translate('newTaskPage.assignTask'),
onSelected: () => interceptAnonymousUser(() => Task.clearOutTaskInfoAndNavigate()),
},
]
: []),
...(!props.isLoading && !Policy.hasActiveFreePolicy(props.allPolicies)
? [
{
icon: Expensicons.NewWorkspace,
iconWidth: 46,
iconHeight: 40,
text: props.translate('workspace.new.newWorkspace'),
description: props.translate('workspace.new.getTheExpensifyCardAndMore'),
onSelected: () => interceptAnonymousUser(() => App.createWorkspaceAndNavigateToIt('', false, '', false, !props.isSmallScreenWidth)),
},
]
: []),
]}
withoutOverlay
anchorRef={anchorRef}
/>
<FloatingActionButton
accessibilityLabel={props.translate('sidebarScreen.fabNewChatExplained')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.BUTTON}
isActive={isCreateMenuActive}
ref={anchorRef}
onPress={() => {
if (isCreateMenuActive) {
hideCreateMenu();
} else {
showCreateMenu();
}
}}
/>
</View>
);
}
FloatingActionButtonAndPopover.propTypes = propTypes;
FloatingActionButtonAndPopover.defaultProps = defaultProps;
FloatingActionButtonAndPopover.displayName = 'FloatingActionButtonAndPopover';
export default compose(
withLocalize,
withNavigation,
withNavigationFocus,
withWindowDimensions,
withOnyx({
allPolicies: {
key: ONYXKEYS.COLLECTION.POLICY,
selector: policySelector,
},
betas: {
key: ONYXKEYS.BETAS,
},
isLoading: {
key: ONYXKEYS.IS_LOADING_REPORT_DATA,
},
}),
)(
forwardRef((props, ref) => (
<FloatingActionButtonAndPopover
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={ref}
/>
)),
);