Skip to content

Conversation

@nee-hit476
Copy link

@nee-hit476 nee-hit476 commented May 29, 2025

  • Did Some changes in state management bcoz due to unproper state management long para events not listing properly.
  • Added reloaddocument for navlinks
  • Added a restart button

After Changes

solved.mp4

Before Changes

not.mp4

Summary by Sourcery

Improve typing board state management and input validation to support long paragraphs, add restart functionality on completion, and ensure proper page reloads on navigation

New Features:

  • Add restart button with reload icon to TypingCompleteScreen for quick test restart

Bug Fixes:

  • Reset and clean quote state on props change to fix long paragraph typing issues
  • Fix incorrect warning state assignment in TypingBoard
  • Extend key handling to accept punctuation characters

Enhancements:

  • Normalize quote strings by trimming and collapsing whitespace on update
  • Enable NavLink reloadDocument prop for proper page reload on navigation

Chores:

  • Add reload.svg asset

@vercel
Copy link

vercel bot commented May 29, 2025

@its-nihit-0407 is attempting to deploy a commit to the deepak's projects Team on Vercel.

A member of the Team first needs to authorize it.

@sourcery-ai
Copy link

sourcery-ai bot commented May 29, 2025

Reviewer's Guide

This PR fixes the long-paragraph typing issue by improving state management and input handling in TypingBoard, adding a restart button with reload fallback in the completion screen, and ensuring navigation links force full page reloads.

Sequence Diagram: Game Restart Process from TypingCompleteScreen

sequenceDiagram
    actor User
    participant TCS as TypingCompleteScreen
    participant ParentComponent as Parent Component (Handles Restart)
    participant Window as Browser Window

    User->>TCS: Clicks Restart Button
    activate TCS
    TCS->>TCS: Calls handleGameRestart()
    alt onRestart prop is available (function)
        TCS->>ParentComponent: Calls onRestart()
        activate ParentComponent
        ParentComponent->>ParentComponent: Resets game state / reloads data
        deactivate ParentComponent
    else onRestart prop is not available
        TCS->>Window: Calls window.location.reload()
    end
    deactivate TCS
Loading

Sequence Diagram: Navigation with Full Page Reload via reloadDocument

sequenceDiagram
    actor User
    participant SB as SideBar
    participant NL as NavLink / ParaLink
    participant Browser

    User->>SB: Clicks a navigation link (e.g., "infinite mode")
    SB->>NL: Renders with reloadDocument prop set to true
    NL->>Browser: Navigates to link's target route
    Browser->>Browser: Performs a full page reload and fetches new page
    activate Browser
    deactivate Browser
Loading

Class Diagram: Updated TypingBoard Component

classDiagram
    class TypingBoard {
        +State:
        -quote: string
        -currentIdx: number
        -typedChars: string
        -wordsPressed: number
        -rightWords: number
        -warning: boolean
        -typos: number
        -isCompeleted: boolean
        -finalTime: number
        -WPM: number
        -accuracy: number
        +Props:
        -quotes: string
        +Key Logic:
        #useEffect_onQuotesChange() : void (New: Cleans quote, resets all game-related states when props.quotes changes)
        #handleKeyDown(event) : void (Modified: Ignores input if completed, allows wider range of chars e.g. punctuation, fixed warning state update)
    }
Loading

Class Diagram: Updated TypingCompleteScreen Component

classDiagram
    class TypingCompleteScreen {
        +Props:
        -finalTime: number
        -WPM: number
        -accuracy: number
        -onRestart: function (New)
        +Key Logic:
        #handleGameRestart() : void (New: Calls onRestart prop if it's a function, otherwise reloads the page)
    }
Loading

File-Level Changes

Change Details Files
Reset and sanitize quote state on prop updates and expand allowed input characters
  • Added a useEffect to trim, collapse whitespace, and reset all typing states when props.quotes changes
  • Stopped processing keystrokes once typing is completed
  • Extended the keydown regex to include punctuation marks
  • Corrected setWarning calls to remove redundant assignment operators
src/components/TypingBoard.jsx
Introduce a restart button with reload fallback in the completion screen
  • Imported reload.svg and added an onRestart prop to TypingCompleteScreen
  • Implemented handleGameRestart to call onRestart or fallback to window.location.reload()
  • Inserted a reload button with the SVG icon and updated JSX to display accuracy inline with “%”
src/components/TypingCompleteScreen.jsx
src/assets/reload.svg
Force full page reload on navigation for mode switches
  • Added reloadDocument prop to NavLink components for infinite and trial modes
  • Enabled reloadDocument on the ParaLink for the long-paragraph route
src/components/SideBar.jsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @its-nihit-0407 - I've reviewed your changes - here's some feedback:

  • Your useEffect for keydown events doesn’t remove the listener on unmount or declare proper dependencies—add a cleanup return and dependency array to avoid stale closures and memory leaks.
  • There are leftover commented console.logs and unused imports (useLocation/useNavigate) in TypingCompleteScreen—remove them for cleaner code.
  • The reset logic inside the props.quotes useEffect is quite verbose; consider extracting it into a standalone reset function or custom hook to improve readability and maintainability.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

const pressedKey = e.key;

// Don't process if typing is completed
if (isCompeleted) return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): Correct spelling of isCompeleted

Use the correct spelling (isCompleted) for clarity and consistency.

Suggested implementation:

            // Don't process if typing is completed
            if (isCompleted) return;

You will also need to rename all other occurrences of isCompeleted to isCompleted in this file (and possibly elsewhere in your codebase) to maintain consistency and avoid reference errors.


const TypingBoard = (props) => {
// console.log(props.quotes)
const [words, setWords] = useState([])
Copy link

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 RESET action. You’ll end up with one state object and one place that describes how it changes:

// 1) Move your “shape” into a single object
const makeInitialState = (rawQuote) => {
  const cleaned = rawQuote.trim().replace(/\s+/g, ' ')
  return {
    quote:        cleaned,
    currentIdx:   0,
    typedChars:   '',
    wordsPressed: 0,
    rightWords:   0,
    warning:      true,
    typos:        0,
    isCompleted:  false,
    finalTime:    0,
    WPM:          0,
    accuracy:     0,
  }
}

// 2) A reducer that handles RESET (and later could handle KEY_PRESS, etc)
function sessionReducer(state, action) {
  switch (action.type) {
    case 'RESET':
      return makeInitialState(action.quote)
    // you can add more actions here, e.g. KEY_PRESS
    default:
      return state
  }
}

const TypingBoard = ({ quotes }) => {
  // 3) swap all your useStates for one useReducer
  const [session, dispatch] = useReducer(
    sessionReducer,
    makeInitialState(quotes)
  )

  // 4) on props.quotes change just RESET in one line
  useEffect(() => {
    dispatch({ type: 'RESET', quote: quotes })
  }, [quotes])

  // 5) in your key handler you can dispatch KEY_PRESS instead of 10 setters
  useEffect(() => {
    const handleKeyDown = e => {
      if (session.isCompleted) return
      // …
      // dispatch({ type: 'KEY_PRESS', key: e.key })
    }

    document.addEventListener('keydown', handleKeyDown)
    return () => document.removeEventListener('keydown', handleKeyDown)
  }, [session, dispatch])

  // …render using session.currentIdx, session.typedChars, etc.
}

Benefits:

  • All reset logic lives in one place
  • No more “setX” duplication
  • Easier to add new actions (typo counting, completion, etc.) without scattering setters.

const pressedKey = e.key;

// Don't process if typing is completed
if (isCompeleted) return;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Use block braces for ifs, whiles, etc. (use-braces)

Suggested change
if (isCompeleted) return;
if (isCompeleted) {


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
situations, especially where subsequently a developer might add another statement
while forgetting to add the braces (meaning that this wouldn't be included in the condition).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant