Skip to content

Commit 984bd1a

Browse files
authored
feat: skip fetching if offline (#2586)
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 85e3a80 commit 984bd1a

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/renderer/hooks/timers/useInactivityTimer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useCallback, useEffect, useRef } from 'react';
22

3+
import { isOnline } from '../../utils/network';
4+
35
const events = ['mousedown', 'keypress', 'click'];
46

57
/**
@@ -23,8 +25,10 @@ export const useInactivityTimer = (callback: () => void, delay: number) => {
2325

2426
if (delay !== null && savedCallback.current) {
2527
timeoutRef.current = setTimeout(() => {
26-
// Fire callback once inactivity threshold reached
27-
savedCallback.current();
28+
// Fire callback once inactivity threshold reached if online
29+
if (savedCallback.current && isOnline()) {
30+
savedCallback.current();
31+
}
2832

2933
// Schedule next run while still inactive
3034
resetTimer();

src/renderer/hooks/timers/useIntervalTimer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useEffect, useRef } from 'react';
22

3+
import { isOnline } from '../../utils/network';
4+
35
/**
46
* Hook that triggers a callback after a specified period of time.
57
*
@@ -16,7 +18,9 @@ export const useIntervalTimer = (callback: () => void, delay: number) => {
1618
// Set up the interval.
1719
useEffect(() => {
1820
function tick() {
19-
savedCallback.current();
21+
if (savedCallback.current && isOnline()) {
22+
savedCallback.current();
23+
}
2024
}
2125

2226
if (delay !== null) {

src/renderer/utils/network.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Simple utility to centralize online status checks
2+
export const isOnline = (): boolean => {
3+
return typeof navigator !== 'undefined' ? navigator.onLine : true;
4+
};
5+
6+
export default isOnline;

0 commit comments

Comments
 (0)