Skip to content

Commit 1eaaa74

Browse files
committed
markOnClick-native-notification
1 parent 8c93f91 commit 1eaaa74

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/hooks/useNotifications.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export const useNotifications = (): NotificationsState => {
100100
notifications,
101101
data,
102102
settings,
103-
accounts
103+
accounts,
104+
markNotification
104105
);
105106
setNotifications(data);
106107
setIsFetching(false);

src/utils/notifications.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ describe('utils/notifications.ts', () => {
107107

108108
const nativeNotification: Notification = notificationsHelpers.raiseNativeNotification(
109109
[mockedGithubNotifications[0]],
110+
defaultSettings,
110111
mockAccounts
111112
);
112113
nativeNotification.onclick(null);
@@ -127,6 +128,7 @@ describe('utils/notifications.ts', () => {
127128

128129
const nativeNotification = notificationsHelpers.raiseNativeNotification(
129130
mockedGithubNotifications,
131+
defaultSettings,
130132
mockAccounts
131133
);
132134
nativeNotification.onclick(null);

src/utils/notifications.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { Notification } from '../typesGithub';
66

77
import { AccountNotifications, SettingsState, AuthState } from '../types';
88

9+
let markNotification: (accounts: AuthState, id: string, hostname: string) => Promise<void>;
10+
911
export const setTrayIconColor = (notifications: AccountNotifications[]) => {
1012
const allNotificationsCount = notifications.reduce(
1113
(memo, acc) => memo + acc.notifications.length,
@@ -19,16 +21,19 @@ export const triggerNativeNotifications = (
1921
previousNotifications: AccountNotifications[],
2022
newNotifications: AccountNotifications[],
2123
settings: SettingsState,
22-
accounts: AuthState
24+
accounts: AuthState,
25+
markNotification_?: (accounts: AuthState, id: string, hostname: string) => Promise<void>
2326
) => {
24-
const diffNotifications = newNotifications
27+
markNotification ??= markNotification_;
28+
29+
const diff = newNotifications
2530
.map((account) => {
2631
const accountPreviousNotifications = previousNotifications.find(
2732
(item) => item.hostname === account.hostname
2833
);
2934

3035
if (!accountPreviousNotifications) {
31-
return account.notifications;
36+
return { hostname: account.hostname, notifications: account.notifications };
3237
}
3338

3439
const accountPreviousNotificationsIds = accountPreviousNotifications.notifications.map(
@@ -39,9 +44,10 @@ export const triggerNativeNotifications = (
3944
return !accountPreviousNotificationsIds.includes(`${item.id}`);
4045
});
4146

42-
return accountNewNotifications;
47+
return { hostname: account.hostname, notifications: accountNewNotifications };
4348
})
44-
.reduce((acc, val) => acc.concat(val), []);
49+
50+
const diffNotifications = diff.flatMap(o => o.notifications)
4551

4652
setTrayIconColor(newNotifications);
4753

@@ -55,13 +61,15 @@ export const triggerNativeNotifications = (
5561
}
5662

5763
if (settings.showNotifications) {
58-
raiseNativeNotification(diffNotifications, accounts);
64+
raiseNativeNotification(diffNotifications, settings, accounts, diff[0].hostname);
5965
}
6066
};
6167

6268
export const raiseNativeNotification = (
6369
notifications: Notification[],
64-
accounts: AuthState
70+
settings: SettingsState,
71+
accounts: AuthState,
72+
hostname?: string
6573
) => {
6674
let title: string;
6775
let body: string;
@@ -84,6 +92,9 @@ export const raiseNativeNotification = (
8492
if (notifications.length === 1) {
8593
remote.getCurrentWindow().hide();
8694
openInBrowser(notifications[0], accounts);
95+
if (settings.markOnClick) {
96+
markNotification?.(accounts, notifications[0].id, hostname);
97+
}
8798
} else {
8899
reOpenWindow();
89100
}

0 commit comments

Comments
 (0)