-
Notifications
You must be signed in to change notification settings - Fork 3
Fixed Long para typing issue #3 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c30cd0e
9182f8d
aca0f97
6446fa5
55b703d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,8 +2,12 @@ import React, { useEffect, useState } from 'react'; | |||||
| import Timer from './Timer'; | ||||||
| import { motion, time } from "motion/react" | ||||||
| import TypingCompleteScreen from './TypingCompleteScreen'; | ||||||
| import reload from '../assets/reload.svg'; | ||||||
| import { useLocation } from 'react-router-dom'; | ||||||
|
|
||||||
|
|
||||||
| const TypingBoard = (props) => { | ||||||
| // console.log(props) | ||||||
| const [words, setWords] = useState([]) | ||||||
| const [quote, setQuote] = useState(props.quotes) | ||||||
|
|
||||||
|
|
@@ -18,13 +22,66 @@ const TypingBoard = (props) => { | |||||
| const [WPM, setWPM] = useState(0) | ||||||
| const [accuracy, setAccuracy] = useState(0) | ||||||
|
|
||||||
|
|
||||||
| const resetGameStates = () => { | ||||||
| setCurrentIdx(0); | ||||||
| setTypedChars(""); | ||||||
| setWordsPressed(0); | ||||||
| setRightWords(0); | ||||||
| setWarning(true); | ||||||
| setTypos(0); | ||||||
| setIsCompeleted(false); | ||||||
| setFinalTime(0); | ||||||
| setWPM(0); | ||||||
| setAccuracy(0); | ||||||
| }; | ||||||
|
|
||||||
| const handleGameRestart = () => { | ||||||
| if (props.onRestart && typeof props.onRestart === 'function') { | ||||||
| props.onRestart(); // Triggers parent to update quotes prop | ||||||
| } else { | ||||||
| // fallback: reset current quote manually | ||||||
| const cleanedQuote = props.quotes.trim().replace(/\s+/g, ' '); | ||||||
| setQuote(cleanedQuote); | ||||||
| resetGameStates(); | ||||||
| } | ||||||
| }; | ||||||
| // Add this useEffect to update quote when props change and reset all states | ||||||
| useEffect(() => { | ||||||
| // Clean the quote string to remove any hidden characters | ||||||
| const cleanedQuote = props.quotes.trim().replace(/\s+/g, ' '); | ||||||
| setQuote(cleanedQuote); | ||||||
|
|
||||||
| // Reset all states when quote changes | ||||||
| setCurrentIdx(0); | ||||||
| setTypedChars(""); | ||||||
| setWordsPressed(0); | ||||||
| setRightWords(0); | ||||||
| setWarning(true); | ||||||
| setTypos(0); | ||||||
| setIsCompeleted(false); | ||||||
| setFinalTime(0); | ||||||
| setWPM(0); | ||||||
| setAccuracy(0); | ||||||
|
|
||||||
| // console.log('Quote updated:', cleanedQuote); | ||||||
| // console.log('Quote length:', cleanedQuote.length); | ||||||
| // console.log('Quote characters:', cleanedQuote.split('').map((char, i) => `${i}: "${char}" (${char.charCodeAt(0)})`)); | ||||||
| }, [props.quotes]); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
|
|
||||||
| const handleKeyDown = (e) => { | ||||||
| const pressedKey = e.key; | ||||||
|
|
||||||
| // Don't process if typing is completed | ||||||
| if (isCompeleted) return; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (typo): Correct spelling of Use the correct spelling ( Suggested implementation: // Don't process if typing is completed
if (isCompleted) return;You will also need to rename all other occurrences of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): Use block braces for ifs, whiles, etc. (
Suggested change
ExplanationIt is recommended to always use braces and create explicit statement blocks.Using the allowed syntax to just write a single statement can lead to very confusing |
||||||
|
|
||||||
| if (/^[a-zA-Z0-9\s]$/.test(pressedKey)) { | ||||||
| // Allow more characters including punctuation | ||||||
| if (/^[a-zA-Z0-9\s.,!?;:'"()-]$/.test(pressedKey)) { | ||||||
| setWordsPressed(prev => prev + 1) | ||||||
|
|
||||||
| // console.log(`Pressed: "${pressedKey}" (${pressedKey.charCodeAt(0)}), Expected: "${quote[currentIdx]}" (${quote[currentIdx]?.charCodeAt(0)}), CurrentIdx: ${currentIdx}`); | ||||||
|
|
||||||
| if (pressedKey === quote[currentIdx]) { | ||||||
| const nextIdx = currentIdx + 1; | ||||||
|
|
@@ -34,10 +91,10 @@ const TypingBoard = (props) => { | |||||
| setCurrentIdx(nextIdx); | ||||||
| setTypedChars(prev => prev + pressedKey); | ||||||
| setRightWords(prev => prev + 1) | ||||||
| setWarning(prev => prev = true) | ||||||
| setWarning(true) // Fixed assignment operator | ||||||
|
|
||||||
| } else { | ||||||
| setWarning(prev => prev = false) | ||||||
| setWarning(false) // Fixed assignment operator | ||||||
| setTypos(prev => prev + 1) | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -74,6 +131,23 @@ const TypingBoard = (props) => { | |||||
| {/* timer */} | ||||||
| <Timer isCompeleted={isCompeleted} setFinalTime={setFinalTime} /> | ||||||
|
|
||||||
| {/* handling the restart button */} | ||||||
| <button | ||||||
| tabIndex={-1} | ||||||
| // onClick={handleGameRestart} | ||||||
| onMouseDown={(e) => { | ||||||
| e.preventDefault(); // prevent focusing on mousedown | ||||||
| handleGameRestart(); | ||||||
| }} | ||||||
| onKeyDown={(e) => { | ||||||
| e.preventDefault(); // block space/enter key presses | ||||||
| e.stopPropagation(); | ||||||
| }} | ||||||
| className='reload inline p-2 uppercase font-med bg-white/10 rounded-md border-1 border-gray-500 hover:bg-white/20 transition-colors' | ||||||
| > | ||||||
| <img src={reload} alt="reload" className="h-5" /> | ||||||
| </button> | ||||||
|
|
||||||
|
|
||||||
| </div> | ||||||
|
|
||||||
|
|
@@ -139,4 +213,4 @@ const TypingBoard = (props) => { | |||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| export default TypingBoard; | ||||||
| export default TypingBoard; | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider refactoring multiple related useState hooks into a single useReducer with a RESET action to centralize and simplify state management.
Here’s one way to collapse all of those individual setters into a single reducer with a
RESETaction. You’ll end up with one state object and one place that describes how it changes:Benefits: