Add multi-game resume list with board previews
This commit is contained in:
@@ -42,6 +42,12 @@ export interface Translations {
|
||||
playAsBlack: string;
|
||||
randomColor: string;
|
||||
analyzeGame: string;
|
||||
savedGamesTitle: string;
|
||||
savedGamesEmpty: string;
|
||||
opponentLabel: string;
|
||||
evaluationLabel: string;
|
||||
noEvaluation: string;
|
||||
deleteGame: string;
|
||||
};
|
||||
|
||||
// Game
|
||||
@@ -157,6 +163,12 @@ const en: Translations = {
|
||||
playAsBlack: 'Play as Black',
|
||||
randomColor: 'Random',
|
||||
analyzeGame: 'Analyze a Game',
|
||||
savedGamesTitle: 'Unfinished games',
|
||||
savedGamesEmpty: 'No unfinished games yet.',
|
||||
opponentLabel: 'Opponent',
|
||||
evaluationLabel: 'Evaluation',
|
||||
noEvaluation: 'No evaluation yet',
|
||||
deleteGame: 'Delete game',
|
||||
},
|
||||
game: {
|
||||
playingAs: 'Playing as',
|
||||
@@ -262,6 +274,12 @@ const de: Translations = {
|
||||
playAsBlack: 'Als Schwarz spielen',
|
||||
randomColor: 'Zufällig',
|
||||
analyzeGame: 'Partie analysieren',
|
||||
savedGamesTitle: 'Unfertige Partien',
|
||||
savedGamesEmpty: 'Keine unfertigen Partien vorhanden.',
|
||||
opponentLabel: 'Gegner',
|
||||
evaluationLabel: 'Bewertung',
|
||||
noEvaluation: 'Keine Bewertung',
|
||||
deleteGame: 'Partie löschen',
|
||||
},
|
||||
game: {
|
||||
playingAs: 'Spielst als',
|
||||
@@ -367,6 +385,12 @@ const fr: Translations = {
|
||||
playAsBlack: 'Jouer Noirs',
|
||||
randomColor: 'Aléatoire',
|
||||
analyzeGame: 'Analyser une partie',
|
||||
savedGamesTitle: 'Parties inachevées',
|
||||
savedGamesEmpty: 'Aucune partie en cours.',
|
||||
opponentLabel: 'Adversaire',
|
||||
evaluationLabel: 'Évaluation',
|
||||
noEvaluation: 'Pas d\'évaluation',
|
||||
deleteGame: 'Supprimer la partie',
|
||||
},
|
||||
game: {
|
||||
playingAs: 'Jouant',
|
||||
@@ -472,6 +496,12 @@ const it: Translations = {
|
||||
playAsBlack: 'Gioca Nero',
|
||||
randomColor: 'Casuale',
|
||||
analyzeGame: 'Analizza una partita',
|
||||
savedGamesTitle: 'Partite non finite',
|
||||
savedGamesEmpty: 'Nessuna partita in corso.',
|
||||
opponentLabel: 'Avversario',
|
||||
evaluationLabel: 'Valutazione',
|
||||
noEvaluation: 'Nessuna valutazione',
|
||||
deleteGame: 'Elimina partita',
|
||||
},
|
||||
game: {
|
||||
playingAs: 'Giocando',
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
import { Personality } from "./personalities";
|
||||
import { StockfishEvaluation } from "./stockfish";
|
||||
import { SupportedLanguage } from "./i18n/translations";
|
||||
|
||||
export type SavedGame = {
|
||||
id: string;
|
||||
fen: string;
|
||||
pgn?: string;
|
||||
selectedPersonality: Personality;
|
||||
playerColor: "white" | "black";
|
||||
updatedAt: number;
|
||||
evaluation?: Pick<StockfishEvaluation, "score" | "mate" | "depth"> | null;
|
||||
language?: SupportedLanguage;
|
||||
apiKey?: string | null;
|
||||
};
|
||||
|
||||
const STORAGE_KEY = "chess_tutor_saves";
|
||||
const LEGACY_KEY = "chess_tutor_save";
|
||||
|
||||
const parseSavedGames = (): SavedGame[] => {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (!raw) return [];
|
||||
|
||||
try {
|
||||
const data = JSON.parse(raw);
|
||||
if (!Array.isArray(data)) return [];
|
||||
return data.filter(Boolean);
|
||||
} catch (e) {
|
||||
console.error("Failed to parse saved games", e);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const persistSavedGames = (games: SavedGame[]) => {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(games));
|
||||
};
|
||||
|
||||
const loadLegacySave = (): SavedGame[] => {
|
||||
const legacy = localStorage.getItem(LEGACY_KEY);
|
||||
if (!legacy) return [];
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(legacy);
|
||||
if (parsed && parsed.fen && parsed.selectedPersonality) {
|
||||
const legacyGame: SavedGame = {
|
||||
id: parsed.id || `legacy-${Date.now()}`,
|
||||
fen: parsed.fen,
|
||||
pgn: parsed.pgn,
|
||||
selectedPersonality: parsed.selectedPersonality,
|
||||
playerColor: parsed.playerColor || "white",
|
||||
updatedAt: parsed.updatedAt || Date.now(),
|
||||
evaluation: parsed.evaluation || null,
|
||||
};
|
||||
return [legacyGame];
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to migrate legacy save", e);
|
||||
}
|
||||
|
||||
return [];
|
||||
};
|
||||
|
||||
export const loadSavedGames = (): SavedGame[] => {
|
||||
const existing = parseSavedGames();
|
||||
if (existing.length > 0) {
|
||||
return existing.sort((a, b) => b.updatedAt - a.updatedAt);
|
||||
}
|
||||
|
||||
const legacy = loadLegacySave();
|
||||
if (legacy.length > 0) {
|
||||
persistSavedGames(legacy);
|
||||
localStorage.removeItem(LEGACY_KEY);
|
||||
return legacy.sort((a, b) => b.updatedAt - a.updatedAt);
|
||||
}
|
||||
|
||||
return [];
|
||||
};
|
||||
|
||||
export const upsertSavedGame = (game: SavedGame) => {
|
||||
const games = parseSavedGames();
|
||||
const index = games.findIndex(g => g.id === game.id);
|
||||
const updatedGames = index >= 0
|
||||
? games.map(g => (g.id === game.id ? game : g))
|
||||
: [...games, game];
|
||||
|
||||
persistSavedGames(updatedGames);
|
||||
};
|
||||
|
||||
export const deleteSavedGame = (id: string) => {
|
||||
const games = parseSavedGames().filter(g => g.id !== id);
|
||||
persistSavedGames(games);
|
||||
};
|
||||
Reference in New Issue
Block a user