MCPcopy
hub / github.com/gitify-app/gitify / useInactivityTimer

Function useInactivityTimer

src/renderer/hooks/timers/useInactivityTimer.ts:14–70  ·  view source on GitHub ↗
(
  callback: () => void,
  delay: number | null,
)

Source from the content-addressed store, hash-verified

12 * @param delay - Inactivity timeout in milliseconds.
13 */
14export const useInactivityTimer = (
15 callback: () => void,
16 delay: number | null,
17) => {
18 const savedCallback = useRef<(() => void) | null>(null);
19 const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
20
21 // Remember the latest callback
22 useEffect(() => {
23 savedCallback.current = callback;
24 }, [callback]);
25
26 // Reset the inactivity timer
27 const resetTimer = useCallback(() => {
28 if (timeoutRef.current) {
29 clearTimeout(timeoutRef.current);
30 }
31
32 if (delay !== null && savedCallback.current) {
33 timeoutRef.current = setTimeout(() => {
34 // Fire callback once inactivity threshold reached if online
35 if (savedCallback.current && isOnline()) {
36 savedCallback.current();
37 }
38
39 // Schedule next run while still inactive
40 resetTimer();
41 }, delay);
42 }
43 }, [delay]);
44
45 // Set up event listeners for user activity
46 useEffect(() => {
47 if (delay === null) {
48 return;
49 }
50
51 // Add event listeners to track activity
52 for (const event of events) {
53 document.addEventListener(event, resetTimer, { passive: true });
54 }
55
56 // Start initial timer
57 resetTimer();
58
59 // Cleanup function
60 return () => {
61 if (timeoutRef.current) {
62 clearTimeout(timeoutRef.current);
63 }
64
65 for (const event of events) {
66 document.removeEventListener(event, resetTimer);
67 }
68 };
69 }, [delay, resetTimer]);
70};

Callers 1

AppProviderFunction · 0.90

Calls 1

isOnlineFunction · 0.90

Tested by

no test coverage detected