Skip to content

Commit 250b650

Browse files
committed
fix: correctly set count when using delayNotificationState
Signed-off-by: Adam Setch <[email protected]>
1 parent fef606a commit 250b650

File tree

8 files changed

+57
-43
lines changed

8 files changed

+57
-43
lines changed

src/renderer/components/Sidebar.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type FC, useContext, useMemo } from 'react';
1+
import { type FC, useContext } from 'react';
22
import { useLocation, useNavigate } from 'react-router-dom';
33

44
import {
@@ -23,7 +23,7 @@ import {
2323
openGitHubPulls,
2424
} from '../utils/links';
2525
import { hasActiveFilters } from '../utils/notifications/filters/filter';
26-
import { getNotificationCount } from '../utils/notifications/notifications';
26+
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
2727
import { LogoIcon } from './icons/LogoIcon';
2828

2929
export const Sidebar: FC = () => {
@@ -65,9 +65,7 @@ export const Sidebar: FC = () => {
6565
fetchNotifications();
6666
};
6767

68-
const notificationsCount = useMemo(() => {
69-
return getNotificationCount(notifications);
70-
}, [notifications]);
68+
const notificationsCount = getUnreadNotificationCount(notifications);
7169

7270
const sidebarButtonStyle = { color: 'white' };
7371

src/renderer/context/App.test.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ describe('renderer/context/App.tsx', () => {
3838
});
3939

4040
describe('notification methods', () => {
41-
const getNotificationCountMock = jest.spyOn(
42-
notifications,
43-
'getNotificationCount',
44-
);
45-
getNotificationCountMock.mockReturnValue(1);
41+
const setTrayIconColorAndTitleMock = jest
42+
.spyOn(notifications, 'setTrayIconColorAndTitle')
43+
.mockImplementation(jest.fn());
4644

4745
const fetchNotificationsMock = jest.fn();
4846
const markNotificationsAsReadMock = jest.fn();
4947
const markNotificationsAsDoneMock = jest.fn();
5048
const unsubscribeNotificationMock = jest.fn();
49+
const setTrayIconColorAndTitle = jest.fn();
5150

5251
const mockDefaultState = {
5352
auth: { accounts: [] },
@@ -60,6 +59,7 @@ describe('renderer/context/App.tsx', () => {
6059
markNotificationsAsRead: markNotificationsAsReadMock,
6160
markNotificationsAsDone: markNotificationsAsDoneMock,
6261
unsubscribeNotification: unsubscribeNotificationMock,
62+
setTrayIconColorAndTitle,
6363
});
6464
});
6565

@@ -144,6 +144,7 @@ describe('renderer/context/App.tsx', () => {
144144
mockDefaultState,
145145
[mockSingleNotification],
146146
);
147+
expect(setTrayIconColorAndTitleMock).toHaveBeenCalledTimes(1);
147148
});
148149

149150
it('should call markNotificationsAsDone', async () => {
@@ -169,6 +170,7 @@ describe('renderer/context/App.tsx', () => {
169170
mockDefaultState,
170171
[mockSingleNotification],
171172
);
173+
expect(setTrayIconColorAndTitleMock).toHaveBeenCalledTimes(1);
172174
});
173175

174176
it('should call unsubscribeNotification', async () => {
@@ -194,6 +196,7 @@ describe('renderer/context/App.tsx', () => {
194196
mockDefaultState,
195197
mockSingleNotification,
196198
);
199+
expect(setTrayIconColorAndTitleMock).toHaveBeenCalledTimes(1);
197200
});
198201
});
199202

src/renderer/context/App.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ import {
4747
setKeyboardShortcut,
4848
setUseAlternateIdleIcon,
4949
setUseUnreadActiveIcon,
50-
updateTrayColor,
51-
updateTrayTitle,
5250
} from '../utils/comms';
53-
import { getNotificationCount } from '../utils/notifications/notifications';
51+
import { setTrayIconColorAndTitle } from '../utils/notifications/notifications';
5452
import { clearState, loadState, saveState } from '../utils/storage';
5553
import {
5654
DEFAULT_DAY_COLOR_SCHEME,
@@ -164,19 +162,11 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
164162
}
165163
}, Constants.REFRESH_ACCOUNTS_INTERVAL_MS);
166164

165+
// biome-ignore lint/correctness/useExhaustiveDependencies: We also want to update the tray on setting changes
167166
useEffect(() => {
168-
const count = getNotificationCount(notifications);
169-
170-
let title = '';
171-
if (settings.showNotificationsCountInTray && count > 0) {
172-
title = count.toString();
173-
}
174-
175167
setUseUnreadActiveIcon(settings.useUnreadActiveIcon);
176168
setUseAlternateIdleIcon(settings.useAlternateIdleIcon);
177-
178-
updateTrayColor(count);
179-
updateTrayTitle(title);
169+
setTrayIconColorAndTitle(notifications, settings);
180170
}, [
181171
settings.showNotificationsCountInTray,
182172
settings.useUnreadActiveIcon,

src/renderer/hooks/useNotifications.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { rendererLogError } from '../utils/logger';
1919
import { triggerNativeNotifications } from '../utils/notifications/native';
2020
import {
2121
getAllNotifications,
22-
setTrayIconColor,
22+
setTrayIconColorAndTitle,
2323
} from '../utils/notifications/notifications';
2424
import { removeNotifications } from '../utils/notifications/remove';
2525

@@ -34,9 +34,13 @@ function markNotificationsAsReadLocally(targetNotifications: Notification[]) {
3434
}
3535

3636
interface NotificationsState {
37+
status: Status;
38+
globalError: GitifyError;
39+
3740
notifications: AccountNotifications[];
38-
removeAccountNotifications: (account: Account) => Promise<void>;
3941
fetchNotifications: (state: GitifyState) => Promise<void>;
42+
removeAccountNotifications: (account: Account) => Promise<void>;
43+
4044
markNotificationsAsRead: (
4145
state: GitifyState,
4246
notifications: Notification[],
@@ -49,8 +53,6 @@ interface NotificationsState {
4953
state: GitifyState,
5054
notification: Notification,
5155
) => Promise<void>;
52-
status: Status;
53-
globalError: GitifyError;
5456
}
5557

5658
export const useNotifications = (): NotificationsState => {
@@ -70,7 +72,7 @@ export const useNotifications = (): NotificationsState => {
7072
);
7173

7274
setNotifications(updatedNotifications);
73-
setTrayIconColor(updatedNotifications);
75+
7476
setStatus('success');
7577
},
7678
[notifications],
@@ -109,6 +111,7 @@ export const useNotifications = (): NotificationsState => {
109111

110112
setNotifications(fetchedNotifications);
111113
triggerNativeNotifications(notifications, fetchedNotifications, state);
114+
112115
setStatus('success');
113116
},
114117
[notifications],
@@ -138,7 +141,7 @@ export const useNotifications = (): NotificationsState => {
138141
markNotificationsAsReadLocally(readNotifications);
139142

140143
setNotifications(updatedNotifications);
141-
setTrayIconColor(updatedNotifications);
144+
setTrayIconColorAndTitle(updatedNotifications, state.settings);
142145
} catch (err) {
143146
rendererLogError(
144147
'markNotificationsAsRead',
@@ -180,7 +183,7 @@ export const useNotifications = (): NotificationsState => {
180183
markNotificationsAsReadLocally(doneNotifications);
181184

182185
setNotifications(updatedNotifications);
183-
setTrayIconColor(updatedNotifications);
186+
setTrayIconColorAndTitle(updatedNotifications, state.settings);
184187
} catch (err) {
185188
rendererLogError(
186189
'markNotificationsAsDone',

src/renderer/routes/Notifications.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { AccountNotifications } from '../components/notifications/AccountNotific
77
import { Oops } from '../components/Oops';
88
import { AppContext } from '../context/App';
99
import { getAccountUUID } from '../utils/auth/utils';
10-
import { getNotificationCount } from '../utils/notifications/notifications';
10+
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
1111

1212
export const NotificationsRoute: FC = () => {
1313
const { notifications, status, globalError, settings } =
@@ -24,7 +24,7 @@ export const NotificationsRoute: FC = () => {
2424
);
2525

2626
const hasNotifications = useMemo(
27-
() => getNotificationCount(notifications) > 0,
27+
() => getUnreadNotificationCount(notifications) > 0,
2828
[notifications],
2929
);
3030

src/renderer/utils/notifications/native.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { AccountNotifications, GitifyState } from '../../types';
44
import type { Notification } from '../../typesGitHub';
55
import { getAccountUUID } from '../auth/utils';
66
import { generateGitHubWebUrl } from '../helpers';
7-
import { setTrayIconColor } from './notifications';
7+
import { setTrayIconColorAndTitle } from './notifications';
88

99
export const triggerNativeNotifications = (
1010
previousNotifications: AccountNotifications[],
@@ -35,7 +35,7 @@ export const triggerNativeNotifications = (
3535
return accountNewNotifications;
3636
});
3737

38-
setTrayIconColor(newNotifications);
38+
setTrayIconColorAndTitle(newNotifications, state.settings);
3939

4040
// If there are no new notifications just stop there
4141
if (!diffNotifications.length) {

src/renderer/utils/notifications/notifications.test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import { mockSettings } from '../../__mocks__/state-mocks';
77
import type { Link } from '../../types';
88
import type { Repository } from '../../typesGitHub';
99
import * as logger from '../../utils/logger';
10-
import { enrichNotification, getNotificationCount } from './notifications';
10+
import {
11+
enrichNotification,
12+
getUnreadNotificationCount,
13+
} from './notifications';
1114

1215
describe('renderer/utils/notifications/notifications.ts', () => {
1316
beforeEach(() => {
@@ -20,8 +23,8 @@ describe('renderer/utils/notifications/notifications.ts', () => {
2023
jest.clearAllMocks();
2124
});
2225

23-
it('getNotificationCount', () => {
24-
const result = getNotificationCount(mockSingleAccountNotifications);
26+
it('getUnreadNotificationCount', () => {
27+
const result = getUnreadNotificationCount(mockSingleAccountNotifications);
2528

2629
expect(result).toBe(1);
2730
});

src/renderer/utils/notifications/notifications.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
import type { GitifySubject, Notification } from '../../typesGitHub';
77
import { listNotificationsForAuthenticatedUser } from '../api/client';
88
import { determineFailureType } from '../api/errors';
9-
import { updateTrayColor } from '../comms';
9+
import { updateTrayColor, updateTrayTitle } from '../comms';
1010
import { rendererLogError, rendererLogWarn } from '../logger';
1111
import {
1212
filterBaseNotifications,
@@ -15,15 +15,32 @@ import {
1515
import { getFlattenedNotificationsByRepo } from './group';
1616
import { createNotificationHandler } from './handlers';
1717

18-
export function setTrayIconColor(notifications: AccountNotifications[]) {
19-
const allNotificationsCount = getNotificationCount(notifications);
18+
/**
19+
* Sets the tray icon color and title based on the number of unread notifications.
20+
* @param notifications
21+
* @param settings
22+
*/
23+
export function setTrayIconColorAndTitle(
24+
notifications: AccountNotifications[],
25+
settings: SettingsState,
26+
) {
27+
const totalUnreadNotifications = getUnreadNotificationCount(notifications);
28+
29+
let title = '';
30+
if (settings.showNotificationsCountInTray && totalUnreadNotifications > 0) {
31+
title = totalUnreadNotifications.toString();
32+
}
2033

21-
updateTrayColor(allNotificationsCount);
34+
updateTrayColor(totalUnreadNotifications);
35+
updateTrayTitle(title);
2236
}
2337

24-
export function getNotificationCount(notifications: AccountNotifications[]) {
38+
export function getUnreadNotificationCount(
39+
notifications: AccountNotifications[],
40+
) {
2541
return notifications.reduce(
26-
(sum, account) => sum + account.notifications.length,
42+
(sum, account) =>
43+
sum + account.notifications.filter((n) => n.unread === true).length,
2744
0,
2845
);
2946
}

0 commit comments

Comments
 (0)