fix: cursor not updating - use useState instead of useRef

The cursor position wasn't updating visually because:
- useRef doesn't trigger re-renders
- useCursorRender wasn't being called on position changes

Solution:
- Changed position from useRef to useState
- Now position changes trigger re-renders
- useCursorRender receives updated position
- Cursor highlight updates properly

Added temporary console.logs for debugging:
- Cursor position changes
- Key press events
- Movement attempts and bounds checking
- Cell selection and class application

This will help verify cursor is working in browser.
Console logs to be removed once verified working.
This commit is contained in:
2025-10-22 18:52:01 +01:00
parent be41ccee14
commit b635c21042
2 changed files with 43 additions and 13 deletions
+31 -12
View File
@@ -1,21 +1,26 @@
import { useRef, useEffect } from 'react'; import { useState, useEffect } from 'react';
import type { Coord, CursorManager } from './types'; import type { Coord, CursorManager } from './types';
import type { GameStatusManager } from '../useGameStatus/types'; import type { GameStatusManager } from '../useGameStatus/types';
export function useCursor(gameStatusManager: GameStatusManager) { export function useCursor(gameStatusManager: GameStatusManager) {
const position = useRef<Coord>({ row: 0, col: 0 }); const [position, setPosition] = useState<Coord>({ row: 0, col: 0 });
const { gameStatus } = gameStatusManager; const { gameStatus } = gameStatusManager;
function move(dRows: number, dCols: number, count: number = 1): void { function move(dRows: number, dCols: number, count: number = 1): void {
const currentPos = position.current; setPosition(currentPos => {
const newRow = currentPos.row + (dRows * count); const newRow = currentPos.row + (dRows * count);
const newCol = currentPos.col + (dCols * count); const newCol = currentPos.col + (dCols * count);
if (newRow < 0 || newRow > 8 || newCol < 0 || newCol > 8) { console.log('🚀 Move attempt:', { currentPos, dRows, dCols, count, newRow, newCol });
return;
}
position.current = { row: newRow, col: newCol }; if (newRow < 0 || newRow > 8 || newCol < 0 || newCol > 8) {
console.log('⛔ Out of bounds, movement blocked');
return currentPos;
}
console.log('✅ Position updated to:', { row: newRow, col: newCol });
return { row: newRow, col: newCol };
});
} }
function moveLeft(count = 1): void { function moveLeft(count = 1): void {
@@ -35,28 +40,42 @@ export function useCursor(gameStatusManager: GameStatusManager) {
} }
useEffect(() => { useEffect(() => {
if (gameStatus !== 'started') return; console.log('🎮 useCursor effect - gameStatus:', gameStatus);
if (gameStatus !== 'started') {
console.log('⏸️ Not started, skipping key listener');
return;
}
function handleKeyDown(e: KeyboardEvent) { function handleKeyDown(e: KeyboardEvent) {
const key = e.key; const key = e.key;
console.log('⌨️ Key pressed:', key);
if (key === 'h') { if (key === 'h') {
console.log('← Move left');
moveLeft(); moveLeft();
} else if (key === 'j') { } else if (key === 'j') {
console.log('↓ Move down');
moveDown(); moveDown();
} else if (key === 'k') { } else if (key === 'k') {
console.log('↑ Move up');
moveUp(); moveUp();
} else if (key === 'l') { } else if (key === 'l') {
console.log('→ Move right');
moveRight(); moveRight();
} }
} }
console.log('✅ Adding keydown listener');
window.addEventListener('keydown', handleKeyDown); window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown); return () => {
console.log('🗑️ Removing keydown listener');
window.removeEventListener('keydown', handleKeyDown);
};
}, [gameStatus]); }, [gameStatus]);
const cursor: CursorManager = { const cursor: CursorManager = {
position: () => position.current, position: () => position,
moveLeft, moveLeft,
moveRight, moveRight,
moveUp, moveUp,
+12 -1
View File
@@ -4,10 +4,16 @@ import type { BoardManager } from '../useBoard/types';
export function useCursorRender(containerRef: BoardManager['containerRef'], position: Coord) { export function useCursorRender(containerRef: BoardManager['containerRef'], position: Coord) {
useEffect(() => { useEffect(() => {
console.log('🎯 Cursor position:', position);
const container = containerRef.current; const container = containerRef.current;
if (!container) return; if (!container) {
console.log('❌ No container ref');
return;
}
const cells = container.querySelectorAll('.sudoku-cell'); const cells = container.querySelectorAll('.sudoku-cell');
console.log('📊 Found cells:', cells.length);
cells.forEach(cell => { cells.forEach(cell => {
cell.classList.remove('cursor'); cell.classList.remove('cursor');
@@ -15,8 +21,13 @@ export function useCursorRender(containerRef: BoardManager['containerRef'], posi
const index = position.row * 9 + position.col; const index = position.row * 9 + position.col;
const targetCell = cells[index]; const targetCell = cells[index];
console.log('🎯 Target cell index:', index, 'cell:', targetCell);
if (targetCell) { if (targetCell) {
targetCell.classList.add('cursor'); targetCell.classList.add('cursor');
console.log('✅ Added cursor class to cell');
} else {
console.log('❌ Target cell not found');
} }
}, [position, containerRef]); }, [position, containerRef]);
} }