Skip to content

Commit 6fffca8

Browse files
committed
fix empty counts
1 parent 1daee30 commit 6fffca8

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

x-pack/plugins/ml/public/application/contexts/ml/ml_notifications_context.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import React, { FC, useContext, useEffect, useState } from 'react';
99
import { combineLatest, of, timer } from 'rxjs';
1010
import { catchError, switchMap, map, tap } from 'rxjs/operators';
1111
import moment from 'moment';
12+
import { isPopulatedObject } from '@kbn/ml-is-populated-object';
1213
import { useMlKibana } from '../kibana';
1314
import { useStorage } from '../storage';
1415
import { ML_NOTIFICATIONS_LAST_CHECKED_AT } from '../../../../common/types/storage';
@@ -17,6 +18,8 @@ import type { NotificationsCountResponse } from '../../../../common/types/notifi
1718

1819
const NOTIFICATIONS_CHECK_INTERVAL = 60000;
1920

21+
const defaultCounts = { info: 0, error: 0, warning: 0 };
22+
2023
export 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

Comments
 (0)