Skip to content

Commit e7d9c7e

Browse files
authored
refactor: account notifications (#2418)
* refactor: account notifications Signed-off-by: Adam Setch <adam.setch@outlook.com> * refactor: account notifications Signed-off-by: Adam Setch <adam.setch@outlook.com> --------- Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 23250ae commit e7d9c7e

File tree

12 files changed

+115
-96
lines changed

12 files changed

+115
-96
lines changed

src/renderer/context/App.tsx

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ import {
4949
setUseAlternateIdleIcon,
5050
setUseUnreadActiveIcon,
5151
} from '../utils/comms';
52-
import {
53-
getNotificationCount,
54-
getUnreadNotificationCount,
55-
} from '../utils/notifications/notifications';
5652
import { clearState, loadState, saveState } from '../utils/storage';
5753
import {
5854
DEFAULT_DAY_COLOR_SCHEME,
@@ -128,30 +124,23 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
128124
const { setColorMode, setDayScheme, setNightScheme } = useTheme();
129125

130126
const {
127+
status,
128+
globalError,
129+
131130
notifications,
131+
notificationCount,
132+
unreadNotificationCount,
133+
hasNotifications,
134+
hasUnreadNotifications,
135+
132136
fetchNotifications,
133137
removeAccountNotifications,
134-
status,
135-
globalError,
138+
136139
markNotificationsAsRead,
137140
markNotificationsAsDone,
138141
unsubscribeNotification,
139142
} = useNotifications();
140143

141-
const notificationCount = getNotificationCount(notifications);
142-
143-
const unreadNotificationCount = getUnreadNotificationCount(notifications);
144-
145-
const hasNotifications = useMemo(
146-
() => notificationCount > 0,
147-
[notificationCount],
148-
);
149-
150-
const hasUnreadNotifications = useMemo(
151-
() => unreadNotificationCount > 0,
152-
[unreadNotificationCount],
153-
);
154-
155144
const refreshAllAccounts = useCallback(() => {
156145
if (!auth.accounts.length) {
157146
return;

src/renderer/hooks/useNotifications.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useState } from 'react';
1+
import { useCallback, useMemo, useState } from 'react';
22

33
import type {
44
Account,
@@ -20,7 +20,11 @@ import {
2020
import { isMarkAsDoneFeatureSupported } from '../utils/features';
2121
import { rendererLogError } from '../utils/logger';
2222
import { raiseNativeNotification } from '../utils/notifications/native';
23-
import { getAllNotifications } from '../utils/notifications/notifications';
23+
import {
24+
getAllNotifications,
25+
getNotificationCount,
26+
getUnreadNotificationCount,
27+
} from '../utils/notifications/notifications';
2428
import { removeNotificationsForAccount } from '../utils/notifications/remove';
2529
import { raiseSoundNotification } from '../utils/notifications/sound';
2630
import { getNewNotifications } from '../utils/notifications/utils';
@@ -30,6 +34,10 @@ interface NotificationsState {
3034
globalError: GitifyError;
3135

3236
notifications: AccountNotifications[];
37+
notificationCount: number;
38+
unreadNotificationCount: number;
39+
hasNotifications: boolean;
40+
hasUnreadNotifications: boolean;
3341

3442
fetchNotifications: (state: GitifyState) => Promise<void>;
3543
removeAccountNotifications: (account: Account) => Promise<void>;
@@ -56,6 +64,20 @@ export const useNotifications = (): NotificationsState => {
5664
[],
5765
);
5866

67+
const notificationCount = getNotificationCount(notifications);
68+
69+
const unreadNotificationCount = getUnreadNotificationCount(notifications);
70+
71+
const hasNotifications = useMemo(
72+
() => notificationCount > 0,
73+
[notificationCount],
74+
);
75+
76+
const hasUnreadNotifications = useMemo(
77+
() => unreadNotificationCount > 0,
78+
[unreadNotificationCount],
79+
);
80+
5981
const removeAccountNotifications = useCallback(
6082
async (account: Account) => {
6183
setStatus('loading');
@@ -224,6 +246,10 @@ export const useNotifications = (): NotificationsState => {
224246
globalError,
225247

226248
notifications,
249+
notificationCount,
250+
unreadNotificationCount,
251+
hasNotifications,
252+
hasUnreadNotifications,
227253

228254
fetchNotifications,
229255
removeAccountNotifications,

src/renderer/utils/errors.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ export const Errors: Record<ErrorType, GitifyError> = {
3535
* Check if all accounts have errors
3636
*/
3737
export function doesAllAccountsHaveErrors(
38-
notifications: AccountNotifications[],
38+
accountNotifications: AccountNotifications[],
3939
) {
4040
return (
41-
notifications.length > 0 &&
42-
notifications.every((account) => account.error !== null)
41+
accountNotifications.length > 0 &&
42+
accountNotifications.every((account) => account.error !== null)
4343
);
4444
}
4545

4646
/**
4747
* Check if all account errors are the same
4848
*/
49-
export function areAllAccountErrorsSame(notifications: AccountNotifications[]) {
50-
if (notifications.length === 0) {
49+
export function areAllAccountErrorsSame(
50+
accountNotifications: AccountNotifications[],
51+
) {
52+
if (accountNotifications.length === 0) {
5153
return true;
5254
}
5355

54-
const firstError = notifications[0].error;
55-
return notifications.every((account) => account.error === firstError);
56+
const firstError = accountNotifications[0].error;
57+
return accountNotifications.every((account) => account.error === firstError);
5658
}

src/renderer/utils/notifications/filters/reason.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export const reasonFilter: Filter<Reason> = {
2525
},
2626

2727
getFilterCount(
28-
notifications: AccountNotifications[],
28+
accountNotifications: AccountNotifications[],
2929
reason: Reason,
3030
): number {
31-
return notifications.reduce(
31+
return accountNotifications.reduce(
3232
(sum, account) =>
3333
sum +
3434
account.notifications.filter((n) => this.filterNotification(n, reason))

src/renderer/utils/notifications/filters/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ export const stateFilter: Filter<FilterStateType> = {
4646
},
4747

4848
getFilterCount(
49-
notifications: AccountNotifications[],
49+
accountNotifications: AccountNotifications[],
5050
stateType: FilterStateType,
5151
): number {
52-
return notifications.reduce(
52+
return accountNotifications.reduce(
5353
(sum, account) =>
5454
sum +
5555
account.notifications.filter((n) =>

src/renderer/utils/notifications/filters/subjectType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ export const subjectTypeFilter: Filter<SubjectType> = {
5757
},
5858

5959
getFilterCount(
60-
notifications: AccountNotifications[],
60+
accountNotifications: AccountNotifications[],
6161
subjectType: SubjectType,
6262
): number {
63-
return notifications.reduce(
63+
return accountNotifications.reduce(
6464
(sum, account) =>
6565
sum +
6666
account.notifications.filter((n) =>

src/renderer/utils/notifications/filters/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface Filter<T extends string> {
1616

1717
isFilterSet(settings: SettingsState, type: T): boolean;
1818

19-
getFilterCount(notifications: AccountNotifications[], type: T): number;
19+
getFilterCount(accountNotifications: AccountNotifications[], type: T): number;
2020

2121
filterNotification(notification: Notification, type: T): boolean;
2222
}

src/renderer/utils/notifications/filters/userType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ export const userTypeFilter: Filter<UserType> = {
3737
},
3838

3939
getFilterCount(
40-
notifications: AccountNotifications[],
40+
accountNotifications: AccountNotifications[],
4141
userType: UserType,
4242
): number {
43-
return notifications.reduce(
43+
return accountNotifications.reduce(
4444
(sum, account) =>
4545
sum +
4646
account.notifications.filter((n) =>

src/renderer/utils/notifications/notifications.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import { createNotificationHandler } from './handlers';
1717
/**
1818
* Get the count of notifications across all accounts.
1919
*
20-
* @param notifications - The account notifications to check.
20+
* @param accountNotifications - The account notifications to check.
2121
* @returns The count of all notifications.
2222
*/
23-
export function getNotificationCount(notifications: AccountNotifications[]) {
24-
return notifications.reduce(
23+
export function getNotificationCount(
24+
accountNotifications: AccountNotifications[],
25+
) {
26+
return accountNotifications.reduce(
2527
(sum, account) => sum + account.notifications.length,
2628
0,
2729
);
@@ -30,13 +32,13 @@ export function getNotificationCount(notifications: AccountNotifications[]) {
3032
/**
3133
* Get the count of notifications across all accounts.
3234
*
33-
* @param notifications - The account notifications to check.
35+
* @param accountNotifications - The account notifications to check.
3436
* @returns The count of unread notifications.
3537
*/
3638
export function getUnreadNotificationCount(
37-
notifications: AccountNotifications[],
39+
accountNotifications: AccountNotifications[],
3840
) {
39-
return notifications.reduce(
41+
return accountNotifications.reduce(
4042
(sum, account) =>
4143
sum + account.notifications.filter((n) => n.unread === true).length,
4244
0,
@@ -64,7 +66,7 @@ function getNotifications(state: GitifyState) {
6466
export async function getAllNotifications(
6567
state: GitifyState,
6668
): Promise<AccountNotifications[]> {
67-
const notifications: AccountNotifications[] = await Promise.all(
69+
const accountNotifications: AccountNotifications[] = await Promise.all(
6870
getNotifications(state)
6971
.filter((response) => !!response)
7072
.map(async (accountNotifications) => {
@@ -113,9 +115,9 @@ export async function getAllNotifications(
113115
);
114116

115117
// Set the order property for the notifications
116-
stabilizeNotificationsOrder(notifications, state.settings);
118+
stabilizeNotificationsOrder(accountNotifications, state.settings);
117119

118-
return notifications;
120+
return accountNotifications;
119121
}
120122

121123
export async function enrichNotifications(
@@ -178,23 +180,23 @@ export async function enrichNotification(
178180
* Assign an order property to each notification to stabilize how they are displayed
179181
* during notification interaction events (mark as read, mark as done, etc.)
180182
*
181-
* @param notifications
183+
* @param accountNotifications
182184
* @param settings
183185
*/
184186
export function stabilizeNotificationsOrder(
185-
notifications: AccountNotifications[],
187+
accountNotifications: AccountNotifications[],
186188
settings: SettingsState,
187189
) {
188190
let orderIndex = 0;
189191

190-
for (const account of notifications) {
192+
for (const account of accountNotifications) {
191193
const flattenedNotifications = getFlattenedNotificationsByRepo(
192194
account.notifications,
193195
settings,
194196
);
195197

196-
for (const n of flattenedNotifications) {
197-
n.order = orderIndex++;
198+
for (const notification of flattenedNotifications) {
199+
notification.order = orderIndex++;
198200
}
199201
}
200202
}

src/renderer/utils/notifications/remove.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ export function removeNotificationsForAccount(
1111
account: Account,
1212
settings: SettingsState,
1313
notificationsToRemove: Notification[],
14-
allNotifications: AccountNotifications[],
14+
accountNotifications: AccountNotifications[],
1515
): AccountNotifications[] {
1616
if (notificationsToRemove.length === 0) {
17-
return allNotifications;
17+
return accountNotifications;
1818
}
1919

2020
const notificationIDsToRemove = new Set(
21-
notificationsToRemove.map((n) => n.id),
21+
notificationsToRemove.map((notification) => notification.id),
2222
);
2323

24-
return allNotifications.map((accountNotifications) =>
24+
return accountNotifications.map((accountNotifications) =>
2525
getAccountUUID(account) === getAccountUUID(accountNotifications.account)
2626
? {
2727
...accountNotifications,

0 commit comments

Comments
 (0)