|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { useCallback, useEffect, useState } from 'react'; |
| 9 | +import { useDispatch, useSelector } from 'react-redux'; |
| 10 | +import { Grid } from '@mui/material'; |
| 11 | +import { |
| 12 | + AnnouncementNotification, |
| 13 | + AuthenticationRouter, |
| 14 | + CardErrorBoundary, |
| 15 | + fetchConfigParameter, |
| 16 | + fetchConfigParameters, |
| 17 | + getComputedLanguage, |
| 18 | + getPreLoginPath, |
| 19 | + initializeAuthenticationProd, |
| 20 | + NotificationsUrlKeys, |
| 21 | + useNotificationsListener, |
| 22 | + UserManagerState, |
| 23 | + useSnackMessage, |
| 24 | +} from '@gridsuite/commons-ui'; |
| 25 | +import { selectComputedLanguage, selectLanguage, selectTheme } from '../../redux/actions'; |
| 26 | +import { AppState } from '../../redux/reducer'; |
| 27 | +import { AppsMetadataSrv, ConfigParameters } from '../../services'; |
| 28 | +import { APP_NAME, COMMON_APP_NAME, PARAM_LANGUAGE, PARAM_THEME } from '../../utils/config-params'; |
| 29 | +import { AppTopBar } from './AppTopBar'; |
| 30 | +import { useDebugRender } from '../../utils/hooks'; |
| 31 | +import { AppDispatch } from '../../redux/store'; |
| 32 | +import { Navigate, Route, Routes, useLocation, useMatch, useNavigate } from 'react-router'; |
| 33 | +import PageNotFound from './PageNotFound'; |
| 34 | +import { FormattedMessage } from 'react-intl'; |
| 35 | +import { MainPaths } from './utils'; |
| 36 | +import { Announcements, Groups, Profiles, Users } from '../../pages'; |
| 37 | +import HomePage from './HomePage'; |
| 38 | + |
| 39 | +export default function App() { |
| 40 | + useDebugRender('app'); |
| 41 | + const { snackError } = useSnackMessage(); |
| 42 | + const dispatch = useDispatch<AppDispatch>(); |
| 43 | + const user = useSelector((state: AppState) => state.user); |
| 44 | + |
| 45 | + const updateParams = useCallback( |
| 46 | + (params: ConfigParameters) => { |
| 47 | + console.groupCollapsed('received UI parameters'); |
| 48 | + console.table(params); |
| 49 | + console.groupEnd(); |
| 50 | + params.forEach((param) => { |
| 51 | + switch (param.name) { |
| 52 | + case PARAM_THEME: |
| 53 | + dispatch(selectTheme(param.value)); |
| 54 | + break; |
| 55 | + case PARAM_LANGUAGE: |
| 56 | + dispatch(selectLanguage(param.value)); |
| 57 | + dispatch(selectComputedLanguage(getComputedLanguage(param.value))); |
| 58 | + break; |
| 59 | + default: |
| 60 | + break; |
| 61 | + } |
| 62 | + }); |
| 63 | + }, |
| 64 | + [dispatch] |
| 65 | + ); |
| 66 | + |
| 67 | + const updateConfig = useCallback( |
| 68 | + (event: MessageEvent) => { |
| 69 | + const eventData = JSON.parse(event.data); |
| 70 | + if (eventData?.headers?.parameterName) { |
| 71 | + fetchConfigParameter(APP_NAME, eventData.headers.parameterName) |
| 72 | + .then((param) => updateParams([param])) |
| 73 | + .catch((error) => snackError({ messageTxt: error.message, headerId: 'paramsRetrievingError' })); |
| 74 | + } |
| 75 | + }, |
| 76 | + [updateParams, snackError] |
| 77 | + ); |
| 78 | + |
| 79 | + useNotificationsListener(NotificationsUrlKeys.CONFIG, { listenerCallbackMessage: updateConfig }); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + if (user !== null) { |
| 83 | + fetchConfigParameters(COMMON_APP_NAME) |
| 84 | + .then((params) => updateParams(params)) |
| 85 | + .catch((error) => |
| 86 | + snackError({ |
| 87 | + messageTxt: error.message, |
| 88 | + headerId: 'paramsRetrievingError', |
| 89 | + }) |
| 90 | + ); |
| 91 | + |
| 92 | + fetchConfigParameters(APP_NAME) |
| 93 | + .then((params) => updateParams(params)) |
| 94 | + .catch((error) => |
| 95 | + snackError({ |
| 96 | + messageTxt: error.message, |
| 97 | + headerId: 'paramsRetrievingError', |
| 98 | + }) |
| 99 | + ); |
| 100 | + } |
| 101 | + }, [user, dispatch, updateParams, snackError]); |
| 102 | + |
| 103 | + const signInCallbackError = useSelector((state: AppState) => state.signInCallbackError); |
| 104 | + const authenticationRouterError = useSelector((state: AppState) => state.authenticationRouterError); |
| 105 | + const showAuthenticationRouterLogin = useSelector((state: AppState) => state.showAuthenticationRouterLogin); |
| 106 | + const [userManager, setUserManager] = useState<UserManagerState>({ |
| 107 | + instance: null, |
| 108 | + error: null, |
| 109 | + }); |
| 110 | + const navigate = useNavigate(); |
| 111 | + const location = useLocation(); |
| 112 | + |
| 113 | + // Can't use lazy initializer because useRouteMatch is a hook |
| 114 | + const [initialMatchSilentRenewCallbackUrl] = useState( |
| 115 | + useMatch({ |
| 116 | + path: '/silent-renew-callback', |
| 117 | + }) |
| 118 | + ); |
| 119 | + |
| 120 | + const [initialMatchSigninCallbackUrl] = useState( |
| 121 | + useMatch({ |
| 122 | + path: '/sign-in-callback', |
| 123 | + }) |
| 124 | + ); |
| 125 | + |
| 126 | + useEffect(() => { |
| 127 | + // need subfunction when async as suggested by rule react-hooks/exhaustive-deps |
| 128 | + (async function initializeAuthentication() { |
| 129 | + try { |
| 130 | + setUserManager({ |
| 131 | + instance: await initializeAuthenticationProd( |
| 132 | + dispatch, |
| 133 | + initialMatchSilentRenewCallbackUrl != null, |
| 134 | + AppsMetadataSrv.fetchIdpSettings, |
| 135 | + initialMatchSigninCallbackUrl != null |
| 136 | + ), |
| 137 | + error: null, |
| 138 | + }); |
| 139 | + } catch (error: any) { |
| 140 | + setUserManager({ instance: null, error: error.message }); |
| 141 | + } |
| 142 | + })(); |
| 143 | + // Note: initialMatchSilentRenewCallbackUrl and dispatch don't change |
| 144 | + }, [initialMatchSilentRenewCallbackUrl, dispatch, initialMatchSigninCallbackUrl]); |
| 145 | + |
| 146 | + return ( |
| 147 | + <Grid |
| 148 | + container |
| 149 | + direction="column" |
| 150 | + spacing={0} |
| 151 | + justifyContent="flex-start" |
| 152 | + alignItems="stretch" |
| 153 | + sx={{ height: '100vh', width: '100vw' }} |
| 154 | + > |
| 155 | + <Grid item xs="auto"> |
| 156 | + <AppTopBar userManagerInstance={userManager.instance} /> |
| 157 | + </Grid> |
| 158 | + <Grid item xs="auto"> |
| 159 | + <AnnouncementNotification user={user} /> |
| 160 | + </Grid> |
| 161 | + <Grid item container xs component="main" height={'100%'}> |
| 162 | + <CardErrorBoundary> |
| 163 | + <div |
| 164 | + className="singlestretch-parent" |
| 165 | + style={{ |
| 166 | + flexGrow: 1, |
| 167 | + }} |
| 168 | + > |
| 169 | + {user !== null ? ( |
| 170 | + <Routes> |
| 171 | + <Route path={'/'} element={<HomePage />} /> |
| 172 | + <Route path={`/${MainPaths.users}`} element={<Users />} /> |
| 173 | + <Route path={`/${MainPaths.profiles}`} element={<Profiles />} /> |
| 174 | + <Route path={`/${MainPaths.groups}`} element={<Groups />} /> |
| 175 | + <Route path={`/${MainPaths.announcements}`} element={<Announcements />} /> |
| 176 | + <Route |
| 177 | + path="/sign-in-callback" |
| 178 | + element={<Navigate replace to={getPreLoginPath() || '/'} />} |
| 179 | + /> |
| 180 | + <Route |
| 181 | + path="/logout-callback" |
| 182 | + element={<h1>Error: logout failed; you are still logged in.</h1>} |
| 183 | + /> |
| 184 | + <Route |
| 185 | + path="*" |
| 186 | + element={<PageNotFound message={<FormattedMessage id="PageNotFound" />} />} |
| 187 | + /> |
| 188 | + </Routes> |
| 189 | + ) : ( |
| 190 | + <AuthenticationRouter |
| 191 | + userManager={userManager} |
| 192 | + signInCallbackError={signInCallbackError} |
| 193 | + authenticationRouterError={authenticationRouterError} |
| 194 | + showAuthenticationRouterLogin={showAuthenticationRouterLogin} |
| 195 | + dispatch={dispatch} |
| 196 | + navigate={navigate} |
| 197 | + location={location} |
| 198 | + /> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + </CardErrorBoundary> |
| 202 | + </Grid> |
| 203 | + </Grid> |
| 204 | + ); |
| 205 | +} |
0 commit comments