import { useReducer, type ActionDispatch } from "react"; import { playingGrid, reduce, type Action, type Initial, type ReductionError, } from "sudoku-rust"; const isReductionError = (val: unknown): val is ReductionError => val === "LockedCell" || (typeof val === "object" && val !== null && "ValidationError" in val && typeof val.ValidationError === "object" && val.ValidationError !== null && "row" in val.ValidationError && "column" in val.ValidationError && "box" in val.ValidationError && typeof val.ValidationError.row === "boolean" && typeof val.ValidationError.column == "boolean" && typeof val.ValidationError.box === "boolean"); export default function useGrid(): [ [Initial, ReductionError | null], ActionDispatch<[Action]>, ] { return useReducer( ([{ playing, solution }], action) => { let reduction = reduce(playing, solution, action); if ("Invalid" in reduction) return [{ playing, solution }, reduction.Invalid]; return [ { solution, playing: reduction.Valid, }, null, ]; }, [], // oxlint-disable-next-line (_) => [playingGrid.makeInitial("Medium"), null], ); }