import { useState, useEffect, type ReactNode } from "react"; import { Flashlight, FlashlightOff } from "lucide-react"; import SplatterImage from "../assets/splatter.png"; import Countdown from "./Countdown"; export default function Layout({ children }: { children: ReactNode }) { const [pointerPosition, setPointerPosition] = useState({ x: 500, y: 500 }); const [isActive, setIsActive] = useState(false); const [isTouchDevice, setIsTouchDevice] = useState(false); const [isFlashlightEnabled, setIsFlashlightEnabled] = useState(false); // Detect touch device on component mount useEffect(() => { const isTouchCapable = "ontouchstart" in window || navigator.maxTouchPoints > 0; setIsTouchDevice(isTouchCapable); }, []); // Handle mouse events for desktop const handleMouseMove = (e: React.MouseEvent) => { setPointerPosition({ x: e.clientX, y: e.clientY }); setIsActive(true); }; const handleMouseLeave = () => { setIsActive(false); }; // Handle touch events for mobile const handleTouchMove = (e: React.TouchEvent) => { if (e.touches && e.touches[0]) { e.preventDefault(); // Prevent scrolling while touching const touch = e.touches[0]; setPointerPosition({ x: touch.clientX, y: touch.clientY }); setIsActive(true); } }; const handleTouchStart = (e: React.TouchEvent) => { if (e.touches && e.touches[0]) { const touch = e.touches[0]; setPointerPosition({ x: touch.clientX, y: touch.clientY }); setIsActive(true); } }; const handleTouchEnd = () => { // On mobile, we'll keep the last position visible for a moment setTimeout(() => { setIsActive(false); }, 1000); }; useEffect(() => { // Define window event handlers with correct types const windowMouseMove = (e: MouseEvent) => { setPointerPosition({ x: e.clientX, y: e.clientY }); setIsActive(true); }; const windowMouseLeave = () => { setIsActive(false); }; const windowTouchStart = (e: TouchEvent) => { if (e.touches && e.touches[0]) { const touch = e.touches[0]; setPointerPosition({ x: touch.clientX, y: touch.clientY }); setIsActive(true); } }; const windowTouchMove = (e: TouchEvent) => { if (e.touches && e.touches[0]) { e.preventDefault?.(); // Optional chaining in case preventDefault isn't available const touch = e.touches[0]; setPointerPosition({ x: touch.clientX, y: touch.clientY }); setIsActive(true); } }; const windowTouchEnd = () => { // On mobile, we'll keep the last position visible for a moment setTimeout(() => { setIsActive(false); }, 1000); }; if (isFlashlightEnabled) { // Add desktop event listeners window.addEventListener("mousemove", windowMouseMove); window.addEventListener("mouseleave", windowMouseLeave); // Add mobile event listeners window.addEventListener("touchstart", windowTouchStart, { passive: false, }); window.addEventListener("touchmove", windowTouchMove, { passive: false }); window.addEventListener("touchend", windowTouchEnd); } else { // Remove desktop event listeners window.removeEventListener("mousemove", windowMouseMove); window.removeEventListener("mouseleave", windowMouseLeave); // Remove mobile event listeners window.removeEventListener("touchstart", windowTouchStart); window.removeEventListener("touchmove", windowTouchMove); window.removeEventListener("touchend", windowTouchEnd); } // Cleanup when component unmounts return () => { window.removeEventListener("mousemove", windowMouseMove); window.removeEventListener("mouseleave", windowMouseLeave); window.removeEventListener("touchstart", windowTouchStart); window.removeEventListener("touchmove", windowTouchMove); window.removeEventListener("touchend", windowTouchEnd); }; }, [isFlashlightEnabled]); return (