diff options
| author | Yuval Adam <_@yuv.al> | 2025-03-05 12:21:25 +0100 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2025-03-05 12:21:25 +0100 |
| commit | 5bf781f3ac381744fbec035f938d69a201f75fa4 (patch) | |
| tree | 2ec82a90ce2cd1c279d77bdd6ca83b256ac4856a /src/pages | |
| parent | c6612a8b6dde5a5a9776d424ed33f5ca685699c6 (diff) | |
Replace all impl with vite
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/HomePage.tsx | 294 | ||||
| -rw-r--r-- | src/pages/chart/ChartPage.tsx | 178 | ||||
| -rw-r--r-- | src/pages/chart/styles.css | 132 |
3 files changed, 604 insertions, 0 deletions
diff --git a/src/pages/HomePage.tsx b/src/pages/HomePage.tsx new file mode 100644 index 0000000..bf07bd5 --- /dev/null +++ b/src/pages/HomePage.tsx @@ -0,0 +1,294 @@ +import { useState, useEffect, useRef } from 'react'; +import { Link } from 'react-router'; +import * as cw from 'cw'; +import '../base.css'; +import '../styles.css'; + +declare global { + interface Window { + cw: any; + } +} + +const HomePage = () => { + // Game variables + const [gameStarted, setGameStarted] = useState(false); + const [score, setScore] = useState(0); + const [totalPlayed, setTotalPlayed] = useState(0); + const [currentChar, setCurrentChar] = useState(''); + const [feedback, setFeedback] = useState(''); + const [options, setOptions] = useState<string[]>([]); + const [wpm, setWpm] = useState(20); + const [showHints, setShowHints] = useState(false); + + const actxRef = useRef<any>(null); + const optionButtonsRef = useRef<Array<HTMLButtonElement | null>>([null, null, null, null]); + + // Define available characters + const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + const numbers = '0123456789'; + const specialChars = '.,?/='; + const allChars = letters + numbers + specialChars; + + // Initialize the game when started - this happens on user interaction (button click) + const startGame = () => { + try { + // Initialize audio context on user interaction + actxRef.current = cw.initAudioContext({ tone: 600 }); + setGameStarted(true); + newRound(); + } catch (error) { + console.error('Failed to initialize audio context:', error); + alert('Failed to initialize audio. Please try again.'); + } + }; + + // Generate a new round + const newRound = () => { + // Clear feedback + setFeedback(''); + + // Reset button states + optionButtonsRef.current.forEach(button => { + if (button) { + button.disabled = false; + button.classList.remove('correct', 'incorrect', 'key-pressed'); + } + }); + + // Generate a random character + const randomIndex = Math.floor(Math.random() * allChars.length); + const newChar = allChars[randomIndex]; + setCurrentChar(newChar); + + // Generate options (one correct, three random) + let newOptions = [newChar]; + + // Add three random unique characters + while (newOptions.length < 4) { + const randomChar = allChars[Math.floor(Math.random() * allChars.length)]; + if (!newOptions.includes(randomChar)) { + newOptions.push(randomChar); + } + } + + // Shuffle options + newOptions = shuffleArray(newOptions); + setOptions(newOptions); + + // Play the Morse code for the character + playMorseCode(newChar); + }; + + // Play Morse code for a character + const playMorseCode = (char: string) => { + if (actxRef.current) { + cw.play(char, { + wpm: wpm, + actx: actxRef.current + }); + } + }; + + // Check if the answer is correct + const checkAnswer = (selectedChar: string) => { + if (selectedChar === currentChar) { + // Correct answer + setFeedback('Correct!'); + setScore(prevScore => prevScore + 1); + + // Add to score tracker + const scoreTracker = document.getElementById('scoreTracker'); + if (scoreTracker) { + const marker = document.createElement('div'); + marker.className = 'score-marker correct'; + scoreTracker.appendChild(marker); + } + + // Disable all buttons + optionButtonsRef.current.forEach(button => { + if (button) button.disabled = true; + }); + + // Highlight the correct button + const correctButtonIndex = options.indexOf(currentChar); + if (correctButtonIndex !== -1 && optionButtonsRef.current[correctButtonIndex]) { + const button = optionButtonsRef.current[correctButtonIndex]; + if (button) button.classList.add('correct'); + } + + // Move to next round after delay + setTimeout(newRound, 1500); + } else { + // Incorrect answer + setFeedback('Incorrect! The correct answer was ' + currentChar); + + // Add to score tracker + const scoreTracker = document.getElementById('scoreTracker'); + if (scoreTracker) { + const marker = document.createElement('div'); + marker.className = 'score-marker incorrect'; + scoreTracker.appendChild(marker); + } + + // Disable all buttons + optionButtonsRef.current.forEach(button => { + if (button) button.disabled = true; + }); + + // Highlight the incorrect button and show the correct one + const selectedButtonIndex = options.indexOf(selectedChar); + if (selectedButtonIndex !== -1 && optionButtonsRef.current[selectedButtonIndex]) { + const button = optionButtonsRef.current[selectedButtonIndex]; + if (button) button.classList.add('incorrect'); + } + + const correctButtonIndex = options.indexOf(currentChar); + if (correctButtonIndex !== -1 && optionButtonsRef.current[correctButtonIndex]) { + const button = optionButtonsRef.current[correctButtonIndex]; + if (button) button.classList.add('correct'); + } + + // Move to next round after delay + setTimeout(newRound, 2000); + } + + setTotalPlayed(prevTotal => prevTotal + 1); + }; + + // Handle keyboard input + useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if (!gameStarted) return; + + const key = event.key.toUpperCase(); + + // Find if any button has this character + const index = options.findIndex(option => option === key); + if (index !== -1) { + const button = optionButtonsRef.current[index]; + if (button && !button.disabled) { + button.classList.add('key-pressed'); + setTimeout(() => { + button.classList.remove('key-pressed'); + checkAnswer(key); + }, 100); + } + } + }; + + document.addEventListener('keydown', handleKeyDown); + return () => { + document.removeEventListener('keydown', handleKeyDown); + }; + }, [gameStarted, options]); + + // Utility function to shuffle an array + const shuffleArray = (array: string[]) => { + const newArray = [...array]; + for (let i = newArray.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [newArray[i], newArray[j]] = [newArray[j], newArray[i]]; + } + return newArray; + }; + + // Get Morse code representation for a character + const getMorseCode = (char: string) => { + const morseMap: { [key: string]: string } = { + 'A': '·−', 'B': '−···', 'C': '−·−·', 'D': '−··', 'E': '·', + 'F': '··−·', 'G': '−−·', 'H': '····', 'I': '··', 'J': '·−−−', + 'K': '−·−', 'L': '·−··', 'M': '−−', 'N': '−·', 'O': '−−−', + 'P': '·−−·', 'Q': '−−·−', 'R': '·−·', 'S': '···', 'T': '−', + 'U': '··−', 'V': '···−', 'W': '·−−', 'X': '−··−', 'Y': '−·−−', + 'Z': '−−··', '0': '−−−−−', '1': '·−−−−', '2': '··−−−', '3': '···−−', + '4': '····−', '5': '·····', '6': '−····', '7': '−−···', '8': '−−−··', + '9': '−−−−·', '.': '·−·−·−', ',': '−−··−−', '?': '··−−··', '/': '−··−·', '=': '−···−' + }; + + return morseMap[char] || ''; + }; + + return ( + <div className="container"> + <h1><Link to="/" className="title-link">Morse Hero</Link></h1> + + {!gameStarted ? ( + <div id="startScreen"> + <p>Learn Morse code the fun way!</p> + <button id="startButton" className="start-button" onClick={startGame}>Start</button> + <div className="chart-link"> + <Link to="/chart">View Morse Code Chart</Link> + </div> + </div> + ) : ( + <div id="gameArea" style={{ display: 'block' }}> + <div className="score-container"> + <div className="score-widget"> + <div className="score-info"> + <div className="score-label">Score:</div> + <div className="score-value"> + <span id="score">{score}</span> / <span id="total">{totalPlayed}</span> + </div> + </div> + <div className="score-tracker" id="scoreTracker"></div> + </div> + </div> + + <div className="game-container"> + <div className="options"> + {options.map((option, index) => ( + <button + key={index} + className="option-button" + ref={el => { + optionButtonsRef.current[index] = el; + }} + onClick={() => checkAnswer(option)} + > + <span className="char-display">{option}</span> + {showHints && <span className="morse-hint">{getMorseCode(option)}</span>} + </button> + ))} + </div> + + <div className="feedback" id="feedback">{feedback}</div> + </div> + + <div className="settings"> + <label htmlFor="wpmSelect">Speed:</label> + <select + id="wpmSelect" + value={wpm} + onChange={(e) => setWpm(parseInt(e.target.value))} + > + <option value="10">10 WPM</option> + <option value="15">15 WPM</option> + <option value="20">20 WPM</option> + <option value="25">25 WPM</option> + <option value="30">30 WPM</option> + </select> + <div className="hint-setting"> + <input + type="checkbox" + id="hintMode" + name="hintMode" + checked={showHints} + onChange={(e) => setShowHints(e.target.checked)} + /> + <label htmlFor="hintMode">Show Hints</label> + </div> + </div> + </div> + )} + + <footer className="footer"> + <p>Need help? View the <Link to="/chart">Morse Code Chart</Link>. </p> + <p>Created with <a href="https://www.mastercw.com/cw.js/">CW.js</a>. For a complete and professional + Morse Code training solution visit <a href="https://www.mastercw.com">Master CW</a>.</p> + </footer> + </div> + ); +}; + +export default HomePage; diff --git a/src/pages/chart/ChartPage.tsx b/src/pages/chart/ChartPage.tsx new file mode 100644 index 0000000..88c1ec2 --- /dev/null +++ b/src/pages/chart/ChartPage.tsx @@ -0,0 +1,178 @@ +import { useEffect, useRef } from 'react'; +import { Link } from 'react-router'; +import * as cw from 'cw'; +import '../../base.css'; +import './styles.css'; + +declare global { + interface Window { + cw: any; + } +} + +const ChartPage = () => { + const actxRef = useRef<any>(null); + + // Initialize audio context + const initAudio = () => { + if (actxRef.current) return; // Already initialized + + try { + actxRef.current = cw.initAudioContext({ tone: 600 }); + } catch (error) { + console.error('Failed to initialize audio context:', error); + } + }; + + // Play Morse code for a character + const playCharacter = (character: string) => { + // Initialize audio on first user interaction + if (!actxRef.current) { + initAudio(); + } + + if (actxRef.current) { + // Remove 'playing' class from all items + document.querySelectorAll('.morse-item').forEach(item => { + item.classList.remove('playing'); + }); + + // Add 'playing' class to the clicked item + const clickedItem = document.querySelector(`.morse-item[data-char="${character}"]`); + if (clickedItem) { + clickedItem.classList.add('playing'); + } + + cw.play(character, { + wpm: 20, + actx: actxRef.current + }); + + // Remove the 'playing' class after the audio finishes + setTimeout(() => { + if (clickedItem) { + clickedItem.classList.remove('playing'); + } + }, 2000); + } + }; + + // Add click event listeners to all morse items + useEffect(() => { + const handleMorseItemClick = (e: Event) => { + const target = e.currentTarget as HTMLElement; + const character = target.getAttribute('data-char'); + if (character) { + playCharacter(character); + } + }; + + const morseItems = document.querySelectorAll('.morse-item'); + morseItems.forEach(item => { + item.addEventListener('click', handleMorseItemClick); + }); + + return () => { + morseItems.forEach(item => { + item.removeEventListener('click', handleMorseItemClick); + }); + }; + }, []); + + return ( + <div className="container"> + <h1><Link to="/" className="title-link">Morse Hero</Link></h1> + <h2>Morse Code Chart</h2> + + <div className="chart-section"> + <h2>Letters</h2> + <div className="morse-grid"> + {[...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'].map(char => ( + <div key={char} className="morse-item" data-char={char}> + <div className="character">{char}</div> + <div className="morse">{getMorseCode(char)}</div> + </div> + ))} + </div> + </div> + + <div className="chart-section"> + <h2>Numbers</h2> + <div className="morse-grid"> + {[...'0123456789'].map(char => ( + <div key={char} className="morse-item" data-char={char}> + <div className="character">{char}</div> + <div className="morse">{getMorseCode(char)}</div> + </div> + ))} + </div> + </div> + + <div className="chart-section"> + <h2>Special Characters</h2> + <div className="morse-grid"> + {[...'.=,?/'].map(char => ( + <div key={char} className="morse-item" data-char={char}> + <div className="character">{char}</div> + <div className="morse">{getMorseCode(char)}</div> + </div> + ))} + </div> + </div> + + <div className="chart-section"> + <h2>Prosigns</h2> + <div className="morse-grid"> + <div className="morse-item"> + <div className="character">AR</div> + <div className="morse">·−·−·</div> + <div className="description">End of message</div> + </div> + <div className="morse-item"> + <div className="character">SK</div> + <div className="morse">···−·−</div> + <div className="description">End of contact</div> + </div> + <div className="morse-item"> + <div className="character">BT</div> + <div className="morse">−···−</div> + <div className="description">Break</div> + </div> + <div className="morse-item"> + <div className="character">KN</div> + <div className="morse">−·−−·</div> + <div className="description">Go only</div> + </div> + </div> + </div> + + <div className="back-link"> + <Link to="/">Back to Morse Hero</Link> + </div> + + <footer className="footer"> + <p>Click on any character to hear its Morse code sound.</p> + <p>Created with <a href="https://www.mastercw.com/cw.js/">CW.js</a>. For a complete and professional + Morse Code training solution visit <a href="https://www.mastercw.com">Master CW</a>.</p> + </footer> + </div> + ); +}; + +// Helper function to get morse code representation +const getMorseCode = (char: string) => { + const morseMap: { [key: string]: string } = { + 'A': '·−', 'B': '−···', 'C': '−·−·', 'D': '−··', 'E': '·', + 'F': '··−·', 'G': '−−·', 'H': '····', 'I': '··', 'J': '·−−−', + 'K': '−·−', 'L': '·−··', 'M': '−−', 'N': '−·', 'O': '−−−', + 'P': '·−−·', 'Q': '−−·−', 'R': '·−·', 'S': '···', 'T': '−', + 'U': '··−', 'V': '···−', 'W': '·−−', 'X': '−··−', 'Y': '−·−−', + 'Z': '−−··', '0': '−−−−−', '1': '·−−−−', '2': '··−−−', '3': '···−−', + '4': '····−', '5': '·····', '6': '−····', '7': '−−···', '8': '−−−··', + '9': '−−−−·', '.': '·−·−·−', ',': '−−··−−', '?': '··−−··', '/': '−··−·', '=': '−···−' + }; + + return morseMap[char] || ''; +}; + +export default ChartPage; diff --git a/src/pages/chart/styles.css b/src/pages/chart/styles.css new file mode 100644 index 0000000..279d421 --- /dev/null +++ b/src/pages/chart/styles.css @@ -0,0 +1,132 @@ +/* Morse Hero Chart Styles */ + +h1 { + font-size: 48px; +} + +h2 { + font-size: 32px; + margin: 30px 0 15px; + color: var(--primary-color); + text-shadow: 2px 2px 0px rgba(0, 0, 0, 0.2), + 0 0 10px rgba(52, 152, 219, 0.4); + font-family: 'Fredoka', sans-serif; + font-weight: 600; +} + +.chart-section { + background: linear-gradient(145deg, var(--bg-light) 0%, #2d3e50 100%); + border-radius: var(--border-radius-md); + padding: 30px 25px; + margin-bottom: 40px; + box-shadow: var(--shadow-lg); + position: relative; + overflow: hidden; +} + +.chart-section::before { + content: ""; + position: absolute; + top: 0; + left: 0; + right: 0; + height: 5px; + background: linear-gradient(90deg, var(--primary-color), var(--secondary-color)); + z-index: 1; +} + +.morse-grid { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); + gap: 20px; + margin: 0 auto; +} + +.morse-item { + background: linear-gradient(145deg, rgba(52, 152, 219, 0.1) 0%, rgba(52, 152, 219, 0.2) 100%); + border-radius: var(--border-radius-sm); + padding: 20px 15px; + display: flex; + flex-direction: column; + align-items: center; + box-shadow: var(--shadow-sm); + transition: all var(--transition-normal); + cursor: pointer; + border: 1px solid rgba(52, 152, 219, 0.2); +} + +.morse-item:hover { + transform: translateY(-5px); + box-shadow: var(--shadow-md); + background: linear-gradient(145deg, rgba(52, 152, 219, 0.2) 0%, rgba(52, 152, 219, 0.3) 100%); + border-color: rgba(52, 152, 219, 0.3); +} + +.morse-item.playing { + background: linear-gradient(145deg, rgba(243, 156, 18, 0.2) 0%, rgba(243, 156, 18, 0.3) 100%); + box-shadow: 0 0 20px rgba(243, 156, 18, 0.4); + border-color: rgba(243, 156, 18, 0.4); +} + +.character { + font-size: 32px; + font-weight: bold; + margin-bottom: 10px; + text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2); +} + +.morse { + font-family: monospace; + font-size: 18px; + letter-spacing: 2px; + color: var(--text-dim); +} + +.back-link { + margin-top: 40px; + font-size: 18px; +} + +.back-link a { + color: var(--primary-color); + text-decoration: none; + transition: all var(--transition-normal); + padding: 10px 20px; + background-color: rgba(255, 255, 255, 0.05); + border-radius: var(--border-radius-sm); + display: inline-block; +} + +.back-link a:hover { + color: var(--secondary-color); + background-color: rgba(255, 255, 255, 0.1); + transform: translateY(-3px); + box-shadow: var(--shadow-sm); +} + +@media (max-width: 768px) { + .morse-grid { + grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); + gap: 15px; + } + + .morse-item { + padding: 15px 10px; + } + + .character { + font-size: 28px; + } + + .morse { + font-size: 16px; + } + + h1 { + font-size: 36px; + } + + h2 { + font-size: 24px; + } +} |
