From 30e4938b6eb13ee5bee1bfcb7d0ebf07a7985e98 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 30 Mar 2024 06:47:47 -0400 Subject: [PATCH 1/3] refactor: extract notification count into reusable function --- src/components/Sidebar.tsx | 10 +++------- src/routes/Notifications.tsx | 8 +++----- src/utils/notifications.ts | 10 ++++++++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 9b5e29a0c..8af0fa848 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -5,9 +5,10 @@ import { XCircleIcon, } from '@primer/octicons-react'; import { ipcRenderer } from 'electron'; -import React, { useCallback, useContext, useMemo } from 'react'; +import React, { useCallback, useContext } 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'; @@ -32,12 +33,7 @@ export const Sidebar: React.FC = () => { ipcRenderer.send('app-quit'); }, []); - const notificationsCount = useMemo(() => { - return notifications.reduce( - (memo, account) => memo + account.notifications.length, - 0, - ); - }, [notifications]); + const notificationsCount = getNotificationCount(notifications); const footerButtonClasses = 'flex justify-evenly items-center bg-transparent border-0 w-full text-sm text-white my-1 py-2 cursor-pointer hover:text-gray-500 focus:outline-none'; diff --git a/src/routes/Notifications.tsx b/src/routes/Notifications.tsx index da7a2f4d5..acf69c670 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,8 @@ 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 = getNotificationCount(notifications); + const hasNotifications = useMemo( () => notificationsCount > 0, [notificationsCount], diff --git a/src/utils/notifications.ts b/src/utils/notifications.ts index 67431832c..6935080f2 100644 --- a/src/utils/notifications.ts +++ b/src/utils/notifications.ts @@ -6,13 +6,19 @@ import { Notification } from '../typesGithub'; import { AccountNotifications, SettingsState, AuthState } from '../types'; export const setTrayIconColor = (notifications: AccountNotifications[]) => { + const allNotificationsCount = getNotificationCount(notifications); + + updateTrayIcon(allNotificationsCount); +}; + +export function getNotificationCount(notifications: AccountNotifications[]) { const allNotificationsCount = notifications.reduce( (memo, acc) => memo + acc.notifications.length, 0, ); - updateTrayIcon(allNotificationsCount); -}; + return allNotificationsCount; +} export const triggerNativeNotifications = ( previousNotifications: AccountNotifications[], From 97203821bb67870e21fe357799b95ee899effde4 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 1 Apr 2024 08:18:23 -0400 Subject: [PATCH 2/3] refactor: keep useMemo --- src/components/Sidebar.tsx | 6 ++++-- src/routes/Notifications.tsx | 4 +++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 8af0fa848..c71d47108 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -5,7 +5,7 @@ import { XCircleIcon, } from '@primer/octicons-react'; import { ipcRenderer } from 'electron'; -import React, { useCallback, useContext } from 'react'; +import React, { useCallback, useContext, useMemo } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { getNotificationCount } from '../utils/notifications'; @@ -33,7 +33,9 @@ export const Sidebar: React.FC = () => { ipcRenderer.send('app-quit'); }, []); - const notificationsCount = getNotificationCount(notifications); + const notificationsCount = useMemo(() => { + return getNotificationCount(notifications); + }, [notifications]); const footerButtonClasses = 'flex justify-evenly items-center bg-transparent border-0 w-full text-sm text-white my-1 py-2 cursor-pointer hover:text-gray-500 focus:outline-none'; diff --git a/src/routes/Notifications.tsx b/src/routes/Notifications.tsx index acf69c670..478f549a4 100644 --- a/src/routes/Notifications.tsx +++ b/src/routes/Notifications.tsx @@ -13,7 +13,9 @@ export const NotificationsRoute: React.FC = (props) => { () => notifications.length > 1, [notifications], ); - const notificationsCount = getNotificationCount(notifications); + const notificationsCount = useMemo(() => { + return getNotificationCount(notifications); + }, [notifications]); const hasNotifications = useMemo( () => notificationsCount > 0, From 289568214b22694afc0be9a655717f793222e789 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 1 Apr 2024 08:19:51 -0400 Subject: [PATCH 3/3] refactor: directly return --- src/utils/notifications.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/utils/notifications.ts b/src/utils/notifications.ts index 6935080f2..2f699123c 100644 --- a/src/utils/notifications.ts +++ b/src/utils/notifications.ts @@ -12,12 +12,10 @@ export const setTrayIconColor = (notifications: AccountNotifications[]) => { }; export function getNotificationCount(notifications: AccountNotifications[]) { - const allNotificationsCount = notifications.reduce( + return notifications.reduce( (memo, acc) => memo + acc.notifications.length, 0, ); - - return allNotificationsCount; } export const triggerNativeNotifications = (