Skip to content

Commit 7989ad6

Browse files
authored
refactor: extract notification count into reusable function (#941)
* refactor: extract notification count into reusable function * refactor: keep useMemo * refactor: directly return
1 parent 9d8c14f commit 7989ad6

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/components/Sidebar.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ipcRenderer } from 'electron';
88
import React, { useCallback, useContext, useMemo } from 'react';
99
import { useNavigate, useLocation } from 'react-router-dom';
1010

11+
import { getNotificationCount } from '../utils/notifications';
1112
import { Logo } from '../components/Logo';
1213
import { AppContext } from '../context/App';
1314
import { Constants } from '../utils/constants';
@@ -33,10 +34,7 @@ export const Sidebar: React.FC = () => {
3334
}, []);
3435

3536
const notificationsCount = useMemo(() => {
36-
return notifications.reduce(
37-
(memo, account) => memo + account.notifications.length,
38-
0,
39-
);
37+
return getNotificationCount(notifications);
4038
}, [notifications]);
4139

4240
const footerButtonClasses =

src/routes/Notifications.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AccountNotifications } from '../components/AccountNotifications';
44
import { AllRead } from '../components/AllRead';
55
import { AppContext } from '../context/App';
66
import { Oops } from '../components/Oops';
7+
import { getNotificationCount } from '../utils/notifications';
78

89
export const NotificationsRoute: React.FC = (props) => {
910
const { notifications, requestFailed } = useContext(AppContext);
@@ -12,11 +13,10 @@ export const NotificationsRoute: React.FC = (props) => {
1213
() => notifications.length > 1,
1314
[notifications],
1415
);
15-
const notificationsCount = useMemo(
16-
() =>
17-
notifications.reduce((memo, acc) => memo + acc.notifications.length, 0),
18-
[notifications],
19-
);
16+
const notificationsCount = useMemo(() => {
17+
return getNotificationCount(notifications);
18+
}, [notifications]);
19+
2020
const hasNotifications = useMemo(
2121
() => notificationsCount > 0,
2222
[notificationsCount],

src/utils/notifications.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ import { Notification } from '../typesGithub';
66
import { AccountNotifications, SettingsState, AuthState } from '../types';
77

88
export const setTrayIconColor = (notifications: AccountNotifications[]) => {
9-
const allNotificationsCount = notifications.reduce(
10-
(memo, acc) => memo + acc.notifications.length,
11-
0,
12-
);
9+
const allNotificationsCount = getNotificationCount(notifications);
1310

1411
updateTrayIcon(allNotificationsCount);
1512
};
1613

14+
export function getNotificationCount(notifications: AccountNotifications[]) {
15+
return notifications.reduce(
16+
(memo, acc) => memo + acc.notifications.length,
17+
0,
18+
);
19+
}
20+
1721
export const triggerNativeNotifications = (
1822
previousNotifications: AccountNotifications[],
1923
newNotifications: AccountNotifications[],

0 commit comments

Comments
 (0)