@@ -9,6 +9,7 @@ import React, { FC, useContext, useEffect, useState } from 'react';
99import { combineLatest , of , timer } from 'rxjs' ;
1010import { catchError , switchMap , map , tap } from 'rxjs/operators' ;
1111import moment from 'moment' ;
12+ import { isPopulatedObject } from '@kbn/ml-is-populated-object' ;
1213import { useMlKibana } from '../kibana' ;
1314import { useStorage } from '../storage' ;
1415import { ML_NOTIFICATIONS_LAST_CHECKED_AT } from '../../../../common/types/storage' ;
@@ -17,6 +18,8 @@ import type { NotificationsCountResponse } from '../../../../common/types/notifi
1718
1819const NOTIFICATIONS_CHECK_INTERVAL = 60000 ;
1920
21+ const defaultCounts = { info : 0 , error : 0 , warning : 0 } ;
22+
2023export const MlNotificationsContext = React . createContext < {
2124 notificationsCounts : NotificationsCountResponse ;
2225 /** Timestamp of the latest notification checked by the user */
@@ -25,7 +28,7 @@ export const MlNotificationsContext = React.createContext<{
2528 latestRequestedAt : number | null ;
2629 setLastCheckedAt : ( v : number ) => void ;
2730} > ( {
28- notificationsCounts : { info : 0 , error : 0 , warning : 0 } ,
31+ notificationsCounts : defaultCounts ,
2932 lastCheckedAt : null ,
3033 latestRequestedAt : null ,
3134 setLastCheckedAt : ( ) => { } ,
@@ -43,11 +46,8 @@ export const MlNotificationsContextProvider: FC = ({ children }) => {
4346
4447 /** Holds the value used for the actual request */
4548 const [ latestRequestedAt , setLatestRequestedAt ] = useState < number | null > ( null ) ;
46- const [ notificationsCounts , setNotificationsCounts ] = useState < NotificationsCountResponse > ( {
47- info : 0 ,
48- error : 0 ,
49- warning : 0 ,
50- } ) ;
49+ const [ notificationsCounts , setNotificationsCounts ] =
50+ useState < NotificationsCountResponse > ( defaultCounts ) ;
5151
5252 useEffect ( function startPollingNotifications ( ) {
5353 const subscription = combineLatest ( [ lastCheckedAt$ , timer ( 0 , NOTIFICATIONS_CHECK_INTERVAL ) ] )
@@ -68,7 +68,7 @@ export const MlNotificationsContextProvider: FC = ({ children }) => {
6868 } )
6969 )
7070 . subscribe ( ( response ) => {
71- setNotificationsCounts ( response ) ;
71+ setNotificationsCounts ( isPopulatedObject ( response ) ? response : defaultCounts ) ;
7272 } ) ;
7373
7474 return ( ) => {
0 commit comments