/
App.tsx
289 lines (268 loc) · 11.3 KB
/
App.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
/* eslint-disable */
import * as React from 'react';
import { ExoticComponent, ReactNode, useState } from 'react';
import {NavLink, Redirect, Route, Switch} from 'react-router-dom';
import {Button, Col, Dropdown, Form, Input, Layout, Menu, message, Modal, Row, Tooltip} from 'antd';
import './App.less';
import ApplicationServices, {setDebugInfo} from './lib/services/ApplicationServices';
import {Align, CenteredSpin, GlobalError, handleError, Preloader} from './lib/components/components';
import {reloadPage} from './lib/commons/utils';
import {User} from './lib/services/model';
import OnboardingForm from './lib/components/OnboardingForm/OnboardingForm';
import { PRIVATE_PAGES, PUBLIC_PAGES, SELFHOSTED_PAGES} from './navigation';
import { ApplicationPage, SlackChatWidget } from './Layout';
import { PaymentPlanStatus } from '@service/billing';
enum AppLifecycle {
LOADING, //Application is loading
REQUIRES_LOGIN, //Login form is displayed
APP, //Application
ERROR //Global error (maintenance)
}
type AppState = {
showOnboardingForm: boolean;
lifecycle: AppLifecycle;
globalErrorDetails?: string;
extraControls?: React.ReactNode;
user?: User;
paymentPlanStatus?: PaymentPlanStatus;
};
const LOGIN_TIMEOUT = 5000;
export default class App extends React.Component<{}, AppState> {
private readonly services: ApplicationServices;
constructor(props: any, context: any) {
super(props, context);
this.services = ApplicationServices.get();
setDebugInfo('applicationServices', this.services, false);
this.state = {
lifecycle: AppLifecycle.LOADING,
showOnboardingForm: false,
extraControls: null
};
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
this.services.analyticsService.onGlobalError(error);
}
public async componentDidMount() {
try {
await this.services.init();
const loginStatus = await this.services.userService.waitForUser();
setDebugInfo('user', loginStatus.user);
if (loginStatus.user) {
this.services.analyticsService.onUserKnown(loginStatus.user);
}
let paymentPlanStatus: (PaymentPlanStatus | undefined) = undefined;
if (loginStatus.user && this.services.features.billingEnabled) {
paymentPlanStatus = new PaymentPlanStatus();
await paymentPlanStatus.init(this.services.activeProject, this.services.backendApiClient)
}
this.setState({
lifecycle: loginStatus.user ? AppLifecycle.APP : AppLifecycle.REQUIRES_LOGIN,
user: loginStatus.user,
showOnboardingForm: !!loginStatus.user && !loginStatus.user.onboarded,
paymentPlanStatus: paymentPlanStatus
});
} catch (error) {
console.error('Failed to initialize ApplicationServices', error);
if (this.services.analyticsService) {
this.services.analyticsService.onGlobalError(error, true);
} else {
console.error("Failed to send error to analytics service, it's not defined yet");
}
this.setState({lifecycle: AppLifecycle.ERROR});
return;
}
window.setTimeout(() => {
if (this.state.lifecycle == AppLifecycle.LOADING) {
this.services.analyticsService.onGlobalError(new Error('Login timeout'));
this.setState({lifecycle: AppLifecycle.ERROR, globalErrorDetails: 'Timeout'});
}
}, LOGIN_TIMEOUT);
}
private getRenderComponent() {
switch (this.state.lifecycle) {
case AppLifecycle.REQUIRES_LOGIN:
let pages = this.services.showSelfHostedSignUp() ? SELFHOSTED_PAGES : PUBLIC_PAGES;
return <>
<Switch>
{pages.map((route) => {
let Component = route.component as ExoticComponent;
return (
<Route
key={route.getPrefixedPath().join('')}
path={route.getPrefixedPath()}
exact
render={(routeProps) => {
this.services.analyticsService.onPageLoad({
pagePath: routeProps.location.key || '/unknown'
});
document.title = route.pageTitle;
return <Component {...(routeProps as any)} />;
}}
/>
);
})}
<Redirect key="rootRedirect" to="/"/>
</Switch>
</>;
case AppLifecycle.APP:
return <>
{this.appLayout()}
{this.services.features.chatSupportType === 'slack' && <SlackChatWidget />}
</>;
case AppLifecycle.ERROR:
return <GlobalError/>;
case AppLifecycle.LOADING:
return <Preloader/>;
}
}
public render() {
return <React.Suspense fallback={<CenteredSpin/>}>{this.getRenderComponent()}</React.Suspense>;
}
appLayout() {
let routes = PRIVATE_PAGES.map((route) => {
if (!this.state.showOnboardingForm) {
let Component = route.component as ExoticComponent;
return <Route
key={route.pageTitle}
path={route.getPrefixedPath()}
exact={true}
render={(routeProps) => {
this.services.analyticsService.onPageLoad({
pagePath: routeProps.location.hash
});
document.title = route.pageTitle;
return route.doNotWrap ?
<Component {...(routeProps as any)} /> :
<ApplicationPage user={this.state.user} plan={this.state.paymentPlanStatus} page={route} {...routeProps} />;
}}
/>;
} else {
return <CenteredSpin/>;
}
});
routes.push(<Redirect key="dashboardRedirect" to="/dashboard"/>);
let extraForms = null;
if (this.state.showOnboardingForm) {
extraForms = (
<OnboardingForm
user={this.state.user}
onCompleted={async () => {
await this.services.userService.waitForUser();
this.setState({showOnboardingForm: false});
}}
/>
);
} else if (this.services.userService.getUser().forcePasswordChange) {
return (
<SetNewPassword
onCompleted={async () => {
reloadPage();
}}
/>
);
}
return (
<>
<Switch>{routes}</Switch>
{extraForms}
</>
);
}
}
function SetNewPassword({onCompleted}: { onCompleted: () => Promise<void> }) {
let [loading, setLoading] = useState(false);
let services = ApplicationServices.get();
let [form] = Form.useForm();
return (
<Modal
title="Please, set a new password"
visible={true}
closable={false}
footer={
<>
<Button
onClick={() => {
services.userService.removeAuth(reloadPage);
}}
>
Logout
</Button>
<Button
type="primary"
loading={loading}
onClick={async () => {
setLoading(true);
let values;
try {
values = await form.validateFields();
} catch (e) {
//error will be displayed on the form, not need for special handling
setLoading(false);
return;
}
try {
let newPassword = values['password'];
await services.userService.changePassword(newPassword);
await services.userService.login(services.userService.getUser().email, newPassword);
let user = (await services.userService.waitForUser()).user;
user.forcePasswordChange = false;
//await services.userService.update(user);
await onCompleted();
} catch (e) {
if ('auth/requires-recent-login' === e.code) {
services.userService.removeAuth(() => {
reloadPage();
});
} else {
handleError(e);
}
} finally {
setLoading(false);
}
}}
>
Set new password
</Button>
</>
}
>
<Form form={form} layout="vertical" requiredMark={false}>
<Form.Item
name="password"
label="Password"
rules={[
{
required: true,
message: 'Please input your password!'
}
]}
hasFeedback
>
<Input.Password/>
</Form.Item>
<Form.Item
name="confirm"
label="Confirm Password"
dependencies={['password']}
hasFeedback
rules={[
{
required: true,
message: 'Please confirm your password!'
},
({getFieldValue}) => ({
validator(rule, value) {
if (!value || getFieldValue('password') === value) {
return Promise.resolve();
}
return Promise.reject('The two passwords that you entered do not match!');
}
})
]}
>
<Input.Password/>
</Form.Item>
</Form>
</Modal>
);
}