-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor config notifications management with NotificationsProvider (#…
…2425) * refactor(): Replace config notifications management with NotificationsProvider from commons-ui Signed-off-by: sBouzols <sylvain.bouzols@gmail.com>
- Loading branch information
Showing
4 changed files
with
72 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
export enum NOTIFICATIONS_URL_KEYS { | ||
CONFIG = 'CONFIG', | ||
} | ||
|
||
export const PREFIX_CONFIG_NOTIFICATION_WS = import.meta.env.VITE_WS_GATEWAY + '/config-notification'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
import { NOTIFICATIONS_URL_KEYS, PREFIX_CONFIG_NOTIFICATION_WS } from 'components/utils/notificationsProvider-utils'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { AppState } from 'redux/reducer'; | ||
import { getUrlWithToken, getWsBase } from 'services/utils'; | ||
import { APP_NAME } from 'utils/config-params'; | ||
|
||
const useNotificationsUrlGenerator = () => { | ||
// The websocket API doesn't allow relative urls | ||
const wsBase = getWsBase(); | ||
const tokenId = useSelector((state: AppState) => state.user?.id_token); | ||
// Add params to Url | ||
const urlParams = useCallback((mapper: Record<string, string>) => { | ||
const usp = new URLSearchParams(); | ||
Object.entries(mapper).forEach(([key, value]) => { | ||
usp.append(key, value); | ||
}); | ||
return usp; | ||
}, []); | ||
|
||
const urlMapper = useMemo(() => { | ||
// return a mapper with NOTIFICATIONS_URL_KEYS and undefined value if URL is not yet buildable (tokenId) | ||
// it will be used to register listeners as soon as possible. | ||
let mapper: Object = { | ||
[NOTIFICATIONS_URL_KEYS.CONFIG]: tokenId | ||
? getUrlWithToken( | ||
`${wsBase}${PREFIX_CONFIG_NOTIFICATION_WS}/notify?${urlParams({ appName: APP_NAME })}` | ||
) | ||
: undefined, | ||
}; | ||
return mapper; | ||
}, [wsBase, urlParams, tokenId]); | ||
return urlMapper; | ||
}; | ||
|
||
export default useNotificationsUrlGenerator; |