-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ContextMenuActions.js
188 lines (174 loc) · 7.41 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
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import * as Expensicons from '../../../../components/Icon/Expensicons';
import * as Report from '../../../../libs/actions/Report';
import Clipboard from '../../../../libs/Clipboard';
import * as ReportUtils from '../../../../libs/ReportUtils';
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 ContextMenuUtils from './ContextMenuUtils';
/**
* 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 [
{
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} = attachmentDetails;
let {sourceURL} = attachmentDetails;
sourceURL = addEncryptedAuthTokenToURL(sourceURL);
fileDownload(sourceURL, originalFileName);
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.copyURLToClipboard',
icon: Expensicons.Clipboard,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.LINK,
onPress: (closePopover, {selection}) => {
Clipboard.setString(selection);
hideContextMenu(true, ReportActionComposeFocusManager.focus);
},
getDescription: ContextMenuUtils.getPopoverDescription,
},
{
textTranslateKey: 'reportActionContextMenu.copyEmailToClipboard',
icon: Expensicons.Clipboard,
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.Clipboard,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: (type, reportAction) => (type === CONTEXT_MENU_TYPES.REPORT_ACTION
&& reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU
&& !ReportUtils.isReportMessageAttachment(lodashGet(reportAction, ['message', 0, 'text'], ''))),
// 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 html = lodashGet(message, 'html', '');
const parser = new ExpensiMark();
const reportMarkdown = parser.htmlToMarkdown(html);
const text = selection || reportMarkdown;
const isAttachment = _.has(reportAction, 'isAttachment')
? reportAction.isAttachment
: ReportUtils.isReportMessageAttachment(message);
if (!isAttachment) {
Clipboard.setString(text);
} else {
Clipboard.setString(html);
}
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.copyLink',
icon: Expensicons.LinkCopy,
shouldShow: () => false,
onPress: () => {},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.markAsUnread',
icon: Expensicons.Mail,
successIcon: Expensicons.Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.REPORT_ACTION,
onPress: (closePopover, {reportAction, reportID}) => {
Report.updateLastReadActionID(reportID, reportAction.sequenceNumber, true);
Report.setNewMarkerPosition(reportID, reportAction.sequenceNumber);
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
getDescription: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.editComment',
icon: Expensicons.Pencil,
shouldShow: (type, reportAction) => (
type === CONTEXT_MENU_TYPES.REPORT_ACTION && ReportUtils.canEditReportAction(reportAction)
),
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.deleteComment',
icon: Expensicons.Trashcan,
shouldShow: (type, reportAction) => type === CONTEXT_MENU_TYPES.REPORT_ACTION
&& ReportUtils.canDeleteReportAction(reportAction),
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,
};