import { useCallback, useEffect, useState } from "react";

export default function useWindowFocus() {
    const [isWindowFocused, setIsWindowFocused] = useState(false);

    const windowFocusCb = useCallback(() => {
        setIsWindowFocused(true);
    }, []);

    const windowBlurCb = useCallback(() => {
        setIsWindowFocused(false);
    }, []);

    useEffect(() => {
        window.addEventListener("focus", windowFocusCb);
        window.addEventListener("blur", windowBlurCb);
        return function () {
            window.removeEventListener("focus", windowFocusCb);
            window.removeEventListener("blur", windowBlurCb);
        };
    }, []);

    return { isWindowFocused };
}
