Skip to content
Merged
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
67 changes: 38 additions & 29 deletions src/renderer/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo, useState } from 'react';
import { useCallback, useMemo, useRef, useState } from 'react';

import type {
Account,
Expand Down Expand Up @@ -94,44 +94,53 @@ export const useNotifications = (): NotificationsState => {
[notifications],
);

const isFetchingRef = useRef(false);
const fetchNotifications = useCallback(
async (state: GitifyState) => {
if (isFetchingRef.current) {
// Prevent overlapping fetches
return;
}
isFetchingRef.current = true;
setStatus('loading');
setGlobalError(null);
try {
const previousNotifications = notifications;
const fetchedNotifications = await getAllNotifications(state);
setNotifications(fetchedNotifications);

// Set Global Error if all accounts have the same error
const allAccountsHaveErrors =
doesAllAccountsHaveErrors(fetchedNotifications);
const allAccountErrorsAreSame =
areAllAccountErrorsSame(fetchedNotifications);

if (allAccountsHaveErrors) {
const accountError = fetchedNotifications[0].error;
setStatus('error');
setGlobalError(allAccountErrorsAreSame ? accountError : null);
return;
}

const previousNotifications = notifications;
const fetchedNotifications = await getAllNotifications(state);
setNotifications(fetchedNotifications);

// Set Global Error if all accounts have the same error
const allAccountsHaveErrors =
doesAllAccountsHaveErrors(fetchedNotifications);
const allAccountErrorsAreSame =
areAllAccountErrorsSame(fetchedNotifications);

if (allAccountsHaveErrors) {
const accountError = fetchedNotifications[0].error;
setStatus('error');
setGlobalError(allAccountErrorsAreSame ? accountError : null);
return;
}
const diffNotifications = getNewNotifications(
previousNotifications,
fetchedNotifications,
);

const diffNotifications = getNewNotifications(
previousNotifications,
fetchedNotifications,
);
if (diffNotifications.length > 0) {
if (state.settings.playSound) {
raiseSoundNotification(state.settings.notificationVolume);
}

if (diffNotifications.length > 0) {
if (state.settings.playSound) {
raiseSoundNotification(state.settings.notificationVolume);
if (state.settings.showNotifications) {
raiseNativeNotification(diffNotifications);
}
}

if (state.settings.showNotifications) {
raiseNativeNotification(diffNotifications);
}
setStatus('success');
} finally {
isFetchingRef.current = false;
}

setStatus('success');
},
[notifications],
);
Expand Down