From 23f300850091940a01bc5807cbd830540adb99ef Mon Sep 17 00:00:00 2001 From: Ali <83188384+testA113@users.noreply.github.com> Date: Fri, 17 Feb 2023 12:51:00 +1300 Subject: [PATCH] chore(notifications):improve performance [EE-4815] (#8475) * chore(notifications):improve performance [EE-4815] Co-authored-by: testa113 --- .../PageHeader/NotificationsMenu.tsx | 24 +++----------- .../notifications/notifications-store.ts | 32 +++++++++++++------ 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/app/react/components/PageHeader/NotificationsMenu.tsx b/app/react/components/PageHeader/NotificationsMenu.tsx index c8ded766b..69b0f35e7 100644 --- a/app/react/components/PageHeader/NotificationsMenu.tsx +++ b/app/react/components/PageHeader/NotificationsMenu.tsx @@ -7,7 +7,6 @@ import { } from '@reach/menu-button'; import { UISrefProps, useSref } from '@uirouter/react'; import Moment from 'moment'; -import { useEffect, useState } from 'react'; import { useStore } from 'zustand'; import { AlertCircle, Bell, CheckCircle, Trash2 } from 'lucide-react'; @@ -35,23 +34,6 @@ export function NotificationsMenu() { (state) => state.userNotifications[user.Id] ); - if (userNotifications && userNotifications.length > 1) { - userNotifications.sort( - (a, b) => - new Date(b.timeStamp).getTime() - new Date(a.timeStamp).getTime() - ); - } - - const [badge, setBadge] = useState(false); - - useEffect(() => { - if (userNotifications?.length > 0) { - setBadge(true); - } else { - setBadge(false); - } - }, [userNotifications]); - return ( - + 0 ? notificationStyles.badge : '' + } + /> diff --git a/app/react/portainer/notifications/notifications-store.ts b/app/react/portainer/notifications/notifications-store.ts index 62c6aa064..266eb99e9 100644 --- a/app/react/portainer/notifications/notifications-store.ts +++ b/app/react/portainer/notifications/notifications-store.ts @@ -2,11 +2,12 @@ import create from 'zustand/vanilla'; import { persist } from 'zustand/middleware'; import { keyBuilder } from '@/react/hooks/useLocalStorage'; +import { UserId } from '@/portainer/users/types'; import { ToastNotification } from './types'; interface NotificationsState { - userNotifications: Record; + userNotifications: Record; addNotification: (userId: number, notification: ToastNotification) => void; removeNotification: (userId: number, notificationId: string) => void; removeNotifications: (userId: number, notifications: string[]) => void; @@ -18,15 +19,26 @@ export const notificationsStore = create()( (set) => ({ userNotifications: {}, addNotification: (userId: number, notification: ToastNotification) => { - set((state) => ({ - userNotifications: { - ...state.userNotifications, - [userId]: [ - ...(state.userNotifications[userId] || []), - notification, - ], - }, - })); + set((state) => { + const currentUserNotifications = + state.userNotifications[userId] || []; + // keep the new notification at the start of the list, so sorting by newest time isn't required + const newUserNotifications = [ + notification, + ...currentUserNotifications, + ]; + const maxNotifications = 50; + const reducedNotifications = newUserNotifications.slice( + 0, + maxNotifications + ); + return { + userNotifications: { + ...state.userNotifications, + [userId]: reducedNotifications, + }, + }; + }); }, removeNotification: (userId: number, notificationId: string) => { set((state) => ({