Skip to content

Commit 1d1a4b8

Browse files
committed
refactor simplify
Signed-off-by: Adam Setch <[email protected]>
1 parent 63a3386 commit 1d1a4b8

File tree

10 files changed

+99
-101
lines changed

10 files changed

+99
-101
lines changed

src/renderer/context/App.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { mockSingleNotification } from '../utils/api/__mocks__/response-mocks';
99
import * as apiRequests from '../utils/api/request';
1010
import * as comms from '../utils/comms';
1111
import * as notifications from '../utils/notifications/notifications';
12+
import * as tray from '../utils/notifications/tray';
1213
import * as storage from '../utils/storage';
1314
import { AppContext, AppProvider } from './App';
1415
import { defaultSettings } from './defaults';
@@ -39,7 +40,7 @@ describe('renderer/context/App.tsx', () => {
3940

4041
describe('notification methods', () => {
4142
const setTrayIconColorAndTitleMock = jest
42-
.spyOn(notifications, 'setTrayIconColorAndTitle')
43+
.spyOn(tray, 'setTrayIconColorAndTitle')
4344
.mockImplementation(jest.fn());
4445

4546
jest

src/renderer/context/App.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ import {
4848
setUseAlternateIdleIcon,
4949
setUseUnreadActiveIcon,
5050
} from '../utils/comms';
51-
import {
52-
getUnreadNotificationCount,
53-
setTrayIconColorAndTitle,
54-
} from '../utils/notifications/notifications';
51+
import { getUnreadNotificationCount } from '../utils/notifications/notifications';
52+
import { setTrayIconColorAndTitle } from '../utils/notifications/tray';
5553
import { clearState, loadState, saveState } from '../utils/storage';
5654
import {
5755
DEFAULT_DAY_COLOR_SCHEME,

src/renderer/hooks/useNotifications.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ describe('renderer/hooks/useNotifications.ts', () => {
370370
});
371371

372372
expect(result.current.notifications.length).toBe(0);
373-
expect(mockSingleNotification.unread).toBeFalsy();
374373
});
375374

376375
it('should mark notifications as read with failure', async () => {
@@ -414,7 +413,6 @@ describe('renderer/hooks/useNotifications.ts', () => {
414413
});
415414

416415
expect(result.current.notifications.length).toBe(0);
417-
expect(mockSingleNotification.unread).toBeFalsy();
418416
});
419417

420418
it('should mark notifications as done with failure', async () => {

src/renderer/hooks/useNotifications.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@ import {
1616
import { updateTrayColor } from '../utils/comms';
1717
import { isMarkAsDoneFeatureSupported } from '../utils/features';
1818
import { rendererLogError } from '../utils/logger';
19-
import {
20-
raiseNativeNotification,
21-
raiseSoundNotification,
22-
} from '../utils/notifications/native';
23-
import {
24-
getAllNotifications,
25-
getNewNotifications,
26-
} from '../utils/notifications/notifications';
19+
import { raiseNativeNotification } from '../utils/notifications/native';
20+
import { getAllNotifications } from '../utils/notifications/notifications';
2721
import { removeNotifications } from '../utils/notifications/remove';
22+
import { raiseSoundNotification } from '../utils/notifications/sound';
23+
import { getNewNotifications } from '../utils/notifications/utils';
2824

2925
interface NotificationsState {
3026
status: Status;

src/renderer/utils/notifications/native.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,3 @@ export const raiseNativeNotification = async (
2424

2525
return window.gitify.raiseNativeNotification(title, body, url);
2626
};
27-
28-
export const raiseSoundNotification = async (volume: number) => {
29-
const path = await window.gitify.notificationSoundPath();
30-
31-
const audio = new Audio(path);
32-
audio.volume = volume;
33-
audio.play();
34-
};

src/renderer/utils/notifications/notifications.ts

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import type {
66
import type { GitifySubject, Notification } from '../../typesGitHub';
77
import { listNotificationsForAuthenticatedUser } from '../api/client';
88
import { determineFailureType } from '../api/errors';
9-
import { getAccountUUID } from '../auth/utils';
10-
import { updateTrayColor, updateTrayTitle } from '../comms';
119
import { rendererLogError, rendererLogWarn } from '../logger';
1210
import {
1311
filterBaseNotifications,
@@ -16,27 +14,6 @@ import {
1614
import { getFlattenedNotificationsByRepo } from './group';
1715
import { createNotificationHandler } from './handlers';
1816

19-
/**
20-
* Sets the tray icon color and title based on the number of unread notifications.
21-
*
22-
* @param notifications
23-
* @param settings
24-
*/
25-
export function setTrayIconColorAndTitle(
26-
notifications: AccountNotifications[],
27-
settings: SettingsState,
28-
) {
29-
const totalUnreadNotifications = getUnreadNotificationCount(notifications);
30-
31-
let title = '';
32-
if (settings.showNotificationsCountInTray && totalUnreadNotifications > 0) {
33-
title = totalUnreadNotifications.toString();
34-
}
35-
36-
updateTrayColor(totalUnreadNotifications);
37-
updateTrayTitle(title);
38-
}
39-
4017
/**
4118
* Get the count of unread notifications.
4219
*
@@ -206,49 +183,3 @@ export function stabilizeNotificationsOrder(
206183
n.order = orderIndex++;
207184
}
208185
}
209-
210-
/**
211-
* Find the account index for a given notification
212-
*
213-
* @param allNotifications - The list of all account notifications
214-
* @param notification - The notification to find the account index for
215-
* @returns The index of the account in the allNotifications array
216-
*/
217-
export const findAccountIndex = (
218-
allNotifications: AccountNotifications[],
219-
notification: Notification,
220-
): number => {
221-
return allNotifications.findIndex(
222-
(accountNotifications) =>
223-
getAccountUUID(accountNotifications.account) ===
224-
getAccountUUID(notification.account),
225-
);
226-
};
227-
228-
/**
229-
* Find notifications that exist in newNotifications but not in previousNotifications
230-
*/
231-
export const getNewNotifications = (
232-
previousNotifications: AccountNotifications[],
233-
newNotifications: AccountNotifications[],
234-
): Notification[] => {
235-
return newNotifications.flatMap((accountNotifications) => {
236-
const accountPreviousNotifications = previousNotifications.find(
237-
(item) =>
238-
getAccountUUID(item.account) ===
239-
getAccountUUID(accountNotifications.account),
240-
);
241-
242-
if (!accountPreviousNotifications) {
243-
return accountNotifications.notifications;
244-
}
245-
246-
const previousIds = new Set(
247-
accountPreviousNotifications.notifications.map((item) => item.id),
248-
);
249-
250-
return accountNotifications.notifications.filter(
251-
(notification) => !previousIds.has(notification.id),
252-
);
253-
});
254-
};

src/renderer/utils/notifications/remove.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AccountNotifications, SettingsState } from '../../types';
22
import type { Notification } from '../../typesGitHub';
3-
import { findAccountIndex } from './notifications';
3+
import { findAccountIndex } from './utils';
44

55
export function removeNotifications(
66
settings: SettingsState,
@@ -34,12 +34,14 @@ export function removeNotifications(
3434
return allNotifications;
3535
}
3636

37-
const updatedNotifications = [...allNotifications];
38-
updatedNotifications[accountIndex] = {
39-
...updatedNotifications[accountIndex],
40-
notifications: updatedNotifications[accountIndex].notifications.filter(
41-
(notification) => !removeIds.has(notification.id),
42-
),
43-
};
44-
return updatedNotifications;
37+
return allNotifications.map((account, index) =>
38+
index === accountIndex
39+
? {
40+
...account,
41+
notifications: account.notifications.filter(
42+
(notification) => !removeIds.has(notification.id),
43+
),
44+
}
45+
: account,
46+
);
4547
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const raiseSoundNotification = async (volume: number) => {
2+
const path = await window.gitify.notificationSoundPath();
3+
4+
const audio = new Audio(path);
5+
audio.volume = volume;
6+
audio.play();
7+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { AccountNotifications, SettingsState } from '../../types';
2+
import { updateTrayColor, updateTrayTitle } from '../comms';
3+
import { getUnreadNotificationCount } from './notifications';
4+
5+
/**
6+
* Sets the tray icon color and title based on the number of unread notifications.
7+
*
8+
* @param notifications
9+
* @param settings
10+
*/
11+
export function setTrayIconColorAndTitle(
12+
notifications: AccountNotifications[],
13+
settings: SettingsState,
14+
) {
15+
const totalUnreadNotifications = getUnreadNotificationCount(notifications);
16+
17+
let title = '';
18+
if (settings.showNotificationsCountInTray && totalUnreadNotifications > 0) {
19+
title = totalUnreadNotifications.toString();
20+
}
21+
22+
updateTrayColor(totalUnreadNotifications);
23+
updateTrayTitle(title);
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type { AccountNotifications } from '../../types';
2+
import type { Notification } from '../../typesGitHub';
3+
import { getAccountUUID } from '../auth/utils';
4+
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 const 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+
23+
/**
24+
* Find notifications that exist in newNotifications but not in previousNotifications
25+
*/
26+
export const getNewNotifications = (
27+
previousNotifications: AccountNotifications[],
28+
newNotifications: AccountNotifications[],
29+
): Notification[] => {
30+
return newNotifications.flatMap((accountNotifications) => {
31+
const accountPreviousNotifications = previousNotifications.find(
32+
(item) =>
33+
getAccountUUID(item.account) ===
34+
getAccountUUID(accountNotifications.account),
35+
);
36+
37+
if (!accountPreviousNotifications) {
38+
return accountNotifications.notifications;
39+
}
40+
41+
const previousIds = new Set(
42+
accountPreviousNotifications.notifications.map((item) => item.id),
43+
);
44+
45+
return accountNotifications.notifications.filter(
46+
(notification) => !previousIds.has(notification.id),
47+
);
48+
});
49+
};

0 commit comments

Comments
 (0)