@gridsuite/commons-ui 0.217.0 → 0.218.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect, useMemo } from "react";
|
|
2
|
+
import { useSyncExternalStore, useEffect, useMemo } from "react";
|
|
3
3
|
import ReconnectingWebSocket from "reconnecting-websocket";
|
|
4
4
|
import { NotificationsContext } from "./contexts/NotificationsContext.js";
|
|
5
5
|
import { useListenerManager } from "./hooks/useListenerManager.js";
|
|
6
|
-
import { getUserToken } from "../../redux/commonStore.js";
|
|
6
|
+
import { subscribeToUserState, getUser, getUserToken } from "../../redux/commonStore.js";
|
|
7
7
|
const DELAY_BEFORE_WEBSOCKET_CONNECTED = 12e3;
|
|
8
8
|
function isUrlDefined(tuple) {
|
|
9
9
|
return tuple[1] !== void 0;
|
|
@@ -23,15 +23,15 @@ function NotificationsProvider({ urls, children }) {
|
|
|
23
23
|
addListener: addListenerOnReopen,
|
|
24
24
|
removeListener: removeListenerOnReopen
|
|
25
25
|
} = useListenerManager(urls);
|
|
26
|
+
const isAuthenticated = useSyncExternalStore(subscribeToUserState, () => getUser() !== null);
|
|
26
27
|
useEffect(() => {
|
|
27
28
|
const token = getUserToken();
|
|
28
|
-
if (!token) {
|
|
29
|
+
if (!isAuthenticated || !token) {
|
|
29
30
|
console.info("Skipping Notification WebSockets: no user token available");
|
|
30
31
|
return void 0;
|
|
31
32
|
}
|
|
32
33
|
const connections = Object.entries(urls).filter(isUrlDefined).map(([urlKey, url]) => {
|
|
33
|
-
const
|
|
34
|
-
const rws = new ReconnectingWebSocket(urlWithToken, [], {
|
|
34
|
+
const rws = new ReconnectingWebSocket(() => appendTokenToUrl(url, getUserToken() ?? ""), [], {
|
|
35
35
|
// this option set the minimum duration being connected before reset the retry count to 0
|
|
36
36
|
minUptime: DELAY_BEFORE_WEBSOCKET_CONNECTED
|
|
37
37
|
});
|
|
@@ -49,7 +49,7 @@ function NotificationsProvider({ urls, children }) {
|
|
|
49
49
|
return rws;
|
|
50
50
|
});
|
|
51
51
|
return () => connections.forEach((c) => c.close());
|
|
52
|
-
}, [broadcastMessage, broadcastOnReopen, urls]);
|
|
52
|
+
}, [broadcastMessage, broadcastOnReopen, urls, isAuthenticated]);
|
|
53
53
|
const contextValue = useMemo(
|
|
54
54
|
() => ({
|
|
55
55
|
addListenerEvent: addListenerMessage,
|
package/dist/index.js
CHANGED
|
@@ -358,7 +358,7 @@ import { useSelectAppearance } from "./hooks/useSelectAppearance.js";
|
|
|
358
358
|
import { OptionalServicesStatus, useParametersBackend } from "./hooks/use-parameters-backend.js";
|
|
359
359
|
import { useGetLabelEquipmentTypes } from "./hooks/useGetLabelEquipmentTypes.js";
|
|
360
360
|
import { LOGOUT_ERROR, RESET_AUTHENTICATION_ROUTER_ERROR, SHOW_AUTH_INFO_LOGIN, SIGNIN_CALLBACK_ERROR, UNAUTHORIZED_USER_INFO, USER, USER_VALIDATION_ERROR, resetAuthenticationRouterError, setLoggedUser, setLogoutError, setShowAuthenticationRouterLogin, setSignInCallbackError, setUnauthorizedUserInfo, setUserValidationError } from "./redux/actions/authActions.js";
|
|
361
|
-
import { getUserToken, setCommonStore } from "./redux/commonStore.js";
|
|
361
|
+
import { getUser, getUserToken, setCommonStore, subscribeToUserState } from "./redux/commonStore.js";
|
|
362
362
|
import { cardErrorBoundaryEn } from "./translations/en/cardErrorBoundaryEn.js";
|
|
363
363
|
import { businessErrorsEn } from "./translations/en/businessErrorsEn.js";
|
|
364
364
|
import { commonButtonEn } from "./translations/en/commonButtonEn.js";
|
|
@@ -1209,6 +1209,7 @@ export {
|
|
|
1209
1209
|
getStudyUrlWithNodeUuid,
|
|
1210
1210
|
getStudyUrlWithNodeUuidAndRootNetworkUuid,
|
|
1211
1211
|
getSystemLanguage,
|
|
1212
|
+
getUser,
|
|
1212
1213
|
getUserToken,
|
|
1213
1214
|
getVoltageInitParameters,
|
|
1214
1215
|
getVoltageInitUrl,
|
|
@@ -1323,6 +1324,7 @@ export {
|
|
|
1323
1324
|
snackWithFallback,
|
|
1324
1325
|
standardTextField,
|
|
1325
1326
|
styles,
|
|
1327
|
+
subscribeToUserState,
|
|
1326
1328
|
substationCreationDtoToForm,
|
|
1327
1329
|
substationCreationEmptyFormData,
|
|
1328
1330
|
substationCreationFormSchema,
|
|
@@ -4,6 +4,7 @@ export type CommonStoreState = {
|
|
|
4
4
|
};
|
|
5
5
|
interface CommonStore {
|
|
6
6
|
getState(): CommonStoreState;
|
|
7
|
+
subscribe(listener: () => void): () => void;
|
|
7
8
|
}
|
|
8
9
|
/**
|
|
9
10
|
* Set a copy of the reference to the store to be able to access it from this library.
|
|
@@ -11,5 +12,7 @@ interface CommonStore {
|
|
|
11
12
|
* NB : temporary solution before refactoring the token management in the whole gridsuite stack.
|
|
12
13
|
*/
|
|
13
14
|
export declare function setCommonStore(store: CommonStore): void;
|
|
15
|
+
export declare function getUser(): User | null;
|
|
14
16
|
export declare function getUserToken(): string | undefined;
|
|
17
|
+
export declare function subscribeToUserState(listener: () => void): () => void;
|
|
15
18
|
export {};
|
|
@@ -2,10 +2,19 @@ let commonStore;
|
|
|
2
2
|
function setCommonStore(store) {
|
|
3
3
|
commonStore = store;
|
|
4
4
|
}
|
|
5
|
+
function getUser() {
|
|
6
|
+
return commonStore?.getState().user ?? null;
|
|
7
|
+
}
|
|
5
8
|
function getUserToken() {
|
|
6
9
|
return commonStore?.getState().user?.id_token;
|
|
7
10
|
}
|
|
11
|
+
function subscribeToUserState(listener) {
|
|
12
|
+
return commonStore?.subscribe(listener) ?? (() => {
|
|
13
|
+
});
|
|
14
|
+
}
|
|
8
15
|
export {
|
|
16
|
+
getUser,
|
|
9
17
|
getUserToken,
|
|
10
|
-
setCommonStore
|
|
18
|
+
setCommonStore,
|
|
19
|
+
subscribeToUserState
|
|
11
20
|
};
|
package/dist/redux/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LOGOUT_ERROR, RESET_AUTHENTICATION_ROUTER_ERROR, SHOW_AUTH_INFO_LOGIN, SIGNIN_CALLBACK_ERROR, UNAUTHORIZED_USER_INFO, USER, USER_VALIDATION_ERROR, resetAuthenticationRouterError, setLoggedUser, setLogoutError, setShowAuthenticationRouterLogin, setSignInCallbackError, setUnauthorizedUserInfo, setUserValidationError } from "./actions/authActions.js";
|
|
2
|
-
import { getUserToken, setCommonStore } from "./commonStore.js";
|
|
2
|
+
import { getUser, getUserToken, setCommonStore, subscribeToUserState } from "./commonStore.js";
|
|
3
3
|
export {
|
|
4
4
|
LOGOUT_ERROR,
|
|
5
5
|
RESET_AUTHENTICATION_ROUTER_ERROR,
|
|
@@ -8,6 +8,7 @@ export {
|
|
|
8
8
|
UNAUTHORIZED_USER_INFO,
|
|
9
9
|
USER,
|
|
10
10
|
USER_VALIDATION_ERROR,
|
|
11
|
+
getUser,
|
|
11
12
|
getUserToken,
|
|
12
13
|
resetAuthenticationRouterError,
|
|
13
14
|
setCommonStore,
|
|
@@ -16,5 +17,6 @@ export {
|
|
|
16
17
|
setShowAuthenticationRouterLogin,
|
|
17
18
|
setSignInCallbackError,
|
|
18
19
|
setUnauthorizedUserInfo,
|
|
19
|
-
setUserValidationError
|
|
20
|
+
setUserValidationError,
|
|
21
|
+
subscribeToUserState
|
|
20
22
|
};
|