-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathdropdownActions.tsx
99 lines (88 loc) · 2.6 KB
/
dropdownActions.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
import styled from '@emotion/styled';
import {CompactSelect, type SelectOption} from 'sentry/components/core/compactSelect';
import {IconNot} from 'sentry/icons';
import {space} from 'sentry/styles/space';
import {openAdminConfirmModal} from 'admin/components/adminConfirmationModal';
type Props = {
actions: Array<{
key: string;
name: string;
onAction: (params: any) => void;
confirmModalOpts?: any;
disabled?: boolean;
disabledReason?: string;
help?: string;
skipConfirmModal?: boolean;
visible?: boolean;
}>;
label?: string;
};
/**
* Map actions to a format that can be used by the CompactSelect component. This exists
* because this used a component with a different signature, and I
*/
function mapActionsToCompactSelect(
actions: Props['actions']
): Array<SelectOption<string>> {
return actions
.map(action => {
if (action.visible === false) {
return null;
}
return {
value: action.key,
label: (
<div>
{action.name}
{action.disabled && <StyledIconNot data-test-id="disabled-icon" size="xs" />}
</div>
),
details: action.help,
disabled: action.disabled,
tooltip: action.disabled ? action.disabledReason : undefined,
help: action.help,
};
})
.filter(Boolean) as Array<SelectOption<string>>;
}
function DropdownActions({actions, label}: Props) {
return (
<CompactSelect
searchable
options={mapActionsToCompactSelect(actions)}
onChange={option => {
const action = actions.find(a => a.key === option.value);
if (!action || action.disabled) {
return;
}
if (action.skipConfirmModal) {
action.onAction({});
return;
}
const {confirmModalOpts} = action;
const {header, modalSpecificContent, showAuditFields} = confirmModalOpts ?? {};
// We provide some defaults for the openAdminConfirmModal call. But
// those defaults may be overridden
openAdminConfirmModal({
...confirmModalOpts,
header: header ?? <h4>{action.name}</h4>,
modalSpecificContent: modalSpecificContent ?? action.help,
showAuditFields: showAuditFields ?? true,
onConfirm: action.onAction,
});
}}
triggerLabel={label}
triggerProps={
{
'data-test-id': 'detail-actions',
} as any
}
/>
);
}
export default DropdownActions;
const StyledIconNot = styled(IconNot)`
color: ${p => p.theme.red200};
margin-left: ${space(0.5)};
transform: translateY(2px);
`;