From 71beedae8949c7841dd29942587328fb732bac1d Mon Sep 17 00:00:00 2001 From: chrislicodes Date: Tue, 23 May 2023 22:29:17 +0200 Subject: [PATCH] chore: refactor sync actions WIP --- src/components/react/ChessStudy.tsx | 46 +++++++---------------------- src/lib/ui-state/index.ts | 17 +++++++++++ storage/Pgx29Fy-EHIrzWKkp8fai.json | 46 +++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/components/react/ChessStudy.tsx b/src/components/react/ChessStudy.tsx index 78cfb30..6afc78d 100644 --- a/src/components/react/ChessStudy.tsx +++ b/src/components/react/ChessStudy.tsx @@ -14,7 +14,11 @@ import { ChessStudyMove, VariantMove, } from 'src/lib/storage'; -import { displayMoveInHistory, findMoveIndex } from 'src/lib/ui-state'; +import { + displayMoveInHistory, + findMoveIndex, + getCurrentMove, +} from 'src/lib/ui-state'; import { useImmerReducer } from 'use-immer'; import { ChessgroundProps, ChessgroundWrapper } from './ChessgroundWrapper'; import { CommentSection } from './CommentSection'; @@ -111,46 +115,18 @@ export const ChessStudy = ({ return draft; } case 'SYNC_SHAPES': { - const currentMoveId = draft.currentMove?.moveId; - const moves = draft.study.moves; + const move = getCurrentMove(draft); - const { variant, moveIndex } = findMoveIndex(moves, currentMoveId); - - if (variant) { - const move = - moves[variant.parentMoveIndex].variants[variant.variantIndex] - .moves[moveIndex]; - move.shapes = action.shapes; - - draft.currentMove = move; - } else { - const move = moves[moveIndex]; - move.shapes = action.shapes; - - draft.currentMove = move; - } + move.shapes = action.shapes; + draft.currentMove = move; return draft; } case 'SYNC_COMMENT': { - const currentMoveId = draft.currentMove?.moveId; - const moves = draft.study.moves; + const move = getCurrentMove(draft); - const { variant, moveIndex } = findMoveIndex(moves, currentMoveId); - - if (variant) { - const move = - moves[variant.parentMoveIndex].variants[variant.variantIndex] - .moves[moveIndex]; - move.comment = action.comment; - - draft.currentMove = move; - } else { - const move = moves[moveIndex]; - move.comment = action.comment; - - draft.currentMove = move; - } + move.comment = action.comment; + draft.currentMove = move; return draft; } diff --git a/src/lib/ui-state/index.ts b/src/lib/ui-state/index.ts index 3ff215e..db2ddc4 100644 --- a/src/lib/ui-state/index.ts +++ b/src/lib/ui-state/index.ts @@ -93,3 +93,20 @@ export const displayMoveInHistory = ( return draft; }; + +export const getCurrentMove = ( + draft: Draft +): Draft | Draft => { + const currentMoveId = draft.currentMove?.moveId; + const moves = draft.study.moves; + + const { variant, moveIndex } = findMoveIndex(moves, currentMoveId); + + if (variant) { + return moves[variant.parentMoveIndex].variants[variant.variantIndex].moves[ + moveIndex + ]; + } else { + return moves[moveIndex]; + } +}; diff --git a/storage/Pgx29Fy-EHIrzWKkp8fai.json b/storage/Pgx29Fy-EHIrzWKkp8fai.json index 0854294..5bbcc06 100644 --- a/storage/Pgx29Fy-EHIrzWKkp8fai.json +++ b/storage/Pgx29Fy-EHIrzWKkp8fai.json @@ -582,8 +582,50 @@ "after": "r1bqkbnr/ppp1pppp/2n5/3p4/3P4/5N2/PPP1PPPP/RNBQKB1R w KQkq - 2 3", "moveId": "EKWVgjt5Z2Cz8bqD5Ie0k", "variants": [], - "shapes": [], - "comment": null + "shapes": [ + { + "orig": "f3", + "dest": "e5", + "brush": "green" + }, + { + "orig": "c3", + "dest": "d5", + "brush": "green" + }, + { + "orig": "b3", + "dest": "b5", + "brush": "green" + }, + { + "orig": "c6", + "dest": "e6", + "brush": "green" + }, + { + "orig": "g6", + "dest": "g4", + "brush": "green" + } + ], + "comment": { + "type": "doc", + "content": [ + { + "type": "heading", + "attrs": { + "level": 1 + }, + "content": [ + { + "type": "text", + "text": "This is a test" + } + ] + } + ] + } } ] }