|
1 | | -import { useCallback, useMemo, useState } from 'react'; |
| 1 | +import { useCallback, useMemo, useRef, useState } from 'react'; |
2 | 2 |
|
3 | 3 | import type { |
4 | 4 | Account, |
@@ -94,44 +94,52 @@ export const useNotifications = (): NotificationsState => { |
94 | 94 | [notifications], |
95 | 95 | ); |
96 | 96 |
|
| 97 | + const isFetchingRef = useRef(false); |
97 | 98 | const fetchNotifications = useCallback( |
98 | 99 | async (state: GitifyState) => { |
99 | | - setStatus('loading'); |
100 | | - setGlobalError(null); |
101 | | - |
102 | | - const previousNotifications = notifications; |
103 | | - const fetchedNotifications = await getAllNotifications(state); |
104 | | - setNotifications(fetchedNotifications); |
105 | | - |
106 | | - // Set Global Error if all accounts have the same error |
107 | | - const allAccountsHaveErrors = |
108 | | - doesAllAccountsHaveErrors(fetchedNotifications); |
109 | | - const allAccountErrorsAreSame = |
110 | | - areAllAccountErrorsSame(fetchedNotifications); |
111 | | - |
112 | | - if (allAccountsHaveErrors) { |
113 | | - const accountError = fetchedNotifications[0].error; |
114 | | - setStatus('error'); |
115 | | - setGlobalError(allAccountErrorsAreSame ? accountError : null); |
| 100 | + if (isFetchingRef.current) { |
| 101 | + // Prevent overlapping fetches |
116 | 102 | return; |
117 | 103 | } |
| 104 | + isFetchingRef.current = true; |
| 105 | + setStatus('loading'); |
| 106 | + setGlobalError(null); |
| 107 | + try { |
| 108 | + const previousNotifications = notifications; |
| 109 | + const fetchedNotifications = await getAllNotifications(state); |
| 110 | + setNotifications(fetchedNotifications); |
| 111 | + |
| 112 | + // Set Global Error if all accounts have the same error |
| 113 | + const allAccountsHaveErrors = |
| 114 | + doesAllAccountsHaveErrors(fetchedNotifications); |
| 115 | + const allAccountErrorsAreSame = |
| 116 | + areAllAccountErrorsSame(fetchedNotifications); |
| 117 | + |
| 118 | + if (allAccountsHaveErrors) { |
| 119 | + const accountError = fetchedNotifications[0].error; |
| 120 | + setStatus('error'); |
| 121 | + setGlobalError(allAccountErrorsAreSame ? accountError : null); |
| 122 | + return; |
| 123 | + } |
118 | 124 |
|
119 | | - const diffNotifications = getNewNotifications( |
120 | | - previousNotifications, |
121 | | - fetchedNotifications, |
122 | | - ); |
| 125 | + const diffNotifications = getNewNotifications( |
| 126 | + previousNotifications, |
| 127 | + fetchedNotifications, |
| 128 | + ); |
123 | 129 |
|
124 | | - if (diffNotifications.length > 0) { |
125 | | - if (state.settings.playSound) { |
126 | | - raiseSoundNotification(state.settings.notificationVolume); |
127 | | - } |
| 130 | + if (diffNotifications.length > 0) { |
| 131 | + if (state.settings.playSound) { |
| 132 | + raiseSoundNotification(state.settings.notificationVolume); |
| 133 | + } |
128 | 134 |
|
129 | | - if (state.settings.showNotifications) { |
130 | | - raiseNativeNotification(diffNotifications); |
| 135 | + if (state.settings.showNotifications) { |
| 136 | + raiseNativeNotification(diffNotifications); |
| 137 | + } |
131 | 138 | } |
| 139 | + } finally { |
| 140 | + setStatus('success'); |
| 141 | + isFetchingRef.current = false; |
132 | 142 | } |
133 | | - |
134 | | - setStatus('success'); |
135 | 143 | }, |
136 | 144 | [notifications], |
137 | 145 | ); |
|
0 commit comments