-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublicDashboard.tsx
189 lines (156 loc) · 5.49 KB
/
PublicDashboard.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
/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/ban-ts-comment */
import { timezoneActions } from './modules/timezone';
if (process.env.NODE_ENV === 'production') {
// @ts-ignore
__webpack_public_path__ = window.dashboardCreatorResourcesBasePath;
}
import React from 'react';
import ReactDOM from 'react-dom';
import i18n from 'i18next';
import { Provider } from 'react-redux';
import {
ConnectedRouter,
connectRouter,
routerMiddleware,
} from 'connected-react-router';
import KeenAnalysis from 'keen-analysis';
import { ThemeProvider } from 'styled-components';
import { configureStore, combineReducers } from '@reduxjs/toolkit';
import { createMemoryHistory } from 'history';
import { PubSub } from '@keen.io/pubsub';
import { ToastProvider } from '@keen.io/toast-notifications';
import { screenBreakpoints } from '@keen.io/ui-core';
import { APIContext, AppContext } from './contexts';
import { DashboardAPI } from './api';
import { NotificationManager } from './modules/notifications';
import { dashboardsActions } from './modules/dashboards';
import PublicDashboardViewer from './components/PublicDashboardViewer';
import GlobalStyles from './components/GlobalStyles';
import createI18n from './i18n';
import createSagaMiddleware from './createSagaMiddleware';
import rootReducer from './rootReducer';
import { createRootSaga } from './rootSaga';
import { SHOW_TOAST_NOTIFICATION_EVENT } from './constants';
import { PublicDashboardOptions, TranslationsSettings } from './types';
export class PublicDashboard {
/** Container used to mount application */
private container: string;
/** Container used to mount application modals */
private modalContainer: string;
/** Project identifer */
private readonly projectId: string;
/** Dashboard identifer */
private readonly dashboardId: string;
/** User key for Keen project */
private readonly accessKey: string;
/** Dashboards API url */
private dashboardsApiUrl = 'dashboard-service.k-n.io';
/** Analytics API url */
private analyticsApiUrl = 'api.keen.io';
/** App localization settings */
private readonly translationsSettings: TranslationsSettings;
/** Widgets configuration **/
private widgetsConfiguration = {};
/** Timezones API url */
private timezonesApiUrl = 'api.keen.io';
constructor(config: PublicDashboardOptions) {
const {
container,
modalContainer,
project,
dashboardId,
backend,
translations,
widgetsConfiguration,
timezonesHost,
} = config;
if (backend?.analyticsApiUrl)
this.analyticsApiUrl = backend.analyticsApiUrl;
if (backend?.dashboardsApiUrl)
this.dashboardsApiUrl = backend.dashboardsApiUrl;
const { id: projectId, accessKey } = project;
this.container = container;
this.modalContainer = modalContainer;
this.projectId = projectId;
this.dashboardId = dashboardId;
this.accessKey = accessKey;
this.translationsSettings = translations || {};
this.widgetsConfiguration = widgetsConfiguration;
if (timezonesHost) {
this.timezonesApiUrl = timezonesHost;
}
}
render() {
const dashboardApi = new DashboardAPI({
projectId: this.projectId,
accessKey: this.accessKey,
url: this.dashboardsApiUrl,
});
const keenAnalysis = new KeenAnalysis({
projectId: this.projectId,
readKey: this.accessKey,
host: this.analyticsApiUrl,
});
createI18n(this.translationsSettings);
const notificationPubSub = new PubSub();
const sagaMiddleware = createSagaMiddleware({
dashboardApi,
keenAnalysis,
i18n,
analyticsApiHost: this.analyticsApiUrl,
timezonesApiHost: this.timezonesApiUrl,
notificationManager: new NotificationManager({
pubsub: notificationPubSub,
eventName: SHOW_TOAST_NOTIFICATION_EVENT,
}),
});
const history = createMemoryHistory();
const store = configureStore({
reducer: combineReducers({
...rootReducer,
router: connectRouter(history),
}),
middleware: [sagaMiddleware, routerMiddleware(history)],
});
const rootSaga = createRootSaga();
const projectSettings = {
id: this.projectId,
userKey: this.accessKey,
masterKey: '',
};
sagaMiddleware.run(rootSaga);
store.dispatch(timezoneActions.fetchTimezones());
store.dispatch(dashboardsActions.viewPublicDashboard(this.dashboardId));
ReactDOM.render(
<Provider store={store}>
<GlobalStyles modalContainer={this.modalContainer} />
<ThemeProvider
theme={{
breakpoints: screenBreakpoints,
}}
>
<ConnectedRouter history={history}>
<ToastProvider>
<AppContext.Provider
value={{
notificationPubSub,
project: projectSettings,
analyticsApiUrl: this.analyticsApiUrl,
timezonesApiUrl: this.timezonesApiUrl,
modalContainer: this.modalContainer,
widgetsConfiguration: this.widgetsConfiguration,
}}
>
<APIContext.Provider value={{ dashboardApi, keenAnalysis }}>
<PublicDashboardViewer dashboardId={this.dashboardId} />
</APIContext.Provider>
</AppContext.Provider>
</ToastProvider>
</ConnectedRouter>
</ThemeProvider>
</Provider>,
document.querySelector(this.container)
);
}
}
export default PublicDashboard;