-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathApp.tsx
More file actions
101 lines (85 loc) · 2.66 KB
/
App.tsx
File metadata and controls
101 lines (85 loc) · 2.66 KB
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
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import React from 'react';
import {ThemeProvider} from '@greenbone/ui-lib';
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
import {Provider as StoreProvider} from 'react-redux';
import Gmp from 'gmp/gmp';
import {_, initLocale} from 'gmp/locale/lang';
import {LOG_LEVEL_DEBUG} from 'gmp/log';
import Settings from 'gmp/settings';
import {isDefined} from 'gmp/utils/identity';
import ErrorBoundary from 'web/components/error/ErrorBoundary';
import GlobalStyles from 'web/components/layout/GlobalStyles';
import GmpContext from 'web/components/provider/GmpProvider';
import LanguageProvider from 'web/components/provider/LanguageProvider';
import Routes from 'web/Routes';
import configureStore from 'web/store';
import {clearStore} from 'web/store/actions';
import {
setUsername,
setTimezone,
setIsLoggedIn,
} from 'web/store/usersettings/actions';
void initLocale();
const queryClient = new QueryClient();
const settings = new Settings(global.localStorage, global.config);
const gmp = new Gmp(settings);
const store = configureStore({
debug: isDefined(settings.enableStoreDebugLog)
? settings.enableStoreDebugLog
: settings.logLevel === LOG_LEVEL_DEBUG,
});
// @ts-expect-error
window.gmp = gmp;
const initStore = () => {
const {timezone, username} = gmp.settings;
if (isDefined(timezone)) {
store.dispatch(setTimezone(timezone));
}
if (isDefined(username)) {
store.dispatch(setUsername(username));
}
store.dispatch(setIsLoggedIn(gmp.isLoggedIn()));
};
class App extends React.Component<{}> {
private unsubscribeFromLogout!: () => void;
constructor(props: {}) {
super(props);
this.handleLogout = this.handleLogout.bind(this);
}
componentDidMount() {
this.unsubscribeFromLogout = gmp.subscribeToLogout(this.handleLogout);
initStore();
}
componentWillUnmount() {
if (isDefined(this.unsubscribeFromLogout)) {
this.unsubscribeFromLogout();
}
}
handleLogout() {
// cleanup store
clearStore(store.dispatch);
}
render() {
return (
<ThemeProvider defaultColorScheme="light">
<GlobalStyles />
<ErrorBoundary message={_('An error occurred on this page')}>
<GmpContext.Provider value={gmp}>
<QueryClientProvider client={queryClient}>
<StoreProvider store={store}>
<LanguageProvider>
<Routes />
</LanguageProvider>
</StoreProvider>
</QueryClientProvider>
</GmpContext.Provider>
</ErrorBoundary>
</ThemeProvider>
);
}
}
export default App;