-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tsx
188 lines (155 loc) · 4.24 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
import React, { memo, ReactElement } from 'react';
import ReactDOM from 'react-dom';
import { Store } from 'easy-peasy';
import { IntlProvider } from 'react-intl';
import _SRMStore, { ContextStoreModel, LoadMessagesFunction } from './store';
import { IEventEmiter } from './event';
export const SRMStore = _SRMStore;
declare var __webpack_public_path__: string;
let packg: { name: string };
try {
packg = require('webapp__package.json');
} catch (e) {
packg = { name: 'srm_testing' };
}
interface PropsMountSelector {
selector: string;
element?: null;
}
interface PropsMountElement {
selector?: null;
element: HTMLElement;
}
type PropsMount = PropsMountSelector | PropsMountElement;
interface PropsCommon {
sendEvent?: (id: string, ...args: Array<any>) => Promise<any>;
navigate: (
commands: string | Array<string>,
options?: { queryParams?: any }
) => any;
loadMessages?: LoadMessagesFunction;
basename?: string;
publicPath?: string;
language?: string;
event?: any;
eventEmiter?: IEventEmiter,
}
export type PropsSRM<T = void> = PropsMount & PropsCommon & T;
export type PropsApp = { [key: string]: any };
export type RenderFunction<Props extends PropsApp> = (
props: Props
) => ReactElement;
export type SRMFunction<Props extends PropsApp> = (
props: Props
) => any;
export function overrideModel<
StoreModel extends Object = {},
Key extends keyof StoreModel = any
>(
store: Store<StoreModel, any>,
key: Key,
value: StoreModel[Key] | undefined | null
) {
if (value != null) {
store.removeModel(key as any);
store.addModel(key as any, value as any);
}
}
function exportSRM<Props extends PropsApp>(
path: string,
srm: SRMFunction<Props>
) {
const out = path.split('.').reduce((obj, part) => {
if (!obj[part]) {
obj[part] = {};
}
return obj[part];
}, window as any);
Object.assign(out || {}, { render: srm });
}
/*
* TODO: add callback array/EventListener like SRM.onContentRendered
* to customize the rendering of the Content (e.g. add IntlProvider
* and modify returned values).
*/
export function SRM<Props extends PropsApp>(
path: string,
render: RenderFunction<Props & { store: Store<ContextStoreModel, any> }>,
loadMessages?: LoadMessagesFunction,
): SRMFunction<Props & PropsSRM & { store: Store<ContextStoreModel, any> }> {
const srm: SRMFunction<Props & PropsSRM & { store: Store<ContextStoreModel, any> }> = (props) => {
const {
element,
selector,
basename,
language,
event,
sendEvent,
navigate,
eventEmiter,
} = props;
let ret;
const Content = memo(() => {
const store = SRMStore.useStore();
if (props.loadMessages || loadMessages) {
overrideModel(store, 'loadMessages', props.loadMessages || loadMessages);
}
const { setBasename } = store.getActions();
if (basename) {
setBasename(basename);
}
const { setLanguage } = store.getActions();
if (language) {
setLanguage(language);
} else {
setLanguage('en');
}
if (sendEvent) {
overrideModel(store, 'sendEvent', sendEvent);
}
if (navigate) {
overrideModel(store, 'navigate', navigate);
}
if (eventEmiter) {
overrideModel(store, 'eventEmiter', eventEmiter);
}
const { setEvent } = store.getActions();
if (event) {
setEvent(event);
}
ret = {
setBasename,
setLanguage,
setEvent,
};
const { messages, language: locale } = store.getState();
return (
<IntlProvider locale={locale} messages={messages} defaultLocale="en">
{ render(Object.assign({}, props, store)) }
</IntlProvider>
);
});
const el = element || document.querySelector(selector || "#srm");
ReactDOM.render(
<div className={`__${packg.name}`}>
<React.StrictMode>
<SRMStore.Provider>
<Content />
</SRMStore.Provider>
</React.StrictMode>
</div>,
el
);
return ret;
};
exportSRM(path, srm);
return srm;
}
let publicPath = process.env.PUBLIC_URL;
if (!publicPath) {
publicPath = '';
}
if (!publicPath.endsWith('/')) {
publicPath += '/';
}
__webpack_public_path__ = publicPath;