Skip to content

refactor(engine): extract shared getCandidates and getBoxOrigin helpers#69

Open
adrienbrault wants to merge 10 commits into
mainfrom
claude/refactor-code-clarity-cYX29
Open

refactor(engine): extract shared getCandidates and getBoxOrigin helpers#69
adrienbrault wants to merge 10 commits into
mainfrom
claude/refactor-code-clarity-cYX29

Conversation

@adrienbrault

Copy link
Copy Markdown
Owner

getCandidates was duplicated identically in hint-engine.ts and
hint-hidden-single.ts. Moved to sudoku.ts as the canonical
implementation and imported in both files.

Added getBoxOrigin(coord) to replace the repeated
Math.floor(x / 3) * 3 pattern used across 3 files. This makes
the box-coordinate calculation explicit and consistent.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw

claude added 10 commits March 9, 2026 01:30
getCandidates was duplicated identically in hint-engine.ts and
hint-hidden-single.ts. Moved to sudoku.ts as the canonical
implementation and imported in both files.

Added getBoxOrigin(coord) to replace the repeated
Math.floor(x / 3) * 3 pattern used across 3 files. This makes
the box-coordinate calculation explicit and consistent.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
…RD_CELLS

Hardcoded 9, 3, and 81 appeared throughout the engine and storage
code without semantic meaning. Named constants make the grid
structure explicit and provide a single source of truth.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
The clipboard-write → "Copied!" feedback → auto-reset pattern
was duplicated in Lobby (twice), FriendsList, and GameResult.
Shared hook eliminates the repeated state + setTimeout boilerplate
and adds proper timer cleanup on rapid re-clicks.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
useAssistLevel, useNumPadPosition, and useOpponentProgressVisible
all repeated the same localStorage init + try/catch + setter
boilerplate. Generic useLocalStorage hook with optional type-guard
validation eliminates ~15 lines per consumer.

useDarkMode is intentionally not migrated — its side effects
(class toggling, media query listener) make a generic hook
less clear rather than more.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
The 7-level nested ternary for cell background class was hard to
read and reason about priority. Early-return function makes the
precedence order explicit and each condition independently
readable.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
… functions

handlePlaceNumber (102 lines) handled three distinct operations:
batch note toggle, single note toggle, and number placement.
Split into handleBatchNoteToggle, handleSingleNoteToggle, and
handleNumberPlacement — each with a single responsibility.

handleErase (59 lines) mixed batch and single-cell flows with
an inline type definition. Split into handleBatchErase and
handleSingleErase with a named ErasedCell type.

Also extracted fromCellKey helper to replace the repeated
Math.floor(key / 9) / key % 9 pattern, and replaced remaining
magic numbers with GRID_SIZE/BOX_SIZE constants.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
…tions

The function had two blocks of if/else-if/else branching on group
type (row/col/box) — once to collect empty cells, once to collect
related filled cells. Extracted getGroupPositions to return all
cell positions for any group type uniformly. This eliminates the
duplicated branching and reduces findHiddenSingleInGroup from
103 lines to a straightforward filter-and-search flow.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
Board.tsx had ~100 lines of pointer event handling mixed into the
rendering component: drag state refs, shift-click logic, click
suppression, and getCellFromPoint utility. Moved to a dedicated
hook that returns ready-to-spread event handlers, leaving Board
focused on rendering.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
Storage key strings were scattered across 12 files with no central
registry. Added STORAGE_KEYS object to constants.ts and updated
all consumers to reference it. This provides a single place to
audit, rename, or search for all persisted keys.

https://claude.ai/code/session_01CC1b6B5KoC6vieVCSae2Pw
@adrienbrault adrienbrault force-pushed the claude/refactor-code-clarity-cYX29 branch from 11b197e to 178374a Compare March 9, 2026 01:32
@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: 178374a
Status: ✅  Deploy successful!
Preview URL: https://34528932.sudoku-4cc.pages.dev
Branch Preview URL: https://claude-refactor-code-clarity.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