-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ContextMenuActions.js
269 lines (252 loc) · 12.2 KB
/
ContextMenuActions.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
import React from 'react';
import _ from 'underscore';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import Str from 'expensify-common/lib/str';
import lodashGet from 'lodash/get';
import * as Expensicons from '../../../../components/Icon/Expensicons';
import * as Report from '../../../../libs/actions/Report';
import * as Download from '../../../../libs/actions/Download';
import Clipboard from '../../../../libs/Clipboard';
import * as ReportUtils from '../../../../libs/ReportUtils';
import * as ReportActionUtils from '../../../../libs/ReportActionsUtils';
import ReportActionComposeFocusManager from '../../../../libs/ReportActionComposeFocusManager';
import {hideContextMenu, showDeleteModal} from './ReportActionContextMenu';
import CONST from '../../../../CONST';
import getAttachmentDetails from '../../../../libs/fileDownload/getAttachmentDetails';
import fileDownload from '../../../../libs/fileDownload';
import addEncryptedAuthTokenToURL from '../../../../libs/addEncryptedAuthTokenToURL';
import * as Environment from '../../../../libs/Environment/Environment';
import Permissions from '../../../../libs/Permissions';
import QuickEmojiReactions from '../../../../components/Reactions/QuickEmojiReactions';
import MiniQuickEmojiReactions from '../../../../components/Reactions/MiniQuickEmojiReactions';
/**
* Gets the HTML version of the message in an action.
* @param {Object} reportAction
* @return {String}
*/
function getActionText(reportAction) {
const message = _.last(lodashGet(reportAction, 'message', null));
return lodashGet(message, 'html', '');
}
const CONTEXT_MENU_TYPES = {
LINK: 'LINK',
REPORT_ACTION: 'REPORT_ACTION',
EMAIL: 'EMAIL',
};
// A list of all the context actions in this menu.
export default [
{
shouldKeepOpen: true,
shouldShow: (type, reportAction) => type === CONTEXT_MENU_TYPES.REPORT_ACTION && _.has(reportAction, 'message'),
renderContent: (closePopover, {reportID, reportAction, close: closeManually, openContextMenu}) => {
const isMini = !closePopover;
const closeContextMenu = (onHideCallback) => {
if (isMini) {
closeManually();
if (onHideCallback) {
onHideCallback();
}
} else {
hideContextMenu(false, onHideCallback);
}
};
const onEmojiSelected = (emoji) => {
Report.toggleEmojiReaction(reportID, reportAction, emoji);
closeContextMenu();
};
if (isMini) {
return (
<MiniQuickEmojiReactions
key="MiniQuickEmojiReactions"
onEmojiSelected={onEmojiSelected}
onPressOpenPicker={openContextMenu}
onEmojiPickerClosed={closeContextMenu}
/>
);
}
return (
<QuickEmojiReactions
key="BaseQuickEmojiReactions"
closeContextMenu={closeContextMenu}
onEmojiSelected={onEmojiSelected}
/>
);
},
},
{
textTranslateKey: 'common.download',
icon: Expensicons.Download,
successTextTranslateKey: 'common.download',
successIcon: Expensicons.Download,
shouldShow: (type, reportAction) => {
const message = _.last(lodashGet(reportAction, 'message', [{}]));
const isAttachment = _.has(reportAction, 'isAttachment') ? reportAction.isAttachment : ReportUtils.isReportMessageAttachment(message);
return isAttachment && reportAction.reportActionID;
},
onPress: (closePopover, {reportAction}) => {
const message = _.last(lodashGet(reportAction, 'message', [{}]));
const html = lodashGet(message, 'html', '');
const attachmentDetails = getAttachmentDetails(html);
const {originalFileName, sourceURL} = attachmentDetails;
const sourceURLWithAuth = addEncryptedAuthTokenToURL(sourceURL);
const sourceID = (sourceURL.match(CONST.REGEX.ATTACHMENT_ID) || [])[1];
Download.setDownload(sourceID, true);
fileDownload(sourceURLWithAuth, originalFileName).then(() => Download.setDownload(sourceID, false));
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.replyInThread',
icon: Expensicons.ChatBubble,
successTextTranslateKey: '',
successIcon: null,
shouldShow: (type, reportAction, isArchivedRoom, betas, anchor, isChronosReport, reportID) =>
Permissions.canUseThreads(betas) &&
type === CONTEXT_MENU_TYPES.REPORT_ACTION &&
reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT &&
!ReportUtils.isThreadFirstChat(reportAction, reportID),
onPress: (closePopover, {reportAction, reportID}) => {
Report.navigateToAndOpenChildReport(lodashGet(reportAction, 'childReportID', '0'), reportAction, reportID);
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.copyURLToClipboard',
icon: Expensicons.Copy,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: (type) => type === CONTEXT_MENU_TYPES.LINK,
onPress: (closePopover, {selection}) => {
Clipboard.setString(selection);
hideContextMenu(true, ReportActionComposeFocusManager.focus);
},
getDescription: (selection) => selection,
},
{
textTranslateKey: 'reportActionContextMenu.copyEmailToClipboard',
icon: Expensicons.Copy,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: (type) => type === CONTEXT_MENU_TYPES.EMAIL,
onPress: (closePopover, {selection}) => {
Clipboard.setString(selection.replace('mailto:', ''));
hideContextMenu(true, ReportActionComposeFocusManager.focus);
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.copyToClipboard',
icon: Expensicons.Copy,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: (type, reportAction) =>
type === CONTEXT_MENU_TYPES.REPORT_ACTION &&
reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU &&
reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.TASKCANCELED &&
reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.TASKCOMPLETED &&
reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.TASKREOPENED &&
!ReportActionUtils.isCreatedTaskReportAction(reportAction) &&
!ReportUtils.isReportMessageAttachment(_.last(lodashGet(reportAction, ['message'], [{}]))),
// If return value is true, we switch the `text` and `icon` on
// `ContextMenuItem` with `successText` and `successIcon` which will fallback to
// the `text` and `icon`
onPress: (closePopover, {reportAction, selection}) => {
const message = _.last(lodashGet(reportAction, 'message', [{}]));
const messageHtml = lodashGet(message, 'html', '');
const isAttachment = _.has(reportAction, 'isAttachment') ? reportAction.isAttachment : ReportUtils.isReportMessageAttachment(message);
if (!isAttachment) {
const content = selection || messageHtml;
if (content) {
const parser = new ExpensiMark();
if (!Clipboard.canSetHtml()) {
Clipboard.setString(parser.htmlToMarkdown(content));
} else {
const plainText = Str.htmlDecode(parser.htmlToText(content));
Clipboard.setHtml(content, plainText);
}
}
} else {
Clipboard.setString(messageHtml);
}
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.copyLink',
icon: Expensicons.LinkCopy,
successIcon: Expensicons.Checkmark,
successTextTranslateKey: 'reportActionContextMenu.copied',
shouldShow: (type, reportAction, isArchivedRoom, betas, menuTarget) => {
const message = _.last(lodashGet(reportAction, 'message', [{}]));
const isAttachment = _.has(reportAction, 'isAttachment') ? reportAction.isAttachment : ReportUtils.isReportMessageAttachment(message);
// Only hide the copylink menu item when context menu is opened over img element.
const isAttachmentTarget = lodashGet(menuTarget, 'tagName') === 'IMG' && isAttachment;
return Permissions.canUseCommentLinking(betas) && type === CONTEXT_MENU_TYPES.REPORT_ACTION && !isAttachmentTarget;
},
onPress: (closePopover, {reportAction, reportID}) => {
Environment.getEnvironmentURL().then((environmentURL) => {
const reportActionID = parseInt(lodashGet(reportAction, 'reportActionID'), 10);
Clipboard.setString(`${environmentURL}/r/${reportID}/${reportActionID}`);
});
hideContextMenu(true, ReportActionComposeFocusManager.focus);
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.markAsUnread',
icon: Expensicons.Mail,
successIcon: Expensicons.Checkmark,
shouldShow: (type) => type === CONTEXT_MENU_TYPES.REPORT_ACTION,
onPress: (closePopover, {reportAction, reportID}) => {
Report.markCommentAsUnread(reportID, reportAction.created);
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.editComment',
icon: Expensicons.Pencil,
shouldShow: (type, reportAction, isArchivedRoom, betas, menuTarget, isChronosReport) =>
type === CONTEXT_MENU_TYPES.REPORT_ACTION && ReportUtils.canEditReportAction(reportAction) && !isArchivedRoom && !isChronosReport,
onPress: (closePopover, {reportID, reportAction, draftMessage}) => {
const editAction = () => Report.saveReportActionDraft(reportID, reportAction.reportActionID, _.isEmpty(draftMessage) ? getActionText(reportAction) : '');
if (closePopover) {
// Hide popover, then call editAction
hideContextMenu(false, editAction);
return;
}
// No popover to hide, call editAction immediately
editAction();
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.deleteAction',
icon: Expensicons.Trashcan,
shouldShow: (type, reportAction, isArchivedRoom, betas, menuTarget, isChronosReport, reportID) =>
// Until deleting parent threads is supported in FE, we will prevent the user from deleting a thread parent
type === CONTEXT_MENU_TYPES.REPORT_ACTION && ReportUtils.canDeleteReportAction(reportAction, reportID) && !isArchivedRoom && !isChronosReport,
onPress: (closePopover, {reportID, reportAction}) => {
if (closePopover) {
// Hide popover, then call showDeleteConfirmModal
hideContextMenu(false, () => showDeleteModal(reportID, reportAction));
return;
}
// No popover to hide, call showDeleteConfirmModal immediately
showDeleteModal(reportID, reportAction);
},
getDescription: () => {},
},
];
export {CONTEXT_MENU_TYPES};