@@ -16,7 +16,7 @@ import {
16
16
} from 'react' ;
17
17
import { capitalize , Tab , Tabs , useTheme } from '@mui/material' ;
18
18
import { Groups , ManageAccounts , PeopleAlt , NotificationImportant } from '@mui/icons-material' ;
19
- import { fetchAppsMetadata , logout , Metadata , TopBar } from '@gridsuite/commons-ui' ;
19
+ import { fetchAppsMetadata , logout , Metadata , TopBar , useNotificationsListener } from '@gridsuite/commons-ui' ;
20
20
import { useParameterState } from '../parameters' ;
21
21
import { APP_NAME , PARAM_LANGUAGE , PARAM_THEME } from '../../utils/config-params' ;
22
22
import { NavLink , type To , useMatches , useNavigate } from 'react-router' ;
@@ -29,6 +29,7 @@ import AppPackage from '../../../package.json';
29
29
import { AppState } from '../../redux/reducer' ;
30
30
import { AppDispatch } from '../../redux/store' ;
31
31
import { MainPaths } from '../../routes/utils' ;
32
+ import { NOTIFICATIONS_URL_KEYS } from '../../utils/notifications-provider' ;
32
33
33
34
const tabs = new Map < MainPaths , ReactElement > ( [
34
35
[
@@ -110,6 +111,33 @@ const AppTopBar: FunctionComponent = () => {
110
111
const [ languageLocal , handleChangeLanguage ] = useParameterState ( PARAM_LANGUAGE ) ;
111
112
112
113
const [ appsAndUrls , setAppsAndUrls ] = useState < Metadata [ ] > ( [ ] ) ;
114
+
115
+ const [ announcementInfos , setAnnouncementInfos ] = useState < AnnouncementProps | null > ( null ) ;
116
+
117
+ useNotificationsListener ( NOTIFICATIONS_URL_KEYS . GLOBAL_CONFIG , {
118
+ listenerCallbackMessage : ( event ) => {
119
+ const eventData = JSON . parse ( event . data ) ;
120
+ if ( eventData . headers . messageType === 'announcement' ) {
121
+ if (
122
+ announcementInfos != null &&
123
+ announcementInfos . announcementId === eventData . headers . announcementId
124
+ ) {
125
+ // If we receive a notification for an announcement that we already received we ignore it
126
+ return ;
127
+ }
128
+ const announcement = {
129
+ announcementId : eventData . headers . announcementId ,
130
+ message : eventData . payload ,
131
+ severity : eventData . headers . severity ,
132
+ duration : eventData . headers . duration ,
133
+ } as AnnouncementProps ;
134
+ setAnnouncementInfos ( announcement ) ;
135
+ } else if ( eventData . headers . messageType === 'cancelAnnouncement' ) {
136
+ setAnnouncementInfos ( null ) ;
137
+ }
138
+ } ,
139
+ } ) ;
140
+
113
141
useEffect ( ( ) => {
114
142
if ( user !== null ) {
115
143
fetchAppsMetadata ( ) . then ( ( res ) => {
@@ -136,6 +164,7 @@ const AppTopBar: FunctionComponent = () => {
136
164
onLanguageClick = { handleChangeLanguage }
137
165
language = { languageLocal }
138
166
developerMode = { false } // TODO: set as optional in commons-ui
167
+ announcementInfos = { announcementInfos }
139
168
>
140
169
< Tabs
141
170
component = "nav"
0 commit comments