import { useCallback, useEffect, useState } from "react"; export default function Page() { const [gameReady, setGameReady] = useState(false); const [gameStarted, setGameStarted] = useState(false); useEffect(() => { // Set up callback for when WASM signals it's ready (window as any).pacmanReady = () => { setGameReady(true); }; if (!(window as any).Module) { const canvas = document.getElementById("canvas"); (window as any).Module = { canvas: canvas, locateFile: (path: string) => { return path.startsWith("/") ? path : `/${path}`; }, preRun: [], }; const script = document.createElement("script"); script.src = "/pacman.js"; script.async = false; document.body.appendChild(script); return () => { script.remove(); delete (window as any).pacmanReady; }; } }, []); const handleInteraction = useCallback(() => { if (gameReady && !gameStarted) { // Call the exported Rust function to start the game const module = (window as any).Module; if (module && module._start_game) { module._start_game(); setGameStarted(true); } } }, [gameReady, gameStarted]); // Handle keyboard interaction useEffect(() => { if (!gameReady || gameStarted) return; const handleKeyDown = (e: KeyboardEvent) => { handleInteraction(); }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [gameReady, gameStarted, handleInteraction]); return (