Skip to content

Commit 8e6f79e

Browse files
committed
refactor
Signed-off-by: Adam Setch <[email protected]>
1 parent bd77663 commit 8e6f79e

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

src/renderer/hooks/useNotifications.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { isMarkAsDoneFeatureSupported } from '../utils/features';
2222
import { rendererLogError } from '../utils/logger';
2323
import { raiseNativeNotification } from '../utils/notifications/native';
2424
import { getAllNotifications } from '../utils/notifications/notifications';
25-
import { removeNotifications } from '../utils/notifications/remove';
25+
import { removeNotificationsForAccount } from '../utils/notifications/remove';
2626
import { raiseSoundNotification } from '../utils/notifications/sound';
2727
import { getNewNotifications } from '../utils/notifications/utils';
2828

@@ -129,7 +129,8 @@ export const useNotifications = (): NotificationsState => {
129129
),
130130
);
131131

132-
const updatedNotifications = removeNotifications(
132+
const updatedNotifications = removeNotificationsForAccount(
133+
readNotifications[0].account,
133134
state.settings,
134135
readNotifications,
135136
notifications,
@@ -168,7 +169,8 @@ export const useNotifications = (): NotificationsState => {
168169
),
169170
);
170171

171-
const updatedNotifications = removeNotifications(
172+
const updatedNotifications = removeNotificationsForAccount(
173+
doneNotifications[0].account,
172174
state.settings,
173175
doneNotifications,
174176
notifications,

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { mockSingleAccountNotifications } from '../../__mocks__/notifications-mocks';
22
import { mockSettings } from '../../__mocks__/state-mocks';
33
import { mockSingleNotification } from '../api/__mocks__/response-mocks';
4-
import { removeNotifications } from './remove';
4+
import { removeNotificationsForAccount } from './remove';
55

66
describe('renderer/utils/remove.ts', () => {
77
it('should remove a notification if it exists', () => {
88
expect(mockSingleAccountNotifications[0].notifications.length).toBe(1);
99

10-
const result = removeNotifications(
10+
const result = removeNotificationsForAccount(
11+
mockSingleAccountNotifications[0].account,
1112
{ ...mockSettings, delayNotificationState: false },
1213
[mockSingleNotification],
1314
mockSingleAccountNotifications,
@@ -16,10 +17,11 @@ describe('renderer/utils/remove.ts', () => {
1617
expect(result[0].notifications.length).toBe(0);
1718
});
1819

19-
it('should skip notification removal if delay state enabled', () => {
20+
it('should mark as read and skip notification removal if delay state enabled', () => {
2021
expect(mockSingleAccountNotifications[0].notifications.length).toBe(1);
2122

22-
const result = removeNotifications(
23+
const result = removeNotificationsForAccount(
24+
mockSingleAccountNotifications[0].account,
2325
{ ...mockSettings, delayNotificationState: true },
2426
[mockSingleNotification],
2527
mockSingleAccountNotifications,
@@ -29,10 +31,30 @@ describe('renderer/utils/remove.ts', () => {
2931
expect(result[0].notifications[0].unread).toBe(false);
3032
});
3133

34+
it('should skip notification removal if delay state enabled and nothing to remove', () => {
35+
expect(mockSingleAccountNotifications[0].notifications.length).toBe(1);
36+
37+
const result = removeNotificationsForAccount(
38+
mockSingleAccountNotifications[0].account,
39+
{ ...mockSettings, delayNotificationState: true },
40+
[
41+
{
42+
...mockSingleNotification,
43+
id: 'non-existent-id',
44+
},
45+
],
46+
mockSingleAccountNotifications,
47+
);
48+
49+
expect(result[0].notifications.length).toBe(1);
50+
expect(result[0].notifications[0].unread).toBe(true);
51+
});
52+
3253
it('should skip notification removal if nothing to remove', () => {
3354
expect(mockSingleAccountNotifications[0].notifications.length).toBe(1);
3455

35-
const result = removeNotifications(
56+
const result = removeNotificationsForAccount(
57+
mockSingleAccountNotifications[0].account,
3658
{ ...mockSettings, delayNotificationState: false },
3759
[],
3860
mockSingleAccountNotifications,
Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
import type { AccountNotifications, SettingsState } from '../../types';
1+
import type { Account, AccountNotifications, SettingsState } from '../../types';
22
import type { Notification } from '../../typesGitHub';
3-
import { findAccountIndex } from './utils';
3+
import { getAccountUUID } from '../auth/utils';
44

5-
export function removeNotifications(
5+
/**
6+
* Remove notifications from the account notifications list.
7+
*
8+
* If delayNotificationState is enabled in settings, mark notifications as read instead of removing them.
9+
*/
10+
export function removeNotificationsForAccount(
11+
account: Account,
612
settings: SettingsState,
713
notificationsToRemove: Notification[],
814
allNotifications: AccountNotifications[],
@@ -11,37 +17,24 @@ export function removeNotifications(
1117
return allNotifications;
1218
}
1319

14-
const removeIds = new Set(notificationsToRemove.map((n) => n.id));
15-
16-
// If delay notifications is enabled, mark notifications as read but do not remove them
17-
if (settings.delayNotificationState) {
18-
return allNotifications.map((accountNotifications) => ({
19-
...accountNotifications,
20-
notifications: accountNotifications.notifications.map((notification) =>
21-
removeIds.has(notification.id)
22-
? { ...notification, unread: false }
23-
: notification,
24-
),
25-
}));
26-
}
27-
28-
const accountIndex = findAccountIndex(
29-
allNotifications,
30-
notificationsToRemove[0],
20+
const notificationIDsToRemove = new Set(
21+
notificationsToRemove.map((n) => n.id),
3122
);
3223

33-
if (accountIndex === -1) {
34-
return allNotifications;
35-
}
36-
37-
return allNotifications.map((account, index) =>
38-
index === accountIndex
24+
return allNotifications.map((accountNotifications) =>
25+
getAccountUUID(account) === getAccountUUID(accountNotifications.account)
3926
? {
40-
...account,
41-
notifications: account.notifications.filter(
42-
(notification) => !removeIds.has(notification.id),
43-
),
27+
...accountNotifications,
28+
notifications: settings.delayNotificationState
29+
? accountNotifications.notifications.map((notification) =>
30+
notificationIDsToRemove.has(notification.id)
31+
? { ...notification, unread: false }
32+
: notification,
33+
)
34+
: accountNotifications.notifications.filter(
35+
(notification) => !notificationIDsToRemove.has(notification.id),
36+
),
4437
}
45-
: account,
38+
: accountNotifications,
4639
);
4740
}

src/renderer/utils/notifications/utils.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,6 @@ import type { AccountNotifications } from '../../types';
22
import type { Notification } from '../../typesGitHub';
33
import { getAccountUUID } from '../auth/utils';
44

5-
/**
6-
* Find the account index for a given notification
7-
*
8-
* @param allNotifications - The list of all account notifications
9-
* @param notification - The notification to find the account index for
10-
* @returns The index of the account in the allNotifications array
11-
*/
12-
export function findAccountIndex(
13-
allNotifications: AccountNotifications[],
14-
notification: Notification,
15-
): number {
16-
return allNotifications.findIndex(
17-
(accountNotifications) =>
18-
getAccountUUID(accountNotifications.account) ===
19-
getAccountUUID(notification.account),
20-
);
21-
}
22-
235
/**
246
* Find notifications that exist in newNotifications but not in previousNotifications
257
*/

0 commit comments

Comments
 (0)