Skip to content

Commit c189418

Browse files
authored
Refactor config notifications management with NotificationsProvider (#2425)
* refactor(): Replace config notifications management with NotificationsProvider from commons-ui Signed-off-by: sBouzols <[email protected]>
1 parent 45a0228 commit c189418

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

src/components/app-wrapper.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
importParamsFr,
4444
exportParamsEn,
4545
exportParamsFr,
46+
NotificationsProvider,
4647
} from '@gridsuite/commons-ui';
4748
import { IntlProvider } from 'react-intl';
4849
import { BrowserRouter } from 'react-router-dom';
@@ -79,6 +80,7 @@ import {
7980
MAP_BASEMAP_CARTO,
8081
MAP_BASEMAP_CARTO_NOLABEL,
8182
} from '../utils/config-params';
83+
import useNotificationsUrlGenerator from 'hooks/use-notifications-url-generator';
8284

8385
let lightTheme = createTheme({
8486
components: {
@@ -327,6 +329,8 @@ const AppWrapperWithRedux = () => {
327329

328330
const theme = useSelector((state) => state[PARAM_THEME]);
329331

332+
const urlMapper = useNotificationsUrlGenerator();
333+
330334
return (
331335
<IntlProvider locale={computedLanguage} messages={messages[computedLanguage]}>
332336
<BrowserRouter basename={basename}>
@@ -335,7 +339,9 @@ const AppWrapperWithRedux = () => {
335339
<SnackbarProvider hideIconVariant={false}>
336340
<CssBaseline />
337341
<CardErrorBoundary>
338-
<App />
342+
<NotificationsProvider urls={urlMapper}>
343+
<App />
344+
</NotificationsProvider>
339345
</CardErrorBoundary>
340346
</SnackbarProvider>
341347
</ThemeProvider>

src/components/app.jsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getPreLoginPath,
2121
initializeAuthenticationProd,
2222
useSnackMessage,
23+
useNotificationsListener,
2324
} from '@gridsuite/commons-ui';
2425
import PageNotFound from './page-not-found';
2526
import { FormattedMessage } from 'react-intl';
@@ -55,7 +56,6 @@ import { getComputedLanguage } from '../utils/language';
5556
import AppTopBar from './app-top-bar';
5657
import { StudyContainer } from './study-container';
5758
import { fetchValidateUser } from '../services/user-admin';
58-
import { connectNotificationsWsUpdateConfig } from '../services/config-notification';
5959
import { fetchConfigParameter, fetchConfigParameters } from '../services/config';
6060
import { fetchDefaultParametersValues, fetchIdpSettings } from '../services/utils';
6161
import { getOptionalServices } from '../services/study';
@@ -87,6 +87,7 @@ import {
8787
setOptionalServices,
8888
setParamsLoaded,
8989
} from '../redux/actions';
90+
import { NOTIFICATIONS_URL_KEYS } from './utils/notificationsProvider-utils';
9091

9192
const noUserManager = { instance: null, error: null };
9293

@@ -270,10 +271,8 @@ const App = () => {
270271
[dispatch, tablesNamesIndexes, tablesDefinitionIndexes]
271272
);
272273

273-
const connectNotificationsUpdateConfig = useCallback(() => {
274-
const ws = connectNotificationsWsUpdateConfig();
275-
276-
ws.onmessage = function (event) {
274+
const updateConfig = useCallback(
275+
(event) => {
277276
let eventData = JSON.parse(event.data);
278277
if (eventData.headers && eventData.headers['parameterName']) {
279278
fetchConfigParameter(eventData.headers['parameterName'])
@@ -290,12 +289,13 @@ const App = () => {
290289
})
291290
);
292291
}
293-
};
294-
ws.onerror = function (event) {
295-
console.error('Unexpected Notification WebSocket error', event);
296-
};
297-
return ws;
298-
}, [updateParams, snackError, dispatch]);
292+
},
293+
[dispatch, snackError, updateParams]
294+
);
295+
296+
useNotificationsListener(NOTIFICATIONS_URL_KEYS.CONFIG, {
297+
listenerCallbackMessage: updateConfig,
298+
});
299299

300300
// Can't use lazy initializer because useRouteMatch is a hook
301301
const [initialMatchSilentRenewCallbackUrl] = useState(
@@ -419,13 +419,8 @@ const App = () => {
419419
headerId: 'paramsRetrievingError',
420420
})
421421
);
422-
423-
const ws = connectNotificationsUpdateConfig();
424-
return function () {
425-
ws.close();
426-
};
427422
}
428-
}, [user, dispatch, updateParams, connectNotificationsUpdateConfig, snackError]);
423+
}, [user, dispatch, updateParams, snackError]);
429424

430425
const onChangeTab = useCallback((newTabIndex) => {
431426
setTabIndex(newTabIndex);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Copyright (c) 2024, 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+
export enum NOTIFICATIONS_URL_KEYS {
8+
CONFIG = 'CONFIG',
9+
}
10+
11+
export const PREFIX_CONFIG_NOTIFICATION_WS = import.meta.env.VITE_WS_GATEWAY + '/config-notification';
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) 2024, 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+
import { NOTIFICATIONS_URL_KEYS, PREFIX_CONFIG_NOTIFICATION_WS } from 'components/utils/notificationsProvider-utils';
8+
import { useCallback, useMemo } from 'react';
9+
import { useSelector } from 'react-redux';
10+
import { AppState } from 'redux/reducer';
11+
import { getUrlWithToken, getWsBase } from 'services/utils';
12+
import { APP_NAME } from 'utils/config-params';
13+
14+
const useNotificationsUrlGenerator = () => {
15+
// The websocket API doesn't allow relative urls
16+
const wsBase = getWsBase();
17+
const tokenId = useSelector((state: AppState) => state.user?.id_token);
18+
// Add params to Url
19+
const urlParams = useCallback((mapper: Record<string, string>) => {
20+
const usp = new URLSearchParams();
21+
Object.entries(mapper).forEach(([key, value]) => {
22+
usp.append(key, value);
23+
});
24+
return usp;
25+
}, []);
26+
27+
const urlMapper = useMemo(() => {
28+
// return a mapper with NOTIFICATIONS_URL_KEYS and undefined value if URL is not yet buildable (tokenId)
29+
// it will be used to register listeners as soon as possible.
30+
let mapper: Object = {
31+
[NOTIFICATIONS_URL_KEYS.CONFIG]: tokenId
32+
? getUrlWithToken(
33+
`${wsBase}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?${urlParams({ appName: APP_NAME })}`
34+
)
35+
: undefined,
36+
};
37+
return mapper;
38+
}, [wsBase, urlParams, tokenId]);
39+
return urlMapper;
40+
};
41+
42+
export default useNotificationsUrlGenerator;

0 commit comments

Comments
 (0)