-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBaseReportActionContextMenu.js
executable file
·203 lines (179 loc) · 8.44 KB
/
BaseReportActionContextMenu.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
import React, {useRef, useMemo, useState, memo} from 'react';
import {InteractionManager, View} from 'react-native';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import getReportActionContextMenuStyles from '../../../../styles/getReportActionContextMenuStyles';
import ContextMenuItem from '../../../../components/ContextMenuItem';
import {propTypes as genericReportActionContextMenuPropTypes, defaultProps as GenericReportActionContextMenuDefaultProps} from './genericReportActionContextMenuPropTypes';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import ContextMenuActions, {CONTEXT_MENU_TYPES} from './ContextMenuActions';
import compose from '../../../../libs/compose';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../../components/withWindowDimensions';
import {withBetas} from '../../../../components/OnyxProvider';
import * as Session from '../../../../libs/actions/Session';
import {hideContextMenu} from './ReportActionContextMenu';
import ONYXKEYS from '../../../../ONYXKEYS';
import CONST from '../../../../CONST';
import useArrowKeyFocusManager from '../../../../hooks/useArrowKeyFocusManager';
import useKeyboardShortcut from '../../../../hooks/useKeyboardShortcut';
import useNetwork from '../../../../hooks/useNetwork';
const propTypes = {
/** String representing the context menu type [LINK, REPORT_ACTION] which controls context menu choices */
type: PropTypes.string,
/** Target node which is the target of ContentMenu */
anchor: PropTypes.oneOfType([PropTypes.node, PropTypes.object]),
/** Flag to check if the chat participant is Chronos */
isChronosReport: PropTypes.bool,
/** Whether the provided report is an archived room */
isArchivedRoom: PropTypes.bool,
contentRef: PropTypes.oneOfType([PropTypes.node, PropTypes.object, PropTypes.func]),
...genericReportActionContextMenuPropTypes,
...withLocalizePropTypes,
...windowDimensionsPropTypes,
};
const defaultProps = {
type: CONTEXT_MENU_TYPES.REPORT_ACTION,
anchor: null,
contentRef: null,
isChronosReport: false,
isArchivedRoom: false,
...GenericReportActionContextMenuDefaultProps,
};
function BaseReportActionContextMenu(props) {
const menuItemRefs = useRef({});
const [shouldKeepOpen, setShouldKeepOpen] = useState(false);
const wrapperStyle = getReportActionContextMenuStyles(props.isMini, props.isSmallScreenWidth);
const {isOffline} = useNetwork();
const reportAction = useMemo(() => {
if (_.isEmpty(props.reportActions) || props.reportActionID === '0') {
return {};
}
return props.reportActions[props.reportActionID] || {};
}, [props.reportActions, props.reportActionID]);
const shouldShowFilter = (contextAction) =>
contextAction.shouldShow(
props.type,
reportAction,
props.isArchivedRoom,
props.betas,
props.anchor,
props.isChronosReport,
props.reportID,
props.isPinnedChat,
props.isUnreadChat,
isOffline,
);
const shouldEnableArrowNavigation = !props.isMini && (props.isVisible || shouldKeepOpen);
const filteredContextMenuActions = _.filter(ContextMenuActions, shouldShowFilter);
// Context menu actions that are not rendered as menu items are excluded from arrow navigation
const nonMenuItemActionIndexes = _.map(filteredContextMenuActions, (contextAction, index) => (_.isFunction(contextAction.renderContent) ? index : undefined));
const disabledIndexes = _.filter(nonMenuItemActionIndexes, (index) => !_.isUndefined(index));
const [focusedIndex, setFocusedIndex] = useArrowKeyFocusManager({
initialFocusedIndex: -1,
disabledIndexes,
maxIndex: filteredContextMenuActions.length - 1,
isActive: shouldEnableArrowNavigation,
});
/**
* Checks if user is anonymous. If true and the action doesn't accept for anonymous user, hides the context menu and
* shows the sign in modal. Else, executes the callback.
*
* @param {Function} callback
* @param {Boolean} isAnonymousAction
*/
const interceptAnonymousUser = (callback, isAnonymousAction = false) => {
if (Session.isAnonymousUser() && !isAnonymousAction) {
hideContextMenu(false);
InteractionManager.runAfterInteractions(() => {
Session.signOutAndRedirectToSignIn();
});
} else {
callback();
}
};
useKeyboardShortcut(
CONST.KEYBOARD_SHORTCUTS.ENTER,
(event) => {
if (!menuItemRefs.current[focusedIndex]) {
return;
}
// Ensures the event does not cause side-effects beyond the context menu, e.g. when an outside element is focused
if (event) {
event.stopPropagation();
}
menuItemRefs.current[focusedIndex].triggerPressAndUpdateSuccess();
setFocusedIndex(-1);
},
{isActive: shouldEnableArrowNavigation},
);
return (
(props.isVisible || shouldKeepOpen) && (
<View
ref={props.contentRef}
style={wrapperStyle}
>
{_.map(filteredContextMenuActions, (contextAction, index) => {
const closePopup = !props.isMini;
const payload = {
reportAction,
reportID: props.reportID,
draftMessage: props.draftMessage,
selection: props.selection,
close: () => setShouldKeepOpen(false),
openContextMenu: () => setShouldKeepOpen(true),
interceptAnonymousUser,
};
if (contextAction.renderContent) {
// make sure that renderContent isn't mixed with unsupported props
if (__DEV__ && (contextAction.text != null || contextAction.icon != null)) {
throw new Error('Dev error: renderContent() and text/icon cannot be used together.');
}
return contextAction.renderContent(closePopup, payload);
}
return (
<ContextMenuItem
ref={(ref) => {
menuItemRefs.current[index] = ref;
}}
icon={contextAction.icon}
text={props.translate(contextAction.textTranslateKey, {action: reportAction})}
successIcon={contextAction.successIcon}
successText={contextAction.successTextTranslateKey ? props.translate(contextAction.successTextTranslateKey) : undefined}
isMini={props.isMini}
key={contextAction.textTranslateKey}
onPress={() => interceptAnonymousUser(() => contextAction.onPress(closePopup, payload), contextAction.isAnonymousAction)}
description={contextAction.getDescription(props.selection, props.isSmallScreenWidth)}
isAnonymousAction={contextAction.isAnonymousAction}
isFocused={focusedIndex === index}
/>
);
})}
</View>
)
);
}
BaseReportActionContextMenu.propTypes = propTypes;
BaseReportActionContextMenu.defaultProps = defaultProps;
export default compose(
withLocalize,
withBetas(),
withWindowDimensions,
withOnyx({
reportActions: {
key: ({originalReportID}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
canEvict: false,
},
}),
)(
memo(BaseReportActionContextMenu, (prevProps, nextProps) => {
const prevReportAction = lodashGet(prevProps.reportActions, prevProps.reportActionID, '');
const nextReportAction = lodashGet(nextProps.reportActions, nextProps.reportActionID, '');
// We only want to re-render when the report action that is attached to is changed
if (prevReportAction !== nextReportAction) {
return false;
}
return _.isEqual(_.omit(prevProps, 'reportActions'), _.omit(nextProps, 'reportActions'));
}),
);