1+ import { italianGameVariations } from "../data/openings to learn/italian" ;
12import { Box , Typography , Button , Stack } from "@mui/material" ;
2- import { useState , useMemo , useEffect , useCallback } from "react" ;
3+ import { useState , useMemo , useEffect } from "react" ;
34import Board from "../components/board" ;
45import { Chess } from "chess.js" ;
56import { atom , useAtom } from "jotai" ;
67import { useChessActions } from "../hooks/useChessActions" ;
78import { Color } from "../types/enums" ;
89import { CurrentPosition } from "../types/eval" ;
9- import { Variation , italianGameVariations } from "../data/openings to learn/italian" ;
10+ import type { Variation } from "../data/openings to learn/italian" ;
1011import OpeningProgress from "../components/OpeningProgress" ;
1112
12- // Extension du type Variation pour inclure les propriétés optionnelles
13- interface ExtendedVariation extends Variation {
14- fen ?: string ;
15- color ?: Color ;
16- }
17-
1813// Détermine la couleur d'apprentissage pour la variante (par défaut blanc, mais extensible)
19- function getLearningColor ( variation : ExtendedVariation ) : Color {
20- return variation . color || Color . White ;
14+ function getLearningColor ( variation : Variation ) : Color {
15+ // TODO: utiliser variation.color si défini, sinon blanc
16+ return Color . White ;
2117}
2218
2319export default function OpeningPage ( ) {
@@ -121,11 +117,7 @@ export default function OpeningPage() {
121117 for ( let i = 0 ; i < moveIdx ; i ++ ) expectedMove . move ( selectedVariation . moves [ i ] ) ;
122118 const expected = expectedMove . move ( selectedVariation . moves [ moveIdx ] ) ;
123119 if ( ! expected || last . from !== expected . from || last . to !== expected . to ) {
124- // Mauvais coup : afficher l'icône de blunder
125- setShowBlunder ( { show : true , square : last . to } ) ;
126- setTimeout ( ( ) => setShowBlunder ( { show : false , square : null } ) , 1500 ) ;
127-
128- // Annuler et annoter
120+ // Mauvais coup : annuler et annoter
129121 let mistakeType = "Mistake" ;
130122 if ( last . captured || last . san . includes ( "#" ) ) mistakeType = "Blunder" ;
131123 setLastMistake ( { from : last . from , to : last . to , type : mistakeType } ) ;
@@ -183,9 +175,6 @@ export default function OpeningPage() {
183175 const openingKey = "italian" ;
184176 const progressMode = trainingMode ? "training" : "learning" ;
185177 const progressStorageKey = `${ openingKey } -progress-${ progressMode } ` ;
186- // État pour gérer l'affichage de l'icône de blunder
187- const [ showBlunder , setShowBlunder ] = useState < { show : boolean , square : string | null } > ( { show : false , square : null } ) ;
188-
189178 const [ completedVariations , setCompletedVariations ] = useState < number [ ] > ( ( ) => {
190179 if ( typeof window === "undefined" ) return [ ] ;
191180 try {
@@ -209,59 +198,11 @@ export default function OpeningPage() {
209198 // eslint-disable-next-line react-hooks/exhaustive-deps
210199 } , [ moveIdx , selectedVariation , currentVariantIdx , progressMode ] ) ;
211200
212- // Réinitialisation de la progression et de l'état du jeu
213- const handleResetProgress = useCallback ( ( ) => {
214- try {
215- // 1. Réinitialiser la progression dans le stockage local
216- localStorage . removeItem ( progressStorageKey ) ;
217- localStorage . setItem ( progressStorageKey , JSON . stringify ( [ ] ) ) ;
218-
219- // 2. Réinitialiser les états locaux
220- setCompletedVariations ( [ ] ) ;
221- setTrainingMode ( false ) ;
222- setLastMistake ( null ) ;
223-
224- // 3. Mettre à jour l'index de la variante et le coup actuel
225- // D'abord, réinitialiser l'index de la variante pour forcer un rechargement
226- setCurrentVariantIdx ( - 1 ) ;
227-
228- // 4. Réinitialiser l'index du coup actuel
229- setMoveIdx ( 0 ) ;
230-
231- // 5. Créer une nouvelle instance de jeu
232- const newGame = new Chess ( ) ;
233-
234- // 6. Mettre à jour l'état du jeu
235- setGame ( newGame ) ;
236-
237- // 7. Forcer un rafraîchissement de l'interface utilisateur
238- // Utiliser un délai pour s'assurer que les états précédents sont bien mis à jour
239- setTimeout ( ( ) => {
240- try {
241- // Forcer un nouveau rendu
242- window . dispatchEvent ( new Event ( 'resize' ) ) ;
243-
244- // Mettre à jour l'index de la variante pour déclencher la réinitialisation du jeu
245- // Cela va déclencher l'effet qui met à jour le jeu avec la première variante
246- setCurrentVariantIdx ( 0 ) ;
247-
248- // S'assurer que le jeu est bien réinitialisé avec la première variante
249- if ( variations . length > 0 ) {
250- const initialGame = new Chess ( ) ;
251- // Appliquer les coups de la première variante si nécessaire
252- // (le premier coup sera appliqué automatiquement par l'effet)
253- setGame ( initialGame ) ;
254- }
255-
256- } catch ( error ) {
257- console . error ( "Erreur lors de la mise à jour de l'interface utilisateur:" , error ) ;
258- }
259- } , 50 ) ;
260-
261- } catch ( error ) {
262- console . error ( "Erreur lors de la réinitialisation de la progression:" , error ) ;
263- }
264- } , [ progressStorageKey , setGame , variations . length ] ) ;
201+ // Réinitialisation de la progression
202+ const handleResetProgress = ( ) => {
203+ localStorage . removeItem ( progressStorageKey ) ;
204+ setCompletedVariations ( [ ] ) ;
205+ } ;
265206
266207 // Détermination de la case cible du dernier coup joué (pour overlay)
267208 const lastMoveSquare = useMemo ( ( ) => {
@@ -329,24 +270,7 @@ export default function OpeningPage() {
329270 </ Box >
330271 { /* Zone de droite : échiquier responsive */ }
331272 < Box sx = { { flex : 2 , display : 'flex' , alignItems : 'center' , justifyContent : 'center' , minWidth : 0 } } >
332- { allDone ? (
333- < Box sx = { { textAlign : 'center' , p : 3 } } >
334- < Typography variant = "h5" color = "success.main" gutterBottom >
335- Félicitations !
336- </ Typography >
337- < Typography variant = "body1" sx = { { mb : 2 } } >
338- Vous avez terminé toutes les variantes de cette ouverture.
339- </ Typography >
340- < Button
341- variant = "contained"
342- color = "primary"
343- onClick = { handleResetProgress }
344- sx = { { mt : 2 } }
345- >
346- Recommencer depuis le début
347- </ Button >
348- </ Box >
349- ) : selectedVariation && game ? (
273+ { selectedVariation && ! allDone && game && (
350274 < Box sx = { { width : '100%' , maxWidth : 600 , aspectRatio : '1' , minWidth : { xs : 260 , sm : 340 , md : 400 } , mx : 'auto' , position : 'relative' } } >
351275 < Board
352276 id = "LearningBoard"
@@ -362,7 +286,7 @@ export default function OpeningPage() {
362286 trainingFeedback = { trainingFeedback && lastMoveSquare ? { square : lastMoveSquare , ...trainingFeedback } : undefined }
363287 />
364288 </ Box >
365- ) : null }
289+ ) }
366290 </ Box >
367291 </ Stack >
368292 </ Box >
0 commit comments