Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions src/renderer/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ import {
setUseAlternateIdleIcon,
setUseUnreadActiveIcon,
} from '../utils/comms';
import {
getNotificationCount,
getUnreadNotificationCount,
} from '../utils/notifications/notifications';
import { clearState, loadState, saveState } from '../utils/storage';
import {
DEFAULT_DAY_COLOR_SCHEME,
Expand Down Expand Up @@ -119,30 +115,23 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
const { setColorMode, setDayScheme, setNightScheme } = useTheme();

const {
status,
globalError,

notifications,
notificationCount,
unreadNotificationCount,
hasNotifications,
hasUnreadNotifications,

fetchNotifications,
removeAccountNotifications,
status,
globalError,

markNotificationsAsRead,
markNotificationsAsDone,
unsubscribeNotification,
} = useNotifications();

const notificationCount = getNotificationCount(notifications);

const unreadNotificationCount = getUnreadNotificationCount(notifications);

const hasNotifications = useMemo(
() => notificationCount > 0,
[notificationCount],
);

const hasUnreadNotifications = useMemo(
() => unreadNotificationCount > 0,
[unreadNotificationCount],
);

const refreshAllAccounts = useCallback(() => {
if (!auth.accounts.length) {
return;
Expand Down
30 changes: 28 additions & 2 deletions src/renderer/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useState } from 'react';
import { useCallback, useMemo, useState } from 'react';

import type {
Account,
Expand All @@ -20,7 +20,11 @@ import {
import { isMarkAsDoneFeatureSupported } from '../utils/features';
import { rendererLogError } from '../utils/logger';
import { raiseNativeNotification } from '../utils/notifications/native';
import { getAllNotifications } from '../utils/notifications/notifications';
import {
getAllNotifications,
getNotificationCount,
getUnreadNotificationCount,
} from '../utils/notifications/notifications';
import { removeNotificationsForAccount } from '../utils/notifications/remove';
import { raiseSoundNotification } from '../utils/notifications/sound';
import { getNewNotifications } from '../utils/notifications/utils';
Expand All @@ -30,6 +34,10 @@ interface NotificationsState {
globalError: GitifyError;

notifications: AccountNotifications[];
notificationCount: number;
unreadNotificationCount: number;
hasNotifications: boolean;
hasUnreadNotifications: boolean;

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

const notificationCount = getNotificationCount(notifications);

const unreadNotificationCount = getUnreadNotificationCount(notifications);

const hasNotifications = useMemo(
() => notificationCount > 0,
[notificationCount],
);

const hasUnreadNotifications = useMemo(
() => unreadNotificationCount > 0,
[unreadNotificationCount],
);

const removeAccountNotifications = useCallback(
async (account: Account) => {
setStatus('loading');
Expand Down Expand Up @@ -224,6 +246,10 @@ export const useNotifications = (): NotificationsState => {
globalError,

notifications,
notificationCount,
unreadNotificationCount,
hasNotifications,
hasUnreadNotifications,

fetchNotifications,
removeAccountNotifications,
Expand Down
16 changes: 9 additions & 7 deletions src/renderer/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,24 @@ export const Errors: Record<ErrorType, GitifyError> = {
* Check if all accounts have errors
*/
export function doesAllAccountsHaveErrors(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
) {
return (
notifications.length > 0 &&
notifications.every((account) => account.error !== null)
accountNotifications.length > 0 &&
accountNotifications.every((account) => account.error !== null)
);
}

/**
* Check if all account errors are the same
*/
export function areAllAccountErrorsSame(notifications: AccountNotifications[]) {
if (notifications.length === 0) {
export function areAllAccountErrorsSame(
accountNotifications: AccountNotifications[],
) {
if (accountNotifications.length === 0) {
return true;
}

const firstError = notifications[0].error;
return notifications.every((account) => account.error === firstError);
const firstError = accountNotifications[0].error;
return accountNotifications.every((account) => account.error === firstError);
}
4 changes: 2 additions & 2 deletions src/renderer/utils/notifications/filters/reason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const reasonFilter: Filter<Reason> = {
},

getFilterCount(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
reason: Reason,
): number {
return notifications.reduce(
return accountNotifications.reduce(
(sum, account) =>
sum +
account.notifications.filter((n) => this.filterNotification(n, reason))
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/utils/notifications/filters/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ export const stateFilter: Filter<FilterStateType> = {
},

getFilterCount(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
stateType: FilterStateType,
): number {
return notifications.reduce(
return accountNotifications.reduce(
(sum, account) =>
sum +
account.notifications.filter((n) =>
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/utils/notifications/filters/subjectType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export const subjectTypeFilter: Filter<SubjectType> = {
},

getFilterCount(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
subjectType: SubjectType,
): number {
return notifications.reduce(
return accountNotifications.reduce(
(sum, account) =>
sum +
account.notifications.filter((n) =>
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/utils/notifications/filters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Filter<T extends string> {

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

getFilterCount(notifications: AccountNotifications[], type: T): number;
getFilterCount(accountNotifications: AccountNotifications[], type: T): number;

filterNotification(notification: Notification, type: T): boolean;
}
4 changes: 2 additions & 2 deletions src/renderer/utils/notifications/filters/userType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export const userTypeFilter: Filter<UserType> = {
},

getFilterCount(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
userType: UserType,
): number {
return notifications.reduce(
return accountNotifications.reduce(
(sum, account) =>
sum +
account.notifications.filter((n) =>
Expand Down
30 changes: 16 additions & 14 deletions src/renderer/utils/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import { createNotificationHandler } from './handlers';
/**
* Get the count of notifications across all accounts.
*
* @param notifications - The account notifications to check.
* @param accountNotifications - The account notifications to check.
* @returns The count of all notifications.
*/
export function getNotificationCount(notifications: AccountNotifications[]) {
return notifications.reduce(
export function getNotificationCount(
accountNotifications: AccountNotifications[],
) {
return accountNotifications.reduce(
(sum, account) => sum + account.notifications.length,
0,
);
Expand All @@ -30,13 +32,13 @@ export function getNotificationCount(notifications: AccountNotifications[]) {
/**
* Get the count of notifications across all accounts.
*
* @param notifications - The account notifications to check.
* @param accountNotifications - The account notifications to check.
* @returns The count of unread notifications.
*/
export function getUnreadNotificationCount(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
) {
return notifications.reduce(
return accountNotifications.reduce(
(sum, account) =>
sum + account.notifications.filter((n) => n.unread === true).length,
0,
Expand Down Expand Up @@ -64,7 +66,7 @@ function getNotifications(state: GitifyState) {
export async function getAllNotifications(
state: GitifyState,
): Promise<AccountNotifications[]> {
const notifications: AccountNotifications[] = await Promise.all(
const accountNotifications: AccountNotifications[] = await Promise.all(
getNotifications(state)
.filter((response) => !!response)
.map(async (accountNotifications) => {
Expand Down Expand Up @@ -113,9 +115,9 @@ export async function getAllNotifications(
);

// Set the order property for the notifications
stabilizeNotificationsOrder(notifications, state.settings);
stabilizeNotificationsOrder(accountNotifications, state.settings);

return notifications;
return accountNotifications;
}

export async function enrichNotifications(
Expand Down Expand Up @@ -178,23 +180,23 @@ export async function enrichNotification(
* Assign an order property to each notification to stabilize how they are displayed
* during notification interaction events (mark as read, mark as done, etc.)
*
* @param notifications
* @param accountNotifications
* @param settings
*/
export function stabilizeNotificationsOrder(
notifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
settings: SettingsState,
) {
let orderIndex = 0;

for (const account of notifications) {
for (const account of accountNotifications) {
const flattenedNotifications = getFlattenedNotificationsByRepo(
account.notifications,
settings,
);

for (const n of flattenedNotifications) {
n.order = orderIndex++;
for (const notification of flattenedNotifications) {
notification.order = orderIndex++;
}
}
}
8 changes: 4 additions & 4 deletions src/renderer/utils/notifications/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ export function removeNotificationsForAccount(
account: Account,
settings: SettingsState,
notificationsToRemove: Notification[],
allNotifications: AccountNotifications[],
accountNotifications: AccountNotifications[],
): AccountNotifications[] {
if (notificationsToRemove.length === 0) {
return allNotifications;
return accountNotifications;
}

const notificationIDsToRemove = new Set(
notificationsToRemove.map((n) => n.id),
notificationsToRemove.map((notification) => notification.id),
);

return allNotifications.map((accountNotifications) =>
return accountNotifications.map((accountNotifications) =>
getAccountUUID(account) === getAccountUUID(accountNotifications.account)
? {
...accountNotifications,
Expand Down
Loading