Skip to content

Commit 4d043b7

Browse files
Remove old authentication flag and update fn call (#76) & Update commons-ui ts (#77)
1 parent b98f229 commit 4d043b7

14 files changed

+438
-202
lines changed

package-lock.json

+274-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"dependencies": {
1212
"@emotion/react": "^11.11.4",
1313
"@emotion/styled": "^11.11.5",
14-
"@gridsuite/commons-ui": "0.53.0",
14+
"@gridsuite/commons-ui": "0.63.0",
1515
"@hookform/resolvers": "^3.3.4",
1616
"@mui/icons-material": "^5.15.14",
1717
"@mui/lab": "^5.0.0-alpha.169",

src/components/app-top-bar.tsx

+13-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import React, {
1111
useEffect,
1212
useState,
1313
} from 'react';
14-
import { LIGHT_THEME, logout, TopBar } from '@gridsuite/commons-ui';
14+
import {
15+
LIGHT_THEME,
16+
logout,
17+
TopBar,
18+
UserManagerState,
19+
} from '@gridsuite/commons-ui';
1520
import Parameters, { useParameterState } from './parameters';
1621
import { APP_NAME, PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
1722
import { useDispatch, useSelector } from 'react-redux';
@@ -25,18 +30,16 @@ import { useNavigate } from 'react-router-dom';
2530
import { ReactComponent as PowsyblLogo } from '../images/powsybl_logo.svg';
2631
import AppPackage from '../../package.json';
2732
import { AppState } from '../redux/reducer';
33+
import { AppDispatch } from '../redux/store';
2834

2935
export type AppTopBarProps = {
3036
user?: AppState['user'];
31-
userManager: {
32-
instance: unknown | null;
33-
error: string | null;
34-
};
37+
userManager: UserManagerState;
3538
};
3639
const AppTopBar: FunctionComponent<AppTopBarProps> = (props) => {
3740
const navigate = useNavigate();
3841

39-
const dispatch = useDispatch();
42+
const dispatch = useDispatch<AppDispatch>();
4043

4144
const [appsAndUrls, setAppsAndUrls] = useState<MetadataJson[]>([]);
4245

@@ -78,10 +81,12 @@ const AppTopBar: FunctionComponent<AppTopBarProps> = (props) => {
7881
logout(dispatch, props.userManager.instance)
7982
}
8083
onLogoClick={() => navigate('/', { replace: true })}
81-
user={props.user}
84+
user={props.user ?? undefined}
8285
appsAndUrls={appsAndUrls}
8386
globalVersionPromise={() =>
84-
fetchVersion().then((res) => res?.deployVersion)
87+
fetchVersion().then(
88+
(res) => res?.deployVersion ?? 'unknown'
89+
)
8590
}
8691
additionalModulesPromise={getServersInfos}
8792
onThemeClick={handleChangeTheme}

src/components/app-wrapper.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
card_error_boundary_en,
1919
card_error_boundary_fr,
2020
CardErrorBoundary,
21+
GsLangUser,
2122
LIGHT_THEME,
2223
login_en,
2324
login_fr,
@@ -28,7 +29,6 @@ import {
2829
import { IntlProvider } from 'react-intl';
2930
import { BrowserRouter } from 'react-router-dom';
3031
import { Provider, useSelector } from 'react-redux';
31-
import { SupportedLanguages } from '../utils/language';
3232
import messages_en from '../translations/en.json';
3333
import messages_fr from '../translations/fr.json';
3434
import messages_plugins_en from '../plugins/translations/en.json';
@@ -98,7 +98,7 @@ const getMuiTheme = (theme: string): Theme => {
9898
}
9999
};
100100

101-
const messages: Record<SupportedLanguages, IntlConfig['messages']> = {
101+
const messages: Record<GsLangUser, IntlConfig['messages']> = {
102102
en: {
103103
...messages_en,
104104
...login_en,

src/components/app.tsx

+43-41
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import { AppState } from '../redux/reducer';
3939
import {
4040
ConfigParameters,
4141
connectNotificationsWsUpdateConfig,
42-
fetchAuthorizationCodeFlowFeatureFlag,
4342
fetchConfigParameter,
4443
fetchConfigParameters,
44+
fetchIdpSettings,
4545
fetchValidateUser,
4646
} from '../utils/rest-api';
4747
import {
@@ -54,6 +54,7 @@ import { getComputedLanguage } from '../utils/language';
5454
import AppTopBar, { AppTopBarProps } from './app-top-bar';
5555
import ReconnectingWebSocket from 'reconnecting-websocket';
5656
import { getErrorMessage } from '../utils/error';
57+
import { AppDispatch } from '../redux/store';
5758

5859
const App: FunctionComponent = () => {
5960
const { snackError } = useSnackMessage();
@@ -76,7 +77,7 @@ const App: FunctionComponent = () => {
7677

7778
const navigate = useNavigate();
7879

79-
const dispatch = useDispatch();
80+
const dispatch = useDispatch<AppDispatch>();
8081

8182
const location = useLocation();
8283

@@ -139,50 +140,45 @@ const App: FunctionComponent = () => {
139140
})
140141
);
141142

142-
const initialize = useCallback((): Promise<unknown | undefined> => {
143-
if (process.env.REACT_APP_USE_AUTHENTICATION === 'true') {
144-
return fetchAuthorizationCodeFlowFeatureFlag().then(
145-
(authorizationCodeFlowEnabled) =>
146-
initializeAuthenticationProd(
147-
dispatch,
148-
initialMatchSilentRenewCallbackUrl != null,
149-
fetch('idpSettings.json'),
150-
fetchValidateUser,
151-
authorizationCodeFlowEnabled,
152-
initialMatchSigninCallbackUrl != null
153-
)
154-
);
155-
} else {
156-
return initializeAuthenticationDev(
157-
dispatch,
158-
initialMatchSilentRenewCallbackUrl != null,
159-
() =>
160-
new Promise((resolve) =>
161-
window.setTimeout(() => resolve(true), 500)
162-
),
163-
initialMatchSigninCallbackUrl != null
164-
);
165-
}
166-
// Note: initialMatchSilentRenewCallbackUrl and dispatch don't change
167-
}, [
168-
initialMatchSilentRenewCallbackUrl,
169-
dispatch,
170-
initialMatchSigninCallbackUrl,
171-
]);
172-
173143
useEffect(() => {
174-
initialize()
175-
.then((userManager) => {
176-
setUserManager({ instance: userManager ?? null, error: null });
177-
})
178-
.catch((error: unknown) => {
144+
// need subfunction when async as suggested by rule react-hooks/exhaustive-deps
145+
(async function initializeAuthentication() {
146+
try {
147+
console.debug(
148+
`auth dev mode: ${process.env.REACT_APP_USE_AUTHENTICATION}`
149+
);
150+
const initAuth =
151+
process.env.REACT_APP_USE_AUTHENTICATION === 'true'
152+
? initializeAuthenticationProd(
153+
dispatch,
154+
initialMatchSilentRenewCallbackUrl != null,
155+
fetchIdpSettings,
156+
fetchValidateUser,
157+
initialMatchSigninCallbackUrl != null
158+
)
159+
: initializeAuthenticationDev(
160+
dispatch,
161+
initialMatchSilentRenewCallbackUrl != null,
162+
validateUserDev,
163+
initialMatchSigninCallbackUrl != null
164+
);
165+
setUserManager({
166+
instance: (await initAuth) ?? null,
167+
error: null,
168+
});
169+
} catch (error) {
179170
setUserManager({
180171
instance: null,
181172
error: getErrorMessage(error),
182173
});
183-
});
184-
// Note: initialize and initialMatchSilentRenewCallbackUrl won't change
185-
}, [initialize, initialMatchSilentRenewCallbackUrl, dispatch]);
174+
}
175+
})();
176+
// Note: dispatch and initialMatchSilentRenewCallbackUrl won't change
177+
}, [
178+
initialMatchSigninCallbackUrl,
179+
initialMatchSilentRenewCallbackUrl,
180+
dispatch,
181+
]);
186182

187183
useEffect(() => {
188184
if (user !== null) {
@@ -280,3 +276,9 @@ const App: FunctionComponent = () => {
280276
);
281277
};
282278
export default App;
279+
280+
function validateUserDev(): Promise<boolean> {
281+
return new Promise((resolve) =>
282+
window.setTimeout(() => resolve(true), 500)
283+
);
284+
}

src/module-commons-ui.d.ts

-11
This file was deleted.

src/redux/actions.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8+
import { GsTheme } from '@gridsuite/commons-ui';
89
import { PARAM_LANGUAGE } from '../utils/config-params';
910
import { Action } from 'redux';
1011
import { AppState } from './reducer';
1112

1213
export const SELECT_THEME = 'SELECT_THEME';
1314
export type ThemeAction = Readonly<Action<typeof SELECT_THEME>> & {
14-
theme: string;
15+
theme: GsTheme;
1516
};
16-
export function selectTheme(theme: string): ThemeAction {
17+
18+
export function selectTheme(theme: GsTheme): ThemeAction {
1719
return { type: SELECT_THEME, theme: theme };
1820
}
1921

2022
export const SELECT_LANGUAGE = 'SELECT_LANGUAGE';
2123
export type LanguageAction = Readonly<Action<typeof SELECT_LANGUAGE>> & {
2224
[PARAM_LANGUAGE]: AppState['language'];
2325
};
26+
2427
export function selectLanguage(language: AppState['language']): LanguageAction {
2528
return { type: SELECT_LANGUAGE, [PARAM_LANGUAGE]: language };
2629
}
@@ -31,6 +34,7 @@ export type ComputedLanguageAction = Readonly<
3134
> & {
3235
computedLanguage: AppState['computedLanguage'];
3336
};
37+
3438
export function selectComputedLanguage(
3539
computedLanguage: AppState['computedLanguage']
3640
): ComputedLanguageAction {

src/redux/local-storage.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,40 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { DARK_THEME, LANG_SYSTEM } from '@gridsuite/commons-ui';
8+
import {
9+
DARK_THEME,
10+
GsLang,
11+
GsLangUser,
12+
GsTheme,
13+
LANG_SYSTEM,
14+
} from '@gridsuite/commons-ui';
915
import { getComputedLanguage } from '../utils/language';
1016
import { APP_NAME } from '../utils/config-params';
11-
import { AppState } from './reducer';
1217

1318
const LOCAL_STORAGE_THEME_KEY = (APP_NAME + '_THEME').toUpperCase();
1419
const LOCAL_STORAGE_LANGUAGE_KEY = (APP_NAME + '_LANGUAGE').toUpperCase();
1520

16-
export function getLocalStorageTheme(): string {
17-
return localStorage.getItem(LOCAL_STORAGE_THEME_KEY) || DARK_THEME;
21+
export function getLocalStorageTheme() {
22+
return (
23+
(localStorage.getItem(LOCAL_STORAGE_THEME_KEY) as GsTheme) || DARK_THEME
24+
);
1825
}
1926

20-
export function saveLocalStorageTheme(theme: string): void {
27+
export function saveLocalStorageTheme(theme: GsTheme): void {
2128
localStorage.setItem(LOCAL_STORAGE_THEME_KEY, theme);
2229
}
2330

24-
export function getLocalStorageLanguage(): AppState['language'] {
25-
return localStorage.getItem(LOCAL_STORAGE_LANGUAGE_KEY) || LANG_SYSTEM;
31+
export function getLocalStorageLanguage() {
32+
return (
33+
(localStorage.getItem(LOCAL_STORAGE_LANGUAGE_KEY) as GsLang) ||
34+
LANG_SYSTEM
35+
);
2636
}
2737

28-
export function saveLocalStorageLanguage(language: AppState['language']): void {
38+
export function saveLocalStorageLanguage(language: GsLang): void {
2939
localStorage.setItem(LOCAL_STORAGE_LANGUAGE_KEY, language);
3040
}
3141

32-
export function getLocalStorageComputedLanguage(): AppState['computedLanguage'] {
42+
export function getLocalStorageComputedLanguage(): GsLangUser {
3343
return getComputedLanguage(getLocalStorageLanguage());
3444
}

0 commit comments

Comments
 (0)