/
index.tsx
67 lines (62 loc) · 1.87 KB
/
index.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
import { useCallback, useRef, useState } from 'react';
import { Popover } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { Hooks } from 'wp-js-hooks';
import { PremiumModal } from 'common/premium-modal';
import { Inserter } from '../inserter';
import { Item } from '../inserter/item';
import { storeName } from '../../store';
import { AddStepCallbackType } from '../../../types/filters';
export function InserterPopover(): JSX.Element | null {
const popoverRef = useRef<HTMLDivElement>();
const [showModal, setShowModal] = useState(false);
const { inserterPopover } = useSelect(
(select) => ({
inserterPopover: select(storeName).getInserterPopover(),
}),
[],
);
const { setInserterPopover } = useDispatch(storeName);
const onInsert = useCallback((item: Item) => {
const addStepCallback: AddStepCallbackType = Hooks.applyFilters(
'mailpoet.automation.add_step_callback',
() => {
setShowModal(true);
},
);
addStepCallback(item);
}, []);
if (!inserterPopover) {
return null;
}
return (
<>
<Popover
ref={popoverRef}
anchorRect={inserterPopover.anchor.getBoundingClientRect()}
onClose={() => {
if (!showModal) {
void setInserterPopover(undefined);
}
}}
>
<Inserter onInsert={onInsert} showInserterHelpPanel={false} />
</Popover>
{showModal && (
<PremiumModal
onRequestClose={() => {
setShowModal(false);
popoverRef.current?.focus();
}}
tracking={{
utm_medium: 'upsell_modal',
utm_campaign: 'add_automation_step',
}}
>
{__('You cannot add a new step to the automation.', 'mailpoet')}
</PremiumModal>
)}
</>
);
}