useMouseIdle.js

import { useEffect, useRef } from 'react';

// Detect mouse idle
export const useMouseIdle = (callback, timeout = 10000) => {
  // Because of closure coming into picture we are using useRef instead of useEffect
  const timeoutRef = useRef(null);

  const setFromEvent = () => {
    if (timeoutRef.current) {
      clearTimeout(timeoutRef.current);
    }

    timeoutRef.current = setTimeout(function () {
      callback();
    }, timeout);
  };

  useEffect(() => {
    window.addEventListener('mousemove', setFromEvent);

    return () => {
      window.removeEventListener('mousemove', setFromEvent);
      clearTimeout(timeoutRef.current);
    };
  }, []);
};