forked from generativefm/generative.fm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable-exporting.js
45 lines (42 loc) · 1.18 KB
/
enable-exporting.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
import setImported from './store/actions/creators/set-imported.creator';
import STATE_STORAGE_KEY from './store/middleware/local-storage.middleware/key';
import store from './store';
const enableExporting = () => {
let openerOrigin;
switch (location.origin) {
case 'https://generative.fm': {
openerOrigin = 'https://play.generative.fm';
break;
}
case 'https://staging.generative.fm': {
openerOrigin = 'https://staging.play.generative.fm';
break;
}
case 'http://localhost:9999': {
openerOrigin = 'http://localhost:8080';
break;
}
default: {
// do nothing
}
}
if (!openerOrigin) {
return;
}
window.addEventListener('message', event => {
const { data, source, origin } = event;
if (origin !== openerOrigin) {
return;
}
if (data.type === 'export-request') {
const state = window.localStorage.getItem(STATE_STORAGE_KEY);
source.postMessage({ type: 'export', state }, origin);
return;
}
if (data.type === 'set-import-request') {
store.dispatch(setImported());
source.postMessage({ type: 'import-set' }, origin);
}
});
};
export default enableExporting;