Skip to content

Commit 91aeb28

Browse files
authored
fix: delay notification when all read (#2359)
Signed-off-by: Adam Setch <[email protected]>
1 parent 80d8b91 commit 91aeb28

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

src/renderer/components/Sidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const Sidebar: FC = () => {
3636
settings,
3737
auth,
3838
unreadCount,
39-
hasNotifications,
39+
hasUnreadNotifications,
4040
} = useContext(AppContext);
4141

4242
// We naively assume that the first account is the primary account for the purposes of our sidebar quick links
@@ -98,7 +98,7 @@ export const Sidebar: FC = () => {
9898
size="small"
9999
tooltipDirection="e"
100100
unsafeDisableTooltip={false}
101-
variant={hasNotifications ? 'primary' : 'invisible'}
101+
variant={hasUnreadNotifications ? 'primary' : 'invisible'}
102102
/>
103103

104104
{isLoggedIn && (

src/renderer/context/App.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ describe('renderer/context/App.tsx', () => {
4343
.spyOn(tray, 'setTrayIconColorAndTitle')
4444
.mockImplementation(jest.fn());
4545

46+
jest
47+
.spyOn(notifications, 'getNotificationCount')
48+
.mockImplementation(jest.fn());
49+
4650
jest
4751
.spyOn(notifications, 'getUnreadNotificationCount')
4852
.mockImplementation(jest.fn());

src/renderer/context/App.tsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ import {
4848
setUseAlternateIdleIcon,
4949
setUseUnreadActiveIcon,
5050
} from '../utils/comms';
51-
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
51+
import {
52+
getNotificationCount,
53+
getUnreadNotificationCount,
54+
} from '../utils/notifications/notifications';
5255
import { clearState, loadState, saveState } from '../utils/storage';
5356
import {
5457
DEFAULT_DAY_COLOR_SCHEME,
@@ -75,6 +78,7 @@ interface AppContextState {
7578

7679
notifications: AccountNotifications[];
7780
unreadCount: number;
81+
hasUnreadNotifications: boolean;
7882
hasNotifications: boolean;
7983
fetchNotifications: () => Promise<void>;
8084
removeAccountNotifications: (account: Account) => Promise<void>;
@@ -111,9 +115,17 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
111115
unsubscribeNotification,
112116
} = useNotifications();
113117

114-
const unreadCount = getUnreadNotificationCount(notifications);
118+
const notificationCount = getNotificationCount(notifications);
119+
const unreadNotificationCount = getUnreadNotificationCount(notifications);
115120

116-
const hasNotifications = useMemo(() => unreadCount > 0, [unreadCount]);
121+
const hasNotifications = useMemo(
122+
() => notificationCount > 0,
123+
[notificationCount],
124+
);
125+
const hasUnreadNotifications = useMemo(
126+
() => unreadNotificationCount > 0,
127+
[unreadNotificationCount],
128+
);
117129

118130
// biome-ignore lint/correctness/useExhaustiveDependencies: restoreSettings is stable and should run only once
119131
useEffect(() => {
@@ -175,12 +187,12 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
175187
useEffect(() => {
176188
setUseUnreadActiveIcon(settings.useUnreadActiveIcon);
177189
setUseAlternateIdleIcon(settings.useAlternateIdleIcon);
178-
setTrayIconColorAndTitle(unreadCount, settings);
190+
setTrayIconColorAndTitle(unreadNotificationCount, settings);
179191
}, [
180192
settings.showNotificationsCountInTray,
181193
settings.useUnreadActiveIcon,
182194
settings.useAlternateIdleIcon,
183-
unreadCount,
195+
unreadNotificationCount,
184196
]);
185197

186198
useEffect(() => {
@@ -359,8 +371,10 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
359371
globalError,
360372

361373
notifications,
362-
unreadCount,
374+
notificationCount,
375+
unreadNotificationCount,
363376
hasNotifications,
377+
hasUnreadNotifications,
364378
fetchNotifications: fetchNotificationsWithAccounts,
365379

366380
markNotificationsAsRead: markNotificationsAsReadWithAccounts,
@@ -385,8 +399,10 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
385399
globalError,
386400

387401
notifications,
388-
unreadCount,
402+
notificationCount,
403+
unreadNotificationCount,
389404
hasNotifications,
405+
hasUnreadNotifications,
390406
fetchNotificationsWithAccounts,
391407
markNotificationsAsReadWithAccounts,
392408
markNotificationsAsDoneWithAccounts,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type { Repository } from '../../typesGitHub';
2121
import * as logger from '../../utils/logger';
2222
import {
2323
enrichNotification,
24+
getNotificationCount,
2425
getUnreadNotificationCount,
2526
stabilizeNotificationsOrder,
2627
} from './notifications';
@@ -36,6 +37,12 @@ describe('renderer/utils/notifications/notifications.ts', () => {
3637
jest.clearAllMocks();
3738
});
3839

40+
it('getNotificationCount', () => {
41+
const result = getNotificationCount(mockSingleAccountNotifications);
42+
43+
expect(result).toBe(1);
44+
});
45+
3946
it('getUnreadNotificationCount', () => {
4047
const result = getUnreadNotificationCount(mockSingleAccountNotifications);
4148

src/renderer/utils/notifications/notifications.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,22 @@ import { getFlattenedNotificationsByRepo } from './group';
1515
import { createNotificationHandler } from './handlers';
1616

1717
/**
18-
* Get the count of unread notifications.
18+
* Get the count of notifications across all accounts.
1919
*
20-
* @param notifications - The notifications to check.
20+
* @param notifications - The account notifications to check.
21+
* @returns The count of all notifications.
22+
*/
23+
export function getNotificationCount(notifications: AccountNotifications[]) {
24+
return notifications.reduce(
25+
(sum, account) => sum + account.notifications.length,
26+
0,
27+
);
28+
}
29+
30+
/**
31+
* Get the count of notifications across all accounts.
32+
*
33+
* @param notifications - The account notifications to check.
2134
* @returns The count of unread notifications.
2235
*/
2336
export function getUnreadNotificationCount(

0 commit comments

Comments
 (0)