Skip to content

Commit

Permalink
refactor: extract notification count into reusable function (#941)
Browse files Browse the repository at this point in the history
* refactor: extract notification count into reusable function

* refactor: keep useMemo

* refactor: directly return
  • Loading branch information
setchy committed Apr 1, 2024
1 parent 9d8c14f commit 7989ad6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 =
Expand Down
10 changes: 5 additions & 5 deletions src/routes/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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],
Expand Down
12 changes: 8 additions & 4 deletions src/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down

0 comments on commit 7989ad6

Please sign in to comment.