Skip to content

Commit

Permalink
fix puzzle auto jump
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Sep 15, 2024
1 parent 99decfb commit 5c28642
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
13 changes: 4 additions & 9 deletions src/components/puzzles/PuzzleBoard.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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";
Expand All @@ -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<boolean>({
key: "puzzle-jump-immediately",
defaultValue: true,
});
const [jumpToNextPuzzleImmediately] = useAtom(jumpToNextPuzzleAtom);

const currentNode = getNodeAtPath(root, position);

Expand Down Expand Up @@ -96,7 +91,7 @@ function PuzzleBoard({
}
setEnded(false);

if (db && newPos.isCheckmate() && jumpToNextPuzzleImmediately) {
if (db && jumpToNextPuzzleImmediately) {
generatePuzzle(db);
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/components/puzzles/Puzzles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
activeTabAtom,
currentPuzzleAtom,
hidePuzzleRatingAtom,
jumpToNextPuzzleAtom,
progressivePuzzlesAtom,
puzzleRatingRangeAtom,
selectedPuzzleDbAtom,
tabsAtom,
} from "@/state/atoms";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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<boolean>({
key: "puzzle-jump-immediately",
defaultValue: true,
});
useAtom(jumpToNextPuzzleAtom);

const wonPuzzles = puzzles.filter(
(puzzle) => puzzle.completion === "correct",
Expand Down
8 changes: 8 additions & 0 deletions src/state/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ export const progressivePuzzlesAtom = atomWithStorage<boolean>(
"progressive-puzzles",
false,
);
export const jumpToNextPuzzleAtom = atomWithStorage<boolean>(
"puzzle-jump-immediately",
true,
);
export const puzzleRatingRangeAtom = atomWithStorage<[number, number]>(
"puzzle-ratings",
[1000, 1500],
);

// CP / WDL

Expand Down

0 comments on commit 5c28642

Please sign in to comment.