-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
92 lines (81 loc) · 3.14 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
import { useKeycloak } from '@react-keycloak/web';
import { StrictMode, useCallback, useContext } from 'react';
import { Outlet } from 'react-router-dom';
import { EnvironmentBanner } from './components/Core/Banner/EnvironmentBanner';
import { ErrorView } from './components/Core/ErrorView/ErrorView';
import { Loading } from './components/Core/Loading/Loading';
import { ToastContainerWrapper } from './components/Core/Popups/Toast';
import { LockedUserView } from './components/Navigation/LockedUserView';
import { NoParticipantAccessView } from './components/Navigation/NoParticipantAccessView';
import { PortalHeader } from './components/PortalHeader/PortalHeader';
import { UpdatesTour } from './components/SiteTour/UpdatesTour';
import { configureFontAwesomeLibrary } from './configureFontAwesomeLibrary';
import { CurrentUserContext } from './contexts/CurrentUserProvider';
import { ParticipantContext, ParticipantProvider } from './contexts/ParticipantProvider';
import { HomeRedirector } from './screens/homeRedirector';
import { PortalErrorBoundary } from './utils/PortalErrorBoundary';
import 'react-toastify/dist/ReactToastify.min.css';
import './App.scss';
configureFontAwesomeLibrary();
function AppContent() {
const { LoggedInUser } = useContext(CurrentUserContext);
const { participant } = useContext(ParticipantContext);
const isLocalDev = process.env.NODE_ENV === 'development';
if (LoggedInUser?.isLocked) {
return <LockedUserView />;
}
if (LoggedInUser?.user?.participants!.length === 0) {
return <ErrorView message='You do not have access to any participants.' />;
}
if (LoggedInUser && !participant) {
return <NoParticipantAccessView user={LoggedInUser?.user} />;
}
const showUpdatesTour = !!LoggedInUser?.user?.acceptedTerms;
return (
<>
<HomeRedirector />
{showUpdatesTour && <UpdatesTour />}
<Outlet />
{isLocalDev && <EnvironmentBanner />}
</>
);
}
export function App() {
const { LoggedInUser } = useContext(CurrentUserContext);
const { keycloak, initialized } = useKeycloak();
const logout = useCallback(() => {
keycloak?.logout();
}, [keycloak]);
const setDarkMode = (darkMode: boolean) => {
if (darkMode) {
document.getElementById('root')?.classList.add('darkmode');
localStorage.setItem('isDarkMode', 'true');
} else {
document.getElementById('root')?.classList.remove('darkmode');
localStorage.setItem('isDarkMode', 'false');
}
};
const fullName =
LoggedInUser?.profile.firstName || LoggedInUser?.profile.lastName
? `${LoggedInUser?.profile.firstName ?? ''} ${LoggedInUser?.profile.lastName ?? ''}`
: undefined;
if (!initialized) return <Loading />;
return (
<StrictMode>
<PortalErrorBoundary>
<ParticipantProvider>
<div className='app'>
<PortalHeader
email={LoggedInUser?.profile?.email}
fullName={fullName}
setDarkMode={setDarkMode}
logout={logout}
/>
<AppContent />
<ToastContainerWrapper />
</div>
</ParticipantProvider>
</PortalErrorBoundary>
</StrictMode>
);
}