forked from cBioPortal/cbioportal-frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppStore.ts
142 lines (119 loc) · 4.14 KB
/
AppStore.ts
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
import { action, computed, observable, makeObservable } from 'mobx';
import {
addServiceErrorHandler,
getBrowserWindow,
remoteData,
} from 'cbioportal-frontend-commons';
import { getLoadConfig, getServerConfig } from './config/config';
import _ from 'lodash';
import client from 'shared/api/cbioportalClientInstance';
import { sendSentryMessage } from './shared/lib/tracking';
import { FeatureFlagStore } from 'shared/FeatureFlagStore';
import { SiteError } from 'shared/model/appMisc';
export class AppStore {
constructor(
public featureFlagStore: FeatureFlagStore = new FeatureFlagStore()
) {
makeObservable(this);
getBrowserWindow().me = this;
addServiceErrorHandler((error: any) => {
try {
sendSentryMessage('ERRORHANDLER:' + error);
} catch (ex) {}
if (error.status && /400|500|5\d\d|403/.test(error.status)) {
sendSentryMessage('ERROR DIALOG SHOWN:' + error);
if (error instanceof Error) {
this.siteErrors.push(new SiteError(error));
} else {
this.siteErrors.push(new SiteError(new Error(error)));
}
}
});
}
get serverConfig() {
return getServerConfig();
}
get loadConfig() {
return getLoadConfig();
}
@observable private _appReady = false;
siteErrors = observable.array<SiteError>();
alertErrors = observable.array<SiteError>();
@observable.ref userName: string | undefined = undefined;
@observable.ref authMethod: string | undefined = undefined;
@computed get isLoggedIn() {
return _.isString(this.userName) && this.userName !== 'anonymousUser';
}
@computed get isSocialAuthenticated() {
if (this.authMethod) {
return this.authMethod.includes('optional_oauth2');
}
return false;
}
get isPublicPortal() {
return this.serverConfig.app_name === 'public-portal';
}
@computed get logoutUrl() {
return 'logout';
}
@computed get isErrorCondition() {
return this.siteErrors.length > 0;
}
@action
public dismissErrors() {
this.siteErrors.clear();
}
@action
public dismissError(err: SiteError) {
this.siteErrors.remove(err);
}
@action public addError(err: SiteError | string) {
if (typeof err === 'string') {
this.siteErrors.push(new SiteError(new Error(err)));
} else {
if (err.displayType === 'alert') {
this.alertErrors.push(err);
} else {
this.siteErrors.push(err);
}
}
}
@action
public setAppReady() {
this._appReady = true;
}
public get appReady() {
return this._appReady;
}
readonly portalVersion = remoteData<string | undefined>({
invoke: async () => {
const portalVersionResult = await client.getInfoUsingGET({});
if (portalVersionResult && portalVersionResult.portalVersion) {
let version = undefined;
// try getting version from branch name assume like release-x.y.z
if (
portalVersionResult.gitBranch &&
portalVersionResult.gitBranch.startsWith('release-')
) {
let branchVersion = portalVersionResult.gitBranch.split(
'-'
)[1];
if (branchVersion.split('.').length == 3) {
version = branchVersion;
}
}
// if branch name does not contain version name, use
// portalVersion
if (version === undefined) {
version = portalVersionResult.portalVersion.split('-')[0];
}
// add v prefix if missing
if (version !== undefined && !version.startsWith('v')) {
version = `v${version}`;
}
return Promise.resolve(version);
}
return undefined;
},
});
}