All files / app/hooks use-window-focus.ts

0% Statements 0/14
100% Branches 0/0
0% Functions 0/5
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18                                   
import { WindowActiveState } from '@models';
import { useState, useEffect, useCallback } from 'react';
 
export function useWindowFocus() {
  const [windowState, setWindowState] = useState<WindowActiveState>('focused');
  const focusHandler = useCallback(() => setWindowState('focused'), []);
  const blurHandler = useCallback(() => setWindowState('blurred'), []);
  useEffect(() => {
    const removeBlurListener = main.windowEvents.blur(blurHandler);
    const removeFocusListener = main.windowEvents.focus(focusHandler);
    return () => {
      removeBlurListener();
      removeFocusListener();
    };
  }, [blurHandler, focusHandler]);
  return windowState;
}