-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathAttachmentPickerWithMenuItems.js
286 lines (256 loc) · 12.1 KB
/
AttachmentPickerWithMenuItems.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
import PropTypes from 'prop-types';
import React, {useMemo} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import AttachmentPicker from '@components/AttachmentPicker';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PopoverMenu from '@components/PopoverMenu';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import Tooltip from '@components/Tooltip/PopoverAnchorTooltip';
import useLocalize from '@hooks/useLocalize';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as Browser from '@libs/Browser';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import useThemeStyles from '@styles/useThemeStyles';
import * as IOU from '@userActions/IOU';
import * as Report from '@userActions/Report';
import * as Task from '@userActions/Task';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
const propTypes = {
/** The report currently being looked at */
report: PropTypes.shape({
/** ID of the report */
reportID: PropTypes.string,
/** Whether or not the report is in the process of being created */
loading: PropTypes.bool,
}).isRequired,
/** The personal details of everyone in the report */
reportParticipantIDs: PropTypes.arrayOf(PropTypes.number),
/** Callback to open the file in the modal */
displayFileInModal: PropTypes.func.isRequired,
/** Whether or not the full size composer is available */
isFullComposerAvailable: PropTypes.bool.isRequired,
/** Whether or not the composer is full size */
isComposerFullSize: PropTypes.bool.isRequired,
/** Updates the isComposerFullSize value */
updateShouldShowSuggestionMenuToFalse: PropTypes.func.isRequired,
/** Whether or not the user is blocked from concierge */
isBlockedFromConcierge: PropTypes.bool.isRequired,
/** Whether or not the attachment picker is disabled */
disabled: PropTypes.bool.isRequired,
/** Sets the menu visibility */
setMenuVisibility: PropTypes.func.isRequired,
/** Whether or not the menu is visible */
isMenuVisible: PropTypes.bool.isRequired,
/** Report ID */
reportID: PropTypes.string.isRequired,
/** Called when opening the attachment picker */
onTriggerAttachmentPicker: PropTypes.func.isRequired,
/** Called when cancelling the attachment picker */
onCanceledAttachmentPicker: PropTypes.func.isRequired,
/** Called when the menu with the items is closed after it was open */
onMenuClosed: PropTypes.func.isRequired,
/** Called when the add action button is pressed */
onAddActionPressed: PropTypes.func.isRequired,
/** Called when the menu item is selected */
onItemSelected: PropTypes.func.isRequired,
/** A ref for the add action button */
actionButtonRef: PropTypes.shape({
// eslint-disable-next-line react/forbid-prop-types
current: PropTypes.object,
}).isRequired,
};
const defaultProps = {
reportParticipantIDs: [],
};
/**
* This includes the popover of options you see when pressing the + button in the composer.
* It also contains the attachment picker, as the menu items need to be able to open it.
*
* @returns {React.Component}
*/
function AttachmentPickerWithMenuItems({
report,
reportParticipantIDs,
displayFileInModal,
isFullComposerAvailable,
isComposerFullSize,
updateShouldShowSuggestionMenuToFalse,
reportID,
isBlockedFromConcierge,
disabled,
setMenuVisibility,
isMenuVisible,
onTriggerAttachmentPicker,
onCanceledAttachmentPicker,
onMenuClosed,
onAddActionPressed,
onItemSelected,
actionButtonRef,
}) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {windowHeight} = useWindowDimensions();
/**
* Returns the list of IOU Options
* @returns {Array<object>}
*/
const moneyRequestOptions = useMemo(() => {
const options = {
[CONST.IOU.TYPE.SPLIT]: {
icon: Expensicons.Receipt,
text: translate('iou.splitBill'),
onSelected: () => Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE.getRoute(CONST.IOU.TYPE.SPLIT, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, report.reportID)),
},
[CONST.IOU.TYPE.REQUEST]: {
icon: Expensicons.MoneyCircle,
text: translate('iou.requestMoney'),
onSelected: () => Navigation.navigate(ROUTES.MONEY_REQUEST_CREATE.getRoute(CONST.IOU.TYPE.REQUEST, CONST.IOU.OPTIMISTIC_TRANSACTION_ID, report.reportID)),
},
[CONST.IOU.TYPE.SEND]: {
icon: Expensicons.Send,
text: translate('iou.sendMoney'),
onSelected: () => IOU.startMoneyRequest(CONST.IOU.TYPE.SEND, report.reportID),
},
};
return _.map(ReportUtils.getMoneyRequestOptions(report, reportParticipantIDs), (option) => ({
...options[option],
}));
}, [report, reportParticipantIDs, translate]);
/**
* Determines if we can show the task option
* @returns {Boolean}
*/
const taskOption = useMemo(() => {
if (!ReportUtils.canCreateTaskInReport(report)) {
return [];
}
return [
{
icon: Expensicons.Task,
text: translate('newTaskPage.assignTask'),
onSelected: () => Task.clearOutTaskInfoAndNavigate(reportID),
},
];
}, [report, reportID, translate]);
const onPopoverMenuClose = () => {
setMenuVisibility(false);
onMenuClosed();
};
return (
<AttachmentPicker>
{({openPicker}) => {
const triggerAttachmentPicker = () => {
onTriggerAttachmentPicker();
openPicker({
onPicked: displayFileInModal,
onCanceled: onCanceledAttachmentPicker,
});
};
const menuItems = [
...moneyRequestOptions,
...taskOption,
{
icon: Expensicons.Paperclip,
text: translate('reportActionCompose.addAttachment'),
onSelected: () => {
if (Browser.isSafari()) {
return;
}
triggerAttachmentPicker();
},
},
];
return (
<>
<View style={[styles.dFlex, styles.flexColumn, isFullComposerAvailable || isComposerFullSize ? styles.justifyContentBetween : styles.justifyContentCenter]}>
{isComposerFullSize && (
<Tooltip text={translate('reportActionCompose.collapse')}>
<PressableWithFeedback
onPress={(e) => {
e.preventDefault();
updateShouldShowSuggestionMenuToFalse();
Report.setIsComposerFullSize(reportID, false);
}}
// Keep focus on the composer when Collapse button is clicked.
onMouseDown={(e) => e.preventDefault()}
style={styles.composerSizeButton}
disabled={isBlockedFromConcierge || disabled}
role={CONST.ROLE.BUTTON}
accessibilityLabel={translate('reportActionCompose.collapse')}
>
<Icon src={Expensicons.Collapse} />
</PressableWithFeedback>
</Tooltip>
)}
{!isComposerFullSize && isFullComposerAvailable && (
<Tooltip text={translate('reportActionCompose.expand')}>
<PressableWithFeedback
onPress={(e) => {
e.preventDefault();
updateShouldShowSuggestionMenuToFalse();
Report.setIsComposerFullSize(reportID, true);
}}
// Keep focus on the composer when Expand button is clicked.
onMouseDown={(e) => e.preventDefault()}
style={styles.composerSizeButton}
disabled={isBlockedFromConcierge || disabled}
role={CONST.ROLE.BUTTON}
accessibilityLabel={translate('reportActionCompose.expand')}
>
<Icon src={Expensicons.Expand} />
</PressableWithFeedback>
</Tooltip>
)}
<Tooltip text={translate('reportActionCompose.addAction')}>
<PressableWithFeedback
ref={actionButtonRef}
onPress={(e) => {
e.preventDefault();
onAddActionPressed();
// Drop focus to avoid blue focus ring.
actionButtonRef.current.blur();
setMenuVisibility(!isMenuVisible);
}}
style={styles.composerSizeButton}
disabled={isBlockedFromConcierge || disabled}
role={CONST.ROLE.BUTTON}
accessibilityLabel={translate('reportActionCompose.addAction')}
>
<Icon src={Expensicons.Plus} />
</PressableWithFeedback>
</Tooltip>
</View>
<PopoverMenu
animationInTiming={CONST.ANIMATION_IN_TIMING}
isVisible={isMenuVisible}
onClose={onPopoverMenuClose}
onItemSelected={(item, index) => {
setMenuVisibility(false);
onItemSelected();
// In order for the file picker to open dynamically, the click
// function must be called from within a event handler that was initiated
// by the user on Safari.
if (index === menuItems.length - 1 && Browser.isSafari()) {
triggerAttachmentPicker();
}
}}
anchorPosition={styles.createMenuPositionReportActionCompose(windowHeight)}
anchorAlignment={{horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT, vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM}}
menuItems={menuItems}
withoutOverlay
anchorRef={actionButtonRef}
/>
</>
);
}}
</AttachmentPicker>
);
}
AttachmentPickerWithMenuItems.propTypes = propTypes;
AttachmentPickerWithMenuItems.defaultProps = defaultProps;
AttachmentPickerWithMenuItems.displayName = 'AttachmentPickerWithMenuItems';
export default AttachmentPickerWithMenuItems;