Skip to content

refactor(engine): extract boxOrigin and getCandidates into sudoku.ts#70

Open
adrienbrault wants to merge 7 commits into
mainfrom
claude/refactor-code-quality-rZElH
Open

refactor(engine): extract boxOrigin and getCandidates into sudoku.ts#70
adrienbrault wants to merge 7 commits into
mainfrom
claude/refactor-code-quality-rZElH

Conversation

@adrienbrault

Copy link
Copy Markdown
Owner

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.

claude added 7 commits March 9, 2026 01:28
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.
@adrienbrault adrienbrault force-pushed the claude/refactor-code-quality-rZElH branch from dd55c71 to 35483d3 Compare March 9, 2026 06:59
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Mar 9, 2026

Copy link
Copy Markdown

Deploying sudoku with  Cloudflare Pages  Cloudflare Pages

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

View logs

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.

2 participants