import { useEffect, useState } from "react";

const MOBILE_WIDTH = 640;

export const useIsMobile = () => {
	const [isMobile, setIsMobile] = useState(false);

	useEffect(() => {
		const checkIsMobile = () =>
			/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
				navigator.userAgent,
			) || window.innerWidth < MOBILE_WIDTH;

		setIsMobile(checkIsMobile());

		const handleResize = () => setIsMobile(checkIsMobile());

		window.addEventListener("resize", handleResize);
		return () => window.removeEventListener("resize", handleResize);
	}, []);

	return isMobile;
};
