-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ContextMenuActions.js
138 lines (127 loc) · 4.98 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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import Str from 'expensify-common/lib/str';
import {
Clipboard as ClipboardIcon, LinkCopy, Mail, Pencil, Trashcan, Checkmark,
} from '../../../../components/Icon/Expensicons';
import {
setNewMarkerPosition, updateLastReadActionID, saveReportActionDraft,
} from '../../../../libs/actions/Report';
import Clipboard from '../../../../libs/Clipboard';
import {isReportMessageAttachment, canEditReportAction, canDeleteReportAction} from '../../../../libs/reportUtils';
import ReportActionComposeFocusManager from '../../../../libs/ReportActionComposeFocusManager';
import {hideContextMenu, showDeleteModal} from './ReportActionContextMenu';
/**
* 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',
};
// A list of all the context actions in this menu.
export default [
{
textTranslateKey: 'reportActionContextMenu.copyURLToClipboard',
icon: ClipboardIcon,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.LINK,
onPress: (closePopover, {selection}) => {
Clipboard.setString(selection);
hideContextMenu(true, ReportActionComposeFocusManager.focus);
},
},
{
textTranslateKey: 'reportActionContextMenu.copyToClipboard',
icon: ClipboardIcon,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.REPORT_ACTION,
// 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', null));
const html = lodashGet(message, 'html', '');
const text = Str.htmlDecode(selection || lodashGet(message, 'text', ''));
const isAttachment = _.has(reportAction, 'isAttachment')
? reportAction.isAttachment
: isReportMessageAttachment(text);
if (!isAttachment) {
Clipboard.setString(text);
} else {
Clipboard.setString(html);
}
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
},
{
textTranslateKey: 'reportActionContextMenu.copyLink',
icon: LinkCopy,
shouldShow: () => false,
onPress: () => {},
},
{
textTranslateKey: 'reportActionContextMenu.markAsUnread',
icon: Mail,
successIcon: Checkmark,
shouldShow: type => type === CONTEXT_MENU_TYPES.REPORT_ACTION,
onPress: (closePopover, {reportAction, reportID}) => {
updateLastReadActionID(reportID, reportAction.sequenceNumber);
setNewMarkerPosition(reportID, reportAction.sequenceNumber);
if (closePopover) {
hideContextMenu(true, ReportActionComposeFocusManager.focus);
}
},
},
{
textTranslateKey: 'reportActionContextMenu.editComment',
icon: Pencil,
shouldShow: (type, reportAction) => (
type === CONTEXT_MENU_TYPES.REPORT_ACTION && canEditReportAction(reportAction)
),
onPress: (closePopover, {reportID, reportAction, draftMessage}) => {
const editAction = () => 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();
},
},
{
textTranslateKey: 'reportActionContextMenu.deleteComment',
icon: Trashcan,
shouldShow: (type, reportAction) => type === CONTEXT_MENU_TYPES.REPORT_ACTION
&& 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);
},
},
];
export {
CONTEXT_MENU_TYPES,
};