diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 9b5e29a0c..c71d47108 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -8,6 +8,7 @@ import { ipcRenderer } from 'electron'; import React, { useCallback, useContext, useMemo } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; +import { getNotificationCount } from '../utils/notifications'; import { Logo } from '../components/Logo'; import { AppContext } from '../context/App'; import { Constants } from '../utils/constants'; @@ -33,10 +34,7 @@ export const Sidebar: React.FC = () => { }, []); const notificationsCount = useMemo(() => { - return notifications.reduce( - (memo, account) => memo + account.notifications.length, - 0, - ); + return getNotificationCount(notifications); }, [notifications]); const footerButtonClasses = diff --git a/src/routes/Notifications.tsx b/src/routes/Notifications.tsx index da7a2f4d5..478f549a4 100644 --- a/src/routes/Notifications.tsx +++ b/src/routes/Notifications.tsx @@ -4,6 +4,7 @@ import { AccountNotifications } from '../components/AccountNotifications'; import { AllRead } from '../components/AllRead'; import { AppContext } from '../context/App'; import { Oops } from '../components/Oops'; +import { getNotificationCount } from '../utils/notifications'; export const NotificationsRoute: React.FC = (props) => { const { notifications, requestFailed } = useContext(AppContext); @@ -12,11 +13,10 @@ export const NotificationsRoute: React.FC = (props) => { () => notifications.length > 1, [notifications], ); - const notificationsCount = useMemo( - () => - notifications.reduce((memo, acc) => memo + acc.notifications.length, 0), - [notifications], - ); + const notificationsCount = useMemo(() => { + return getNotificationCount(notifications); + }, [notifications]); + const hasNotifications = useMemo( () => notificationsCount > 0, [notificationsCount], diff --git a/src/utils/notifications.ts b/src/utils/notifications.ts index 67431832c..2f699123c 100644 --- a/src/utils/notifications.ts +++ b/src/utils/notifications.ts @@ -6,14 +6,18 @@ import { Notification } from '../typesGithub'; import { AccountNotifications, SettingsState, AuthState } from '../types'; export const setTrayIconColor = (notifications: AccountNotifications[]) => { - const allNotificationsCount = notifications.reduce( - (memo, acc) => memo + acc.notifications.length, - 0, - ); + const allNotificationsCount = getNotificationCount(notifications); updateTrayIcon(allNotificationsCount); }; +export function getNotificationCount(notifications: AccountNotifications[]) { + return notifications.reduce( + (memo, acc) => memo + acc.notifications.length, + 0, + ); +} + export const triggerNativeNotifications = ( previousNotifications: AccountNotifications[], newNotifications: AccountNotifications[],