fix: remove invalid hook call in anchor movement validation

Remove validateAnchorMove function that called useMazeNavigation hook inside a regular function, which violates React's rules of hooks. Use the existing navigator instance directly in anchor movement methods instead.
This commit is contained in:
2025-10-23 20:30:07 +01:00
parent 1b9d963f46
commit f79d9d1c65
+40 -23
View File
@@ -7,25 +7,6 @@ import { useGameKeys } from './useGameKeys';
import { useMazeNavigation } from '../useBoard';
import { useVimMotions } from './useVimMotions';
function validateAnchorMove(direction: 'start' | 'end' | 'top' | 'bottom', currentPos: Coord, board: BoardManager): boolean {
const navDirection = direction === 'start' ? 'left' :
direction === 'end' ? 'right' :
direction === 'top' ? 'top' : 'bottom';
const navigator = useMazeNavigation(board.mazeRef.current);
const target = navigator.findAnchorTarget(currentPos, navDirection);
if (!target) return false;
const dr = target.row > currentPos.row ? 1 :
target.row < currentPos.row ? -1 : 0;
const dc = target.col > currentPos.col ? 1 :
target.col < currentPos.col ? -1 : 0;
const steps = Math.abs(target.row - currentPos.row) + Math.abs(target.col - currentPos.col);
return navigator.validatePath(currentPos, { row: dr, col: dc }, steps);
}
export function useCursor(board: BoardManager, gameStatusManager: GameStatusManager) {
const position = useRef<Coord>({ row: 0, col: 0 })
const mode = useRef<CursorMode>('normal')
@@ -105,7 +86,16 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
}
function moveToStart(): void {
if (!validateAnchorMove('start', position.current, board)) {
const currentPos = position.current;
const target = navigator.findAnchorTarget(currentPos, 'left');
if (!target) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
const dr = target.row > currentPos.row ? 1 : target.row < currentPos.row ? -1 : 0;
const dc = target.col > currentPos.col ? 1 : target.col < currentPos.col ? -1 : 0;
const steps = Math.abs(target.row - currentPos.row) + Math.abs(target.col - currentPos.col);
if (!navigator.validatePath(currentPos, { row: dr, col: dc }, steps)) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
@@ -113,7 +103,16 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
}
function moveToEnd(): void {
if (!validateAnchorMove('end', position.current, board)) {
const currentPos = position.current;
const target = navigator.findAnchorTarget(currentPos, 'right');
if (!target) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
const dr = target.row > currentPos.row ? 1 : target.row < currentPos.row ? -1 : 0;
const dc = target.col > currentPos.col ? 1 : target.col < currentPos.col ? -1 : 0;
const steps = Math.abs(target.row - currentPos.row) + Math.abs(target.col - currentPos.col);
if (!navigator.validatePath(currentPos, { row: dr, col: dc }, steps)) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
@@ -121,7 +120,16 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
}
function moveToTop(): void {
if (!validateAnchorMove('top', position.current, board)) {
const currentPos = position.current;
const target = navigator.findAnchorTarget(currentPos, 'top');
if (!target) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
const dr = target.row > currentPos.row ? 1 : target.row < currentPos.row ? -1 : 0;
const dc = target.col > currentPos.col ? 1 : target.col < currentPos.col ? -1 : 0;
const steps = Math.abs(target.row - currentPos.row) + Math.abs(target.col - currentPos.col);
if (!navigator.validatePath(currentPos, { row: dr, col: dc }, steps)) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
@@ -129,7 +137,16 @@ export function useCursor(board: BoardManager, gameStatusManager: GameStatusMana
}
function moveToBottom(): void {
if (!validateAnchorMove('bottom', position.current, board)) {
const currentPos = position.current;
const target = navigator.findAnchorTarget(currentPos, 'bottom');
if (!target) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}
const dr = target.row > currentPos.row ? 1 : target.row < currentPos.row ? -1 : 0;
const dc = target.col > currentPos.col ? 1 : target.col < currentPos.col ? -1 : 0;
const steps = Math.abs(target.row - currentPos.row) + Math.abs(target.col - currentPos.col);
if (!navigator.validatePath(currentPos, { row: dr, col: dc }, steps)) {
window.dispatchEvent(new Event("maze-invalid"));
return;
}