From af679f88551205753885068666ff4e9cad845b8c Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Sun, 9 Mar 2025 21:59:57 +0100 Subject: add emed start button --- src/components/MorseGame.tsx | 171 +++++++++++++++++++++++++------------------ 1 file changed, 99 insertions(+), 72 deletions(-) (limited to 'src/components/MorseGame.tsx') diff --git a/src/components/MorseGame.tsx b/src/components/MorseGame.tsx index 0a3b902..c140e92 100644 --- a/src/components/MorseGame.tsx +++ b/src/components/MorseGame.tsx @@ -24,6 +24,7 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { const [wpm, setWpm] = useState(20); const [showHints, setShowHints] = useState(false); const [selectedOption, setSelectedOption] = useState(null); + const [gameStarted, setGameStarted] = useState(!embed); // Auto-start if not in embed mode const actxRef = useRef(null); const optionButtonsRef = useRef>([null, null, null, null]); @@ -43,10 +44,12 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { actxRef.current = cw.initAudioContext({ tone: 600 }); isInitializedRef.current = true; - // Start the first round after audio context is initialized - setTimeout(() => { - newRound(); - }, 100); + // Start the first round after audio context is initialized if not in embed mode or if game already started + if (gameStarted) { + setTimeout(() => { + newRound(); + }, 100); + } } catch (error) { console.error('Failed to initialize audio context:', error); } @@ -56,14 +59,17 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { return () => { if (actxRef.current) { try { - actxRef.current.close(); + // Check if close method exists before calling it + if (typeof actxRef.current.close === 'function') { + actxRef.current.close(); + } actxRef.current = null; } catch (error) { - console.error('Error closing audio context:', error); + console.error('Error cleaning up audio context:', error); } } }; - }, []); + }, [gameStarted]); // Generate a new round const newRound = () => { @@ -222,80 +228,101 @@ const MorseGame: React.FC = ({ onReset, embed = false }) => { } }; + // Start the game (for embed mode) + const startGame = () => { + setGameStarted(true); + setTimeout(() => { + newRound(); + }, 100); + }; + return (
-
-
-
-
Score:
-
- {score} / {totalPlayed} + {embed && !gameStarted ? ( +
+ +
+ ) : ( + <> +
+
+
+
Score:
+
+ {score} / {totalPlayed} +
+
+
-
-
-
-
-
-

Listen to the Morse code and select the correct character

-
+
+
+

Listen to the Morse code and select the correct character

+
-
- {options.map((option, index) => ( - - ))} -
+
+ {options.map((option, index) => ( + + ))} +
-
-
- - -
-
- setShowHints(e.target.checked)} - /> - -
-
+
+
+ + +
+
+ setShowHints(e.target.checked)} + /> + +
+
- {!embed && ( -
-

Pro Tip: Use your keyboard to select options faster!

+ {!embed && ( +
+

Pro Tip: Use your keyboard to select options faster!

+
+ )}
- )} -
+ + )}
); }; -- cgit v1.3.1