-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.tsx
213 lines (197 loc) · 6.84 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
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
import classnames from 'classnames';
import { createRoot } from 'react-dom/client';
import { useEffect, useState } from 'react';
import { Button, Icon, SlotFillProvider } from '@wordpress/components';
import { store as noticesStore } from '@wordpress/notices';
import { dispatch, select as globalSelect, useSelect } from '@wordpress/data';
import { getSettings, setSettings } from '@wordpress/date';
import { Platform } from '@wordpress/element';
import { wordpress } from '@wordpress/icons';
import {
store as interfaceStore,
ComplementaryArea,
FullscreenMode,
InterfaceSkeleton,
} from '@wordpress/interface';
import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
import { __, setLocaleData } from '@wordpress/i18n';
import { addQueryArgs } from '@wordpress/url';
import { registerTranslations } from 'common';
import { Header } from './components/header';
import { InserterSidebar } from './components/inserter-sidebar';
import { KeyboardShortcuts } from './components/keyboard-shortcuts';
import { EditorNotices } from './components/notices';
import { Sidebar } from './components/sidebar';
import { Automation } from './components/automation';
import { automationSidebarKey, createStore, storeName } from './store';
import { initializeApi } from '../api';
import { MailPoet } from '../../mailpoet';
import { LISTING_NOTICES } from '../listing/automation-listing-notices';
import { registerApiErrorHandler } from './api-error-handler';
import { ActivatePanel } from './components/panel/activate-panel';
import { AutomationStatus } from '../listing/automation';
import { initializeIntegrations } from './integrations';
// See:
// https://github.com/WordPress/gutenberg/blob/9601a33e30ba41bac98579c8d822af63dd961488/packages/edit-post/src/components/layout/index.js
// https://github.com/WordPress/gutenberg/blob/0ee78b1bbe9c6f3e6df99f3b967132fa12bef77d/packages/edit-site/src/components/editor/index.js
// disable inserter sidebar until we implement drag & drop
const showInserterSidebar = false;
/**
* Show temporary message that active automations cant be updated
*
* see MAILPOET-4744
*/
function updatingActiveAutomationNotPossible() {
const automation = globalSelect(storeName).getAutomationData();
if (
![AutomationStatus.ACTIVE, AutomationStatus.DEACTIVATING].includes(
automation.status,
)
) {
return;
}
if (automation.stats.totals.in_progress === 0) {
return;
}
const { createNotice } = dispatch(noticesStore);
void createNotice(
'success',
__(
'Editing an active automation is temporarily unavailable. We are working on introducing this functionality.',
'mailpoet',
),
{
type: 'snackbar',
},
);
}
function onUnload(event) {
if (globalSelect(storeName).getSavedState() !== 'saved') {
// eslint-disable-next-line no-param-reassign
event.returnValue = __(
'There are unsaved changes that will be lost. Do you want to continue?',
'mailpoet',
);
return event.returnValue;
}
return '';
}
function useConfirmUnsaved() {
useEffect(() => {
window.addEventListener('beforeunload', onUnload);
return () => window.removeEventListener('beforeunload', onUnload);
}, []);
}
function Editor(): JSX.Element {
const {
isFullscreenActive,
isInserterOpened,
isActivationPanelOpened,
isSidebarOpened,
showIconLabels,
automation,
} = useSelect(
(select) => ({
isFullscreenActive: select(storeName).isFeatureActive('fullscreenMode'),
isInserterOpened: select(storeName).isInserterSidebarOpened(),
isSidebarOpened: select(storeName).isSidebarOpened(),
isActivationPanelOpened: select(storeName).isActivationPanelOpened(),
showIconLabels: select(storeName).isFeatureActive('showIconLabels'),
automation: select(storeName).getAutomationData(),
}),
[],
);
const [isBooting, setIsBooting] = useState(true);
useConfirmUnsaved();
useEffect(() => {
if (!isBooting) {
return;
}
if (automation.status === 'trash') {
window.location.href = addQueryArgs(MailPoet.urls.automationListing, {
notice: LISTING_NOTICES.automationHadBeenDeleted,
'notice-args': [automation.name],
});
}
updatingActiveAutomationNotPossible();
setIsBooting(false);
}, [automation.name, automation.status, isBooting]);
if (automation.status === 'trash') {
return null;
}
const className = classnames('interface-interface-skeleton', {
'is-sidebar-opened': isSidebarOpened,
'show-icon-labels': showIconLabels,
});
return (
<ShortcutProvider>
<SlotFillProvider>
<FullscreenMode isActive={isFullscreenActive} />
<KeyboardShortcuts />
<Sidebar />
<InterfaceSkeleton
className={className}
drawer={
isFullscreenActive && (
<div className="edit-site-navigation-toggle">
<Button
className="edit-site-navigation-toggle__button has-icon"
href="admin.php?page=mailpoet-automation"
>
<Icon size={36} icon={wordpress} />
</Button>
</div>
)
}
header={<Header showInserterToggle={showInserterSidebar} />}
content={
<>
<EditorNotices />
<Automation context="edit" />
</>
}
sidebar={<ComplementaryArea.Slot scope={storeName} />}
secondarySidebar={
showInserterSidebar && isInserterOpened ? <InserterSidebar /> : null
}
/>
{isActivationPanelOpened && <ActivatePanel />}
</SlotFillProvider>
</ShortcutProvider>
);
}
window.addEventListener('DOMContentLoaded', () => {
setLocaleData(window.wp.i18n.getLocaleData());
if (window.wp.date.getSettings !== undefined) {
const dateSettings = window.wp as unknown as {
date: { getSettings: typeof getSettings };
};
setSettings(dateSettings.date.getSettings());
} else {
const dateSettings = window.wp as unknown as {
/* eslint-disable no-underscore-dangle */
date: { __experimentalGetSettings: typeof getSettings };
};
setSettings(dateSettings.date.__experimentalGetSettings());
}
createStore();
// Sync default sidebar state to the interface store before starting rendering,
// so that the layout is computed early enough to center the automation scroll.
const sidebarActiveByDefault = Platform.select({
web: true,
native: false,
});
dispatch(interfaceStore).enableComplementaryArea(
storeName,
sidebarActiveByDefault ? automationSidebarKey : undefined,
);
const container = document.getElementById('mailpoet_automation_editor');
if (container) {
registerTranslations();
registerApiErrorHandler();
initializeApi();
initializeIntegrations();
const root = createRoot(container);
root.render(<Editor />);
}
});