forked from cBioPortal/cbioportal-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappBootstrapper.tsx
executable file
·387 lines (325 loc) · 11.4 KB
/
appBootstrapper.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import React from 'react';
import ReactDOM from 'react-dom';
import { configure, toJS } from 'mobx';
import { Provider } from 'mobx-react';
import { Router } from 'react-router-dom';
import { createBrowserHistory } from 'history';
import { syncHistoryWithStore } from 'mobx-react-router';
import ExtendedRoutingStore from './shared/lib/ExtendedRouterStore';
import { datadogLogs } from '@datadog/browser-logs';
import {
fetchServerConfig,
getLoadConfig,
getServerConfig,
initializeAPIClients,
initializeAppStore,
initializeLoadConfiguration,
initializeServerConfiguration,
} from './config/config';
import './shared/lib/ajaxQuiet';
import _ from 'lodash';
import $ from 'jquery';
import * as superagent from 'superagent';
import { buildCBioPortalPageUrl } from './shared/api/urls';
import browser from 'bowser';
import { setNetworkListener } from './shared/lib/ajaxQuiet';
import { initializeTracking, sendToLoggly } from 'shared/lib/tracking';
import superagentCache from 'superagent-cache';
import {
getBrowserWindow,
hashString,
isWebdriver,
onMobxPromise,
} from 'cbioportal-frontend-commons';
import { AppStore } from './AppStore';
import { handleLongUrls } from 'shared/lib/handleLongUrls';
import 'shared/polyfill/canvasToBlob';
import { setCurrentURLHeader } from 'shared/lib/extraHeader';
import Container from 'appShell/App/Container';
import { IServerConfig } from 'config/IAppConfig';
import { initializeGenericAssayServerConfig } from 'shared/lib/GenericAssayUtils/GenericAssayConfig';
import { FeatureFlagStore } from 'shared/FeatureFlagStore';
import eventBus from 'shared/events/eventBus';
import { SiteError } from 'shared/model/appMisc';
import load from 'little-loader';
import internalClient from 'shared/api/cbioportalInternalClientInstance';
export interface ICBioWindow {
globalStores: {
routing: ExtendedRoutingStore;
appStore: AppStore;
};
routingStore: ExtendedRoutingStore;
$: JQueryStatic;
jQuery: JQueryStatic;
e2etest: boolean;
FRONTEND_VERSION: string;
FRONTEND_COMMIT: string;
rawServerConfig: IServerConfig;
postLoadForMskCIS: () => void;
isMSKCIS: boolean;
}
const browserWindow: ICBioWindow = window as any;
superagentCache(superagent);
configure({
enforceActions: 'never',
//disableErrorBoundaries: true
});
/*enableLogging({
action: true,
reaction: true,
transaction: true,
compute: true
});*/
// this must occur before we initialize tracking
// it fixes the hash portion of url when cohort patient list is too long
handleLongUrls();
// YOU MUST RUN THESE initialize and then set the public path after
initializeLoadConfiguration();
// THIS TELLS WEBPACK BUNDLE LOADER WHERE TO LOAD SPLIT BUNDLES
//@ts-ignore
__webpack_public_path__ = getLoadConfig().frontendUrl;
if (!browserWindow.hasOwnProperty('$')) {
browserWindow.$ = $;
}
if (!browserWindow.hasOwnProperty('jQuery')) {
browserWindow.jQuery = $;
}
// write browser name, version to body tag
if (browser) {
$(document).ready(() => {
$('body').addClass(browser.name);
});
}
// e2e test specific stuff
if (getBrowserWindow().navigator.webdriver) {
$(document).ready(() => {
$('body').addClass('e2etest');
browserWindow.e2etest = true;
});
}
// if we are running e2e OR we are testing performance improvements manually
if (getBrowserWindow().navigator.webdriver || localStorage.recordAjaxQuiet) {
setNetworkListener();
}
if (localStorage.getItem('timeElementVisible')) {
const interval = setInterval(() => {
const elementIsVisible = $(
localStorage.getItem('timeElementVisible')!
).is(':visible');
if (elementIsVisible) {
clearInterval(interval);
console.log(
`TimeElementVisible for selector "${localStorage.timeElementVisible}"`,
performance.now()
);
}
}, 1000);
}
// for cbioportal instances, add an extra custom HTTP header to
// aid debugging in Sentry
if (/cbioportal\.org/.test(getBrowserWindow().location.href)) {
setCurrentURLHeader();
}
// expose version on window
//@ts-ignore
browserWindow.FRONTEND_VERSION = VERSION;
//@ts-ignore
browserWindow.FRONTEND_COMMIT = COMMIT;
// this is a NOOP to fix CIS issue
browserWindow.postLoadForMskCIS = () => {};
// this is the only supported way to disable tracking for the $3Dmol.js
(browserWindow as any).$3Dmol = { notrack: true };
// expose lodash on window
getBrowserWindow()._ = _;
const routingStore = new ExtendedRoutingStore();
const history = createBrowserHistory({
basename: getLoadConfig().basePath || '',
});
const syncedHistory = syncHistoryWithStore(history, routingStore);
const featureFlagStore = new FeatureFlagStore();
const stores = {
// Key can be whatever you want
routing: routingStore,
appStore: new AppStore(featureFlagStore),
};
browserWindow.globalStores = stores;
eventBus.on('error', (err: SiteError) => {
sendToLoggly({
message: err?.errorObj?.message,
...err.meta,
});
stores.appStore.addError(err);
});
//@ts-ignore
const end = superagent.Request.prototype.end;
let redirecting = false;
//@ts-ignore
superagent.Request.prototype.end = function(callback) {
return end.call(this, (error: any, response: any) => {
if (redirecting) {
return;
}
if (response && response.statusCode === 401) {
var storageKey = `login-redirect`;
localStorage.setItem(storageKey, window.location.href);
// build URL with a reference to storage key so that /restore route can restore it after login
//@ts-ignore because we're using buildCBioPortalPageUrl without a pathname, which is normally required
const loginUrl = buildCBioPortalPageUrl({
query: {
'spring-security-redirect': buildCBioPortalPageUrl({
pathname: 'restore',
query: { key: storageKey },
}),
},
});
redirecting = true;
window.location.href = loginUrl;
} else {
callback(error, response);
}
});
};
function enableDataDogTracking(store: AppStore) {
datadogLogs.init({
clientToken: 'pub9a94ebb002f105ff44d8e427b6549775',
site: 'datadoghq.com',
service: 'cbioportalinternal',
forwardErrorsToLogs: true,
sessionSampleRate: 100,
} as any);
const match = [
/filtered-samples/,
/clinical-data-bin-counts/,
/generic-assay-data-bin-counts/,
/mutated-genes/,
/molecular-profile-sample-counts/,
/cna-genes/,
/structuralvariant-genes/,
/clinical-data-counts/,
/sample-lists-counts/,
/clinical-data-density-plot/,
/clinical-data-violin-plots/,
/genomic-data-counts/,
/mutation-data-counts/,
/clinical-event-type-counts/,
/treatments\/patient-counts/,
/treatments\/sample-counts/,
/genomic-data-bin-counts/,
/clinical-event-type-counts/,
];
const oldRequest = (internalClient as any).request;
(internalClient as any).request = function(...args: any) {
try {
let url = args[1];
if (Object.keys(args[4]).length) {
url = url + '?' + $.param(args[4]);
}
const data = args[2];
const studyIds = data.studyIds || data.studyViewFilter.studyIds;
const appName = store.serverConfig.app_name;
if (studyIds.length < 4 && _.some(match, re => re.test(url))) {
const hash = hashString(url + JSON.stringify(toJS(data)));
datadogLogs.logger.info('study view request', {
url,
data,
hash,
appName,
});
}
} catch (ex) {
// fail silently
}
return oldRequest.apply(this, args);
};
}
//
browserWindow.routingStore = routingStore;
let render = (key?: number) => {
if (!getBrowserWindow().navigator.webdriver) initializeTracking();
if (stores.appStore?.serverConfig.user_display_name === 'servcbioportal') {
getLoadConfig().hide_login = true;
browserWindow.isMSKCIS = true;
}
// @ts-ignore
if (stores.appStore.serverConfig.app_name === 'public-portal') {
stores.appStore.serverConfig.download_custom_buttons_json = `[
{
"id": "avm",
"name": "AVM for cBioPortal",
"tooltip": "Launch AVM for cBioPortal with data (copied to clipboard)",
"image_src": "https://aquminmedical.com/images/content/AquminLogoSimple.png",
"required_user_agent": "Win",
"required_installed_font_family": "AVMInstalled",
"url_format": "avm://?importclipboard&-AutoMode=true&-ProjectNameHint={studyName}&-ImportDataLength={dataLength}",
"visualize_title": "AVM for cBioPortal (Windows)",
"visualize_href": "https://bit.ly/avm-cbioportal",
"visualize_description": "Windows software that loads data into 3D Landscapes for interactive visualization and pathway analysis. Download table data directly from cBioPortal.",
"visualize_image_src": "https://github.com/user-attachments/assets/5c17f5ed-0357-4ffa-a6e1-5a9d435dd3c5"
}
]`;
}
const rootNode = document.getElementById('reactRoot');
ReactDOM.render(
<Provider {...stores}>
<Router history={syncedHistory}>
{/*@ts-ignore*/}
<Container location={routingStore.location} />
</Router>
</Provider>,
rootNode
);
};
//@ts-ignore
if (__DEBUG__ && module.hot) {
const renderApp = render;
render = () => renderApp(Math.random());
//@ts-ignore
module.hot.accept('./routes', () => render());
}
async function loadCustomJs() {
if (!getServerConfig().custom_js_urls) {
return Promise.resolve();
}
const customJsFiles = getServerConfig().custom_js_urls.split(',');
return Promise.all(
Object.values(customJsFiles).map(
(customJsFileUrl: string) =>
new Promise((resolve, reject) => {
load(customJsFileUrl, (err: any) => {
if (err) {
reject(err);
} else {
resolve();
}
});
})
)
);
}
$(document).ready(async () => {
// we show blank page if the window.name is "blank"
if (window.name === 'blank') {
return;
}
// we use rawServerConfig (written by JSP) if it is present
// or fetch from config service if not
// need to use jsonp, so use jquery
let initialServerConfig =
browserWindow.rawServerConfig || (await fetchServerConfig());
getBrowserWindow().onMobxPromise = onMobxPromise;
initializeServerConfiguration(initialServerConfig);
initializeGenericAssayServerConfig();
initializeAPIClients();
initializeAppStore(stores.appStore);
// if (
// ['genie-public-portal', 'public-portal'].includes(
// stores.appStore.serverConfig.app_name!
// ) &&
// !isWebdriver()
// ) {
// enableDataDogTracking(stores.appStore);
// }
await loadCustomJs();
render();
stores.appStore.setAppReady();
});