import React from "react";

type Params = {
    timeout?: number;
};

let timeout: any;

export default function useReady(params?: Params) {
    const [ready, setReady] = React.useState(false);

    const finalTimeout = params?.timeout || 300;

    React.useEffect(() => {
        requestAnimationFrame(() => {
            window.clearTimeout(timeout);

            timeout = setTimeout(() => {
                setReady(true);
            }, finalTimeout);
        });

        return function () {
            window.clearTimeout(timeout);
        };
    }, []);

    return { ready };
}
