Skip to content

Commit

Permalink
fix(notification): fix potential infinite effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Thuan Vo committed Jun 2, 2023
1 parent 640f2ad commit 156b6a4
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/app/Notifications/NotificationCenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ import * as React from 'react';
import { combineLatest } from 'rxjs';
import { Notification, NotificationsContext } from './Notifications';

const countUnreadNotifications = (notifications: Notification[]) => {
return notifications.filter((n) => !n.read).length;
};

export interface NotificationCenterProps {
onClose: () => void;
}
Expand All @@ -77,17 +81,12 @@ export const NotificationCenter: React.FC<NotificationCenterProps> = (props) =>

const [totalUnreadNotificationsCount, setTotalUnreadNotificationsCount] = React.useState(0);
const [isHeaderDropdownOpen, setHeaderDropdownOpen] = React.useState(false);
const PROBLEMS_CATEGORY_IDX = 2;
const [drawerCategories, setDrawerCategories] = React.useState([
{ title: 'Completed Actions', isExpanded: true, notifications: [] as Notification[], unreadCount: 0 },
{ title: 'Cryostat Status', isExpanded: false, notifications: [] as Notification[], unreadCount: 0 },
{ title: 'Problems', isExpanded: false, notifications: [] as Notification[], unreadCount: 0 },
{ title: 'Problems', isExpanded: true, notifications: [] as Notification[], unreadCount: 0 },
] as NotificationDrawerCategory[]);

const countUnreadNotifications = (notifications: Notification[]) => {
return notifications.filter((n) => !n.read).length;
};

React.useEffect(() => {
addSubscription(
combineLatest([
Expand All @@ -104,7 +103,7 @@ export const NotificationCenter: React.FC<NotificationCenterProps> = (props) =>
});
})
);
}, [addSubscription, context, context.notifications, setDrawerCategories]);
}, [addSubscription, context, setDrawerCategories]);

React.useEffect(() => {
addSubscription(
Expand All @@ -130,20 +129,6 @@ export const NotificationCenter: React.FC<NotificationCenterProps> = (props) =>
[setDrawerCategories]
);

// Expands the Problems tab when unread errors/warnings are present
React.useEffect(() => {
if (drawerCategories[PROBLEMS_CATEGORY_IDX].unreadCount === 0) {
return;
}

setDrawerCategories((drawerCategories) => {
return drawerCategories.map((category: NotificationDrawerCategory, idx) => {
category.isExpanded = idx === PROBLEMS_CATEGORY_IDX;
return category;
});
});
}, [setDrawerCategories, drawerCategories]);

const handleMarkAllRead = React.useCallback(() => {
context.markAllRead();
}, [context]);
Expand Down

0 comments on commit 156b6a4

Please sign in to comment.