-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathApp.jsx
170 lines (155 loc) · 4.66 KB
/
App.jsx
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
// eslint-disable-next-line camelcase
import { unstable_batchedUpdates } from 'react-dom';
import React, { useCallback, useEffect, useState } from 'react';
// import { useTranslation } from 'react-i18next';
import './App.css';
import {
SELECTED_PROJECT_LOCAL_STORAGE_KEY,
authApi,
extensionsApi,
usersApi,
} from '../../services/contaxy-api';
import { mapExtensionToAppPage } from '../../utils/app-pages';
import AppBar from '../../components/AppBar/AppBar';
import AppDrawer from '../../components/AppDrawer/AppDrawer';
import ContentContainer from '../../app/routing/ContentContainer';
import GlobalStateContainer from '../../app/store';
function App() {
// const { t } = useTranslation();
const [isDrawerOpen, setDrawerOpen] = useState(false);
const [additionalAppDrawerItems, setAdditionalAppDrawerItems] = useState([]);
const {
activeProject,
setActiveProject,
loadProjects,
setUser,
projects,
user,
projectExtensions,
setProjectExtensions,
setIsAuthenticated,
isAuthenticated,
setOauthEnabled,
} = GlobalStateContainer.useContainer();
const onDrawerClick = useCallback(
() => setDrawerOpen(!isDrawerOpen),
[isDrawerOpen]
);
useEffect(() => {
if (isAuthenticated && user) return;
async function requestUserInfo() {
// Check whether the user is logged in currently (the auth cookie - if existing - is sent to the endpoint which returns a user object when a valid token exists and an error otherwise)
let userInfo;
try {
userInfo = await usersApi.getMyUser();
} catch (e) {
unstable_batchedUpdates(() => {
setUser(null);
setIsAuthenticated(false);
});
return;
}
try {
await authApi.verifyAccess({
permission: '*#admin',
});
userInfo.is_admin = true;
} catch (e) {
userInfo.is_admin = false;
}
unstable_batchedUpdates(() => {
setUser(userInfo);
setIsAuthenticated(true);
});
}
requestUserInfo();
}, [user, isAuthenticated, setUser, setIsAuthenticated]);
useEffect(() => {
if (!user) return;
loadProjects();
}, [user, loadProjects]);
useEffect(() => {
if (activeProject.id || !projects || projects.length === 0) return;
const prevSelectedProjectId = window.localStorage.getItem(
SELECTED_PROJECT_LOCAL_STORAGE_KEY
);
if (prevSelectedProjectId) {
const previouslySelectedProject = projects.find(
(project) => project.id === prevSelectedProjectId
);
if (previouslySelectedProject) {
setActiveProject(previouslySelectedProject);
return;
}
// This project is not accessible anymore
window.localStorage.removeItem(SELECTED_PROJECT_LOCAL_STORAGE_KEY);
}
const userProject = projects.find((project) => project.id === user.id);
if (!userProject) return;
setActiveProject(userProject);
window.localStorage.setItem(
SELECTED_PROJECT_LOCAL_STORAGE_KEY,
userProject.id
);
}, [activeProject, setActiveProject, projects, user]);
useEffect(() => {
// Check whether external authentication is activated
authApi
.oauthEnabled()
.then((res) => {
setOauthEnabled(res === '1');
})
.catch(() => {
setOauthEnabled(false);
});
}, [setOauthEnabled]);
// TODO: Load project specific extensions
useEffect(() => {
if (!isAuthenticated) return;
extensionsApi
.listExtensions('ctxy-global')
.then((res) => setProjectExtensions(res))
.catch(() => {});
}, [isAuthenticated, setProjectExtensions]);
useEffect(() => {
setAdditionalAppDrawerItems(
projectExtensions.map((extension) => mapExtensionToAppPage(extension))
);
}, [projectExtensions]);
const [appDrawerElement, setAppDrawerElement] = useState(false);
useEffect(() => {
const newState =
!isAuthenticated || !user ? (
false
) : (
<AppDrawer
isAdmin={user.is_admin}
open={isDrawerOpen}
additionalPages={additionalAppDrawerItems}
handleDrawerClose={onDrawerClick}
/>
);
setAppDrawerElement(newState);
}, [
isAuthenticated,
setAppDrawerElement,
additionalAppDrawerItems,
isDrawerOpen,
onDrawerClick,
user,
]);
return (
<div className="App">
<AppBar isAuthenticated={isAuthenticated} onDrawerOpen={onDrawerClick} />
{appDrawerElement}
<main className="main">
<ContentContainer
isAuthenticated={isAuthenticated}
additionalPages={additionalAppDrawerItems}
/>
</main>
<div id="snackbar-container" />
</div>
);
}
export default App;