diff --git a/src/components/puzzles/PuzzleBoard.tsx b/src/components/puzzles/PuzzleBoard.tsx index 380f87a3..0852a696 100644 --- a/src/components/puzzles/PuzzleBoard.tsx +++ b/src/components/puzzles/PuzzleBoard.tsx @@ -1,12 +1,11 @@ import { Chessground } from "@/chessground/Chessground"; -import { showCoordinatesAtom } from "@/state/atoms"; +import { jumpToNextPuzzleAtom, showCoordinatesAtom } from "@/state/atoms"; import { chessboard } from "@/styles/Chessboard.css"; import { positionFromFen } from "@/utils/chessops"; 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, @@ -17,7 +16,7 @@ import { import { chessgroundDests, chessgroundMove } from "chessops/compat"; import { parseFen } from "chessops/fen"; import equal from "fast-deep-equal"; -import { useAtomValue } from "jotai"; +import { useAtom, useAtomValue } from "jotai"; import { useContext, useState } from "react"; import { useStore } from "zustand"; import PromotionModal from "../boards/PromotionModal"; @@ -42,11 +41,7 @@ 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 [jumpToNextPuzzleImmediately] = useAtom(jumpToNextPuzzleAtom); const currentNode = getNodeAtPath(root, position); @@ -96,7 +91,7 @@ function PuzzleBoard({ } setEnded(false); - if (db && newPos.isCheckmate() && jumpToNextPuzzleImmediately) { + if (db && jumpToNextPuzzleImmediately) { generatePuzzle(db); } } diff --git a/src/components/puzzles/Puzzles.tsx b/src/components/puzzles/Puzzles.tsx index 2c02c93f..5e5bd075 100644 --- a/src/components/puzzles/Puzzles.tsx +++ b/src/components/puzzles/Puzzles.tsx @@ -3,7 +3,9 @@ import { activeTabAtom, currentPuzzleAtom, hidePuzzleRatingAtom, + jumpToNextPuzzleAtom, progressivePuzzlesAtom, + puzzleRatingRangeAtom, selectedPuzzleDbAtom, tabsAtom, } from "@/state/atoms"; @@ -35,7 +37,7 @@ import { Text, Tooltip, } from "@mantine/core"; -import { useLocalStorage, useSessionStorage } from "@mantine/hooks"; +import { useSessionStorage } from "@mantine/hooks"; import { IconPlus, IconX, IconZoomCheck } from "@tabler/icons-react"; import { Chess, parseUci } from "chessops"; import { parseFen } from "chessops/fen"; @@ -72,16 +74,10 @@ function Puzzles({ id }: { id: string }) { }); }, []); - const [ratingRange, setRatingRange] = useLocalStorage<[number, number]>({ - key: "puzzle-ratings", - defaultValue: [1000, 1500], - }); + const [ratingRange, setRatingRange] = useAtom(puzzleRatingRangeAtom); const [jumpToNextPuzzleImmediately, setJumpToNextPuzzleImmediately] = - useLocalStorage({ - key: "puzzle-jump-immediately", - defaultValue: true, - }); + useAtom(jumpToNextPuzzleAtom); const wonPuzzles = puzzles.filter( (puzzle) => puzzle.completion === "correct", diff --git a/src/state/atoms.ts b/src/state/atoms.ts index 8b66e2d2..0beb7187 100644 --- a/src/state/atoms.ts +++ b/src/state/atoms.ts @@ -250,6 +250,14 @@ export const progressivePuzzlesAtom = atomWithStorage( "progressive-puzzles", false, ); +export const jumpToNextPuzzleAtom = atomWithStorage( + "puzzle-jump-immediately", + true, +); +export const puzzleRatingRangeAtom = atomWithStorage<[number, number]>( + "puzzle-ratings", + [1000, 1500], +); // CP / WDL