refactor(engine): extract boxOrigin and getCandidates into sudoku.ts#70
Open
adrienbrault wants to merge 7 commits into
Open
refactor(engine): extract boxOrigin and getCandidates into sudoku.ts#70adrienbrault wants to merge 7 commits into
adrienbrault wants to merge 7 commits into
Conversation
getCandidates was duplicated verbatim in hint-engine.ts and hint-hidden-single.ts. The box origin formula Math.floor(row/3)*3 appeared 10+ times across four files with no shared abstraction. Adds boxOrigin(row, col) and getCandidates(board, row, col) as exported functions in sudoku.ts. Both hint files and sudokuActions.ts now import these instead of inlining the same math. Adds unit tests for both new exports.
The 7-level nested ternary for background class selection was hard to scan and fragile to extend. A named function with early-return if-statements makes the priority of conditions explicit and readable.
handlePlaceNumber handled three distinct behaviors interleaved in one function: batch note toggle across multiple selected cells, single note toggle, and value placement. The isGiven guard sat between the batch and single-note blocks, making the dispatch logic implicit. Extracts handleBatchToggleNote, handleSingleToggleNote, and handlePlaceValue as private functions. handlePlaceNumber becomes a pure dispatcher that makes the control flow explicit. No behavior change — the isGiven guard is hoisted to the dispatcher, preserving its original scope (batch path filters isGiven per-cell internally).
Board.tsx contained ~100 lines of pointer event handling for drag multi-selection: dragRef, suppressClickRef, getCellFromPoint, and three pointer event callbacks. This logic was unrelated to the board's rendering concerns. Moves all drag-select state and handlers into useDragSelection, which accepts onSetSelectedCells, selectedCells, and selectedCell and returns the four event handlers. Board.tsx reduces to a single hook call. Tests cover shift+click merging, multi-cell drag accumulation, and no-op cases when no element is under the pointer.
Pause state and the visibilitychange auto-pause effect were scattered across SoloGame's body with no clear grouping. Extracting them into useGameTimer(status) makes the concern explicit: one hook owns when and why the game timer pauses. Also names the two magic timeout values (600ms reveal, 300ms modal) as constants to document their intent.
…eResult GameResult had 13 props with an isMultiplayer flag driving mutually exclusive rendering paths. Solo-only props (streakInfo, stats, isNewPB, tip, hintsUsed, isDaily, onDismissTip) and multiplayer-only props (onAddFriend, opponentId, opponentName) were mixed into one type, making call sites harder to read. Adds SoloGameResult and MultiplayerGameResult as focused exports that delegate to GameResult with the correct isMultiplayer flag. Both callers now use the typed component for their context. The original GameResult remains exported for backwards compatibility with tests.
The 106-line effect mixed connection setup, state derivation, observer registration, awareness tracking, and status listening in a flat block without visible structure. The if (state) early return was also buried inside updateState. Adds explicit section separators (Connection setup, Room state derivation, Room map observers, Awareness/presence tracking, Connection status) so each concern is immediately locatable. Flattens the nested if (state) in updateState to an early-return guard. No behavior change.
dd55c71 to
35483d3
Compare
Deploying sudoku with
|
| Latest commit: |
35483d3
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://ccfe38e5.sudoku-4cc.pages.dev |
| Branch Preview URL: | https://claude-refactor-code-quality.sudoku-4cc.pages.dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
getCandidates was duplicated verbatim in hint-engine.ts and
hint-hidden-single.ts. The box origin formula Math.floor(row/3)*3
appeared 10+ times across four files with no shared abstraction.
Adds boxOrigin(row, col) and getCandidates(board, row, col) as
exported functions in sudoku.ts. Both hint files and sudokuActions.ts
now import these instead of inlining the same math. Adds unit tests
for both new exports.