diff --git a/src/components/puzzles/PuzzleBoard.tsx b/src/components/puzzles/PuzzleBoard.tsx index fb50c125..b8cb1e8b 100644 --- a/src/components/puzzles/PuzzleBoard.tsx +++ b/src/components/puzzles/PuzzleBoard.tsx @@ -6,6 +6,7 @@ import type { Completion, Puzzle } from "@/utils/puzzles"; import { getNodeAtPath, treeIteratorMainLine } from "@/utils/treeReducer"; import { Box } from "@mantine/core"; import { useElementSize, useForceUpdate } from "@mantine/hooks"; +import { useLocalStorage, useSessionStorage } from "@mantine/hooks"; import { Chess, type Move, @@ -41,6 +42,11 @@ function PuzzleBoard({ const makeMove = useStore(store, (s) => s.makeMove); const makeMoves = useStore(store, (s) => s.makeMoves); const reset = useForceUpdate(); + const [jumpToNextPuzzleImmediately, setJumpToNextPuzzleImmediately] = + useLocalStorage({ + key: "puzzle-jump-immediately", + defaultValue: true, + }); const currentNode = getNodeAtPath(root, position); @@ -77,17 +83,20 @@ function PuzzleBoard({ function checkMove(move: Move) { if (!pos) return; + if (!puzzle) return; + const newPos = pos.clone(); const uci = makeUci(move); newPos.play(move); - if (puzzle && (puzzle.moves[currentMove] === uci || newPos.isCheckmate())) { + + if (puzzle.moves[currentMove] === uci) { if (currentMove === puzzle.moves.length - 1) { if (puzzle.completion !== "incorrect") { changeCompletion("correct"); } setEnded(false); - if (db) { + if (db && newPos.isCheckmate() && jumpToNextPuzzleImmediately) { generatePuzzle(db); } } diff --git a/src/components/puzzles/Puzzles.tsx b/src/components/puzzles/Puzzles.tsx index 038dc21d..6b59668e 100644 --- a/src/components/puzzles/Puzzles.tsx +++ b/src/components/puzzles/Puzzles.tsx @@ -31,6 +31,7 @@ import { ScrollArea, Select, Stack, + Switch, Text, Tooltip, } from "@mantine/core"; @@ -40,6 +41,7 @@ import { Chess, parseUci } from "chessops"; import { parseFen } from "chessops/fen"; import { useAtom, useSetAtom } from "jotai"; import { useContext, useEffect, useState } from "react"; +import { useTranslation } from "react-i18next"; import { useStore } from "zustand"; import GameNotation from "../boards/GameNotation"; import ChallengeHistory from "../common/ChallengeHistory"; @@ -49,6 +51,7 @@ import AddPuzzle from "./AddPuzzle"; import PuzzleBoard from "./PuzzleBoard"; function Puzzles({ id }: { id: string }) { + const { t } = useTranslation(); const store = useContext(TreeStateContext)!; const setFen = useStore(store, (s) => s.setFen); const goToStart = useStore(store, (s) => s.goToStart); @@ -74,6 +77,12 @@ function Puzzles({ id }: { id: string }) { defaultValue: [1000, 1500], }); + const [jumpToNextPuzzleImmediately, setJumpToNextPuzzleImmediately] = + useLocalStorage({ + key: "puzzle-jump-immediately", + defaultValue: true, + }); + const wonPuzzles = puzzles.filter( (puzzle) => puzzle.completion === "correct", ); @@ -249,6 +258,15 @@ function Puzzles({ id }: { id: string }) { )} + + setJumpToNextPuzzleImmediately(event.currentTarget.checked) + } + checked={jumpToNextPuzzleImmediately} + label={t("Puzzle.JumpToNextPuzzleImmediately")} + /> +