-
Notifications
You must be signed in to change notification settings - Fork 3k
/
AttachmentPickerWithMenuItems.tsx
345 lines (310 loc) · 15.2 KB
/
AttachmentPickerWithMenuItems.tsx
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import {useIsFocused} from '@react-navigation/native';
import React, {useCallback, useEffect, useMemo} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {FileObject} from '@components/AttachmentModal';
import AttachmentPicker from '@components/AttachmentPicker';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import type {PopoverMenuItem} from '@components/PopoverMenu';
import PopoverMenu from '@components/PopoverMenu';
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback';
import Tooltip from '@components/Tooltip/PopoverAnchorTooltip';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as Browser from '@libs/Browser';
import getIconForAction from '@libs/getIconForAction';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import * as SubscriptionUtils from '@libs/SubscriptionUtils';
import * as IOU from '@userActions/IOU';
import * as Modal from '@userActions/Modal';
import * as Report from '@userActions/Report';
import * as Task from '@userActions/Task';
import type {IOUType} from '@src/CONST';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
type MoneyRequestOptions = Record<Exclude<IOUType, typeof CONST.IOU.TYPE.REQUEST | typeof CONST.IOU.TYPE.SEND>, PopoverMenuItem>;
type AttachmentPickerWithMenuItemsOnyxProps = {
/** The policy tied to the report */
policy: OnyxEntry<OnyxTypes.Policy>;
};
type AttachmentPickerWithMenuItemsProps = AttachmentPickerWithMenuItemsOnyxProps & {
/** The report currently being looked at */
report: OnyxEntry<OnyxTypes.Report>;
/** Callback to open the file in the modal */
displayFileInModal: (url: FileObject) => void;
/** Whether or not the full size composer is available */
isFullComposerAvailable: boolean;
/** Whether or not the composer is full size */
isComposerFullSize: boolean;
/** Whether or not the user is blocked from concierge */
isBlockedFromConcierge: boolean;
/** Whether or not the attachment picker is disabled */
disabled?: boolean;
/** Sets the menu visibility */
setMenuVisibility: (isVisible: boolean) => void;
/** Whether or not the menu is visible */
isMenuVisible: boolean;
/** Report ID */
reportID: string;
/** Called when opening the attachment picker */
onTriggerAttachmentPicker: () => void;
/** Called when cancelling the attachment picker */
onCanceledAttachmentPicker: () => void;
/** Called when the menu with the items is closed after it was open */
onMenuClosed: () => void;
/** Called when the add action button is pressed */
onAddActionPressed: () => void;
/** Called when the menu item is selected */
onItemSelected: () => void;
/** A ref for the add action button */
actionButtonRef: React.RefObject<HTMLDivElement | View>;
/** A function that toggles isScrollLikelyLayoutTriggered flag for a certain period of time */
raiseIsScrollLikelyLayoutTriggered: () => void;
/** The personal details of everyone in the report */
reportParticipantIDs?: number[];
};
/**
* 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.
*/
function AttachmentPickerWithMenuItems({
report,
policy,
reportParticipantIDs,
displayFileInModal,
isFullComposerAvailable,
isComposerFullSize,
reportID,
isBlockedFromConcierge,
disabled,
setMenuVisibility,
isMenuVisible,
onTriggerAttachmentPicker,
onCanceledAttachmentPicker,
onMenuClosed,
onAddActionPressed,
onItemSelected,
actionButtonRef,
raiseIsScrollLikelyLayoutTriggered,
}: AttachmentPickerWithMenuItemsProps) {
const isFocused = useIsFocused();
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
const {windowHeight, windowWidth} = useWindowDimensions();
const {shouldUseNarrowLayout} = useResponsiveLayout();
/**
* Returns the list of IOU Options
*/
const moneyRequestOptions = useMemo(() => {
const selectOption = (onSelected: () => void, shouldRestrictAction: boolean) => {
if (shouldRestrictAction && policy && SubscriptionUtils.shouldRestrictUserBillableActions(policy.id)) {
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
return;
}
onSelected();
};
const options: MoneyRequestOptions = {
[CONST.IOU.TYPE.SPLIT]: {
icon: Expensicons.Transfer,
text: translate('iou.splitExpense'),
onSelected: () => selectOption(() => IOU.startMoneyRequest(CONST.IOU.TYPE.SPLIT, report?.reportID ?? '-1'), true),
},
[CONST.IOU.TYPE.SUBMIT]: {
icon: getIconForAction(CONST.IOU.TYPE.REQUEST),
text: translate('iou.submitExpense'),
onSelected: () => selectOption(() => IOU.startMoneyRequest(CONST.IOU.TYPE.SUBMIT, report?.reportID ?? '-1'), true),
},
[CONST.IOU.TYPE.PAY]: {
icon: getIconForAction(CONST.IOU.TYPE.SEND),
text: translate('iou.paySomeone', {name: ReportUtils.getPayeeName(report)}),
onSelected: () => selectOption(() => IOU.startMoneyRequest(CONST.IOU.TYPE.PAY, report?.reportID ?? '-1'), false),
},
[CONST.IOU.TYPE.TRACK]: {
icon: getIconForAction(CONST.IOU.TYPE.TRACK),
text: translate('iou.trackExpense'),
onSelected: () => selectOption(() => IOU.startMoneyRequest(CONST.IOU.TYPE.TRACK, report?.reportID ?? '-1'), true),
},
[CONST.IOU.TYPE.INVOICE]: {
icon: Expensicons.InvoiceGeneric,
text: translate('workspace.invoices.sendInvoice'),
onSelected: () => selectOption(() => IOU.startMoneyRequest(CONST.IOU.TYPE.INVOICE, report?.reportID ?? '-1'), false),
},
};
return ReportUtils.temporary_getMoneyRequestOptions(report, policy, reportParticipantIDs ?? []).map((option) => ({
...options[option],
}));
}, [translate, report, policy, reportParticipantIDs]);
/**
* Determines if we can show the task option
*/
const taskOption: PopoverMenuItem[] = useMemo(() => {
if (!ReportUtils.canCreateTaskInReport(report)) {
return [];
}
return [
{
icon: Expensicons.Task,
text: translate('newTaskPage.assignTask'),
onSelected: () => Task.clearOutTaskInfoAndNavigate(reportID, report),
},
];
}, [report, reportID, translate]);
const onPopoverMenuClose = () => {
setMenuVisibility(false);
onMenuClosed();
};
const prevIsFocused = usePrevious(isFocused);
/**
* Check if current screen is inactive and previous screen is active.
* Used to close already opened popover menu when any other page is opened over current page.
*
* @return {Boolean}
*/
const didScreenBecomeInactive = useCallback(() => !isFocused && prevIsFocused, [isFocused, prevIsFocused]);
// When the navigation is focused, we want to close the popover menu.
useEffect(() => {
if (!didScreenBecomeInactive() || !isMenuVisible) {
return;
}
setMenuVisibility(false);
}, [didScreenBecomeInactive, isMenuVisible, setMenuVisibility]);
return (
<AttachmentPicker>
{({openPicker}) => {
const triggerAttachmentPicker = () => {
onTriggerAttachmentPicker();
openPicker({
onPicked: displayFileInModal,
onCanceled: onCanceledAttachmentPicker,
});
};
const menuItems = [
...moneyRequestOptions,
...taskOption,
{
icon: Expensicons.Paperclip,
text: translate('reportActionCompose.addAttachment'),
onSelected: () =>
Modal.close(() => {
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();
raiseIsScrollLikelyLayoutTriggered();
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
fill={theme.icon}
src={Expensicons.Collapse}
/>
</PressableWithFeedback>
</Tooltip>
)}
{!isComposerFullSize && isFullComposerAvailable && (
<Tooltip text={translate('reportActionCompose.expand')}>
<PressableWithFeedback
onPress={(e) => {
e?.preventDefault();
raiseIsScrollLikelyLayoutTriggered();
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
fill={theme.icon}
src={Expensicons.Expand}
/>
</PressableWithFeedback>
</Tooltip>
)}
<Tooltip text={translate('common.create')}>
<PressableWithFeedback
ref={actionButtonRef}
onPress={(e) => {
e?.preventDefault();
if (!isFocused) {
return;
}
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('common.create')}
>
<Icon
fill={theme.icon}
src={Expensicons.Plus}
/>
</PressableWithFeedback>
</Tooltip>
</View>
<PopoverMenu
animationInTiming={CONST.ANIMATION_IN_TIMING}
isVisible={isMenuVisible && isFocused}
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(shouldUseNarrowLayout, windowHeight, windowWidth)}
anchorAlignment={{horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT, vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM}}
menuItems={menuItems}
withoutOverlay
anchorRef={actionButtonRef}
/>
</>
);
}}
</AttachmentPicker>
);
}
AttachmentPickerWithMenuItems.displayName = 'AttachmentPickerWithMenuItems';
export default withOnyx<AttachmentPickerWithMenuItemsProps, AttachmentPickerWithMenuItemsOnyxProps>({
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`,
initialValue: {} as OnyxTypes.Policy,
},
})(AttachmentPickerWithMenuItems);