{"size": 700, "ext": "js", "max_stars_repo_path": "components/common/useSingleAndDoubleClick.js", "max_stars_repo_name": "sbx0/nextjs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "components/common/useSingleAndDoubleClick.js", "max_issues_repo_name": "sbx0/nextjs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "components/common/useSingleAndDoubleClick.js", "max_forks_repo_name": "sbx0/nextjs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 29.1666666667, "max_line_length": 100, "alphanum_fraction": 0.5957142857}
import {useEffect, useState} from 'react';

// https://stackoverflow.com/a/63891352/8608749
export default function useSingleAndDoubleClick(actionSimpleClick, actionDoubleClick, delay = 250) {
    const [click, setClick] = useState(0);

    useEffect(() => {
        const timer = setTimeout(() => {
            // simple click
            if (click === 1) actionSimpleClick();
            setClick(0);
        }, delay);

        // the duration between this click and the previous one
        // is less than the value of delay = double-click
        if (click === 2) actionDoubleClick();

        return () => clearTimeout(timer);

    }, [click]);

    return () => setClick(prev => prev + 1);
}
