diff --git a/src/renderer/hooks/useNotifications.ts b/src/renderer/hooks/useNotifications.ts index eaae98ddc..595b140a4 100644 --- a/src/renderer/hooks/useNotifications.ts +++ b/src/renderer/hooks/useNotifications.ts @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useCallback, useMemo, useRef, useState } from 'react'; import type { Account, @@ -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], );