From faf10b93b10e388ff858e14925994e198f5efa3b Mon Sep 17 00:00:00 2001 From: F88 <685250+F88@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:08:13 +0900 Subject: [PATCH] fix(game): restore the selected deck after reload The save effect wrote null on mount, before the repository was ready, so the restore effect had nothing to read. Keep the saved deck id until the repository is available. Refs: #81 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ src/hooks/use-game-setup.test.ts | 34 +++++++++++++++++++++++++++++--- src/hooks/use-game-setup.ts | 7 ++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18d0b92..56fa57d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [CalVer](https://calver.org/). - A deck no longer ends up with 0 cards when it is selected after saving the token again. The deck is now fetched for the new repository. +- The selected deck is restored after reloading the page and saving the token + again. ## [2026.11.09] - 2026-11-09 diff --git a/src/hooks/use-game-setup.test.ts b/src/hooks/use-game-setup.test.ts index fa25d51..32ae3a5 100644 --- a/src/hooks/use-game-setup.test.ts +++ b/src/hooks/use-game-setup.test.ts @@ -1,4 +1,4 @@ -import { act, renderHook } from '@testing-library/react'; +import { act, renderHook, waitFor } from '@testing-library/react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { DeckManager } from '@/lib/karuta/deck/deck-manager'; @@ -8,6 +8,11 @@ import { useGameSetup } from './use-game-setup'; import type { ProtopediaInMemoryRepository } from 'promidas'; +const STORAGE_KEY = 'pp-karuta-deck-setup'; + +const savedDeckRecipeId = () => + JSON.parse(sessionStorage.getItem(STORAGE_KEY) ?? '{}').selectedDeckRecipeId; + const createRepository = () => ({ getAllFromSnapshot: vi.fn().mockResolvedValue([]), @@ -17,9 +22,9 @@ describe('useGameSetup', () => { // These recipes share the same apiParams, so the snapshot can be reused const [firstRecipe, secondRecipe] = ETO_RECIPES; - const render = (repository: ProtopediaInMemoryRepository) => + const render = (repository: ProtopediaInMemoryRepository | null) => renderHook( - (props: { repository: ProtopediaInMemoryRepository }) => + (props: { repository: ProtopediaInMemoryRepository | null }) => useGameSetup({ ...props, onGameStateCreated: vi.fn() }), { initialProps: { repository } }, ); @@ -56,4 +61,27 @@ describe('useGameSetup', () => { ); expect(replaced.getAllFromSnapshot).not.toHaveBeenCalled(); }); + + it('restores the saved deck once the repository is available', async () => { + sessionStorage.setItem( + STORAGE_KEY, + JSON.stringify({ selectedDeckRecipeId: firstRecipe.id }), + ); + const repository = createRepository(); + const { result, rerender } = render(null); + + await act(async () => {}); + expect(savedDeckRecipeId()).toBe(firstRecipe.id); + + await act(async () => rerender({ repository })); + + await waitFor(() => + expect(result.current.selectedDeckRecipe?.id).toBe(firstRecipe.id), + ); + expect(DeckManager.getPrototypesFromRecipe).toHaveBeenCalledWith( + firstRecipe, + repository, + ); + expect(savedDeckRecipeId()).toBe(firstRecipe.id); + }); }); diff --git a/src/hooks/use-game-setup.ts b/src/hooks/use-game-setup.ts index 7a35586..3f05b48 100644 --- a/src/hooks/use-game-setup.ts +++ b/src/hooks/use-game-setup.ts @@ -537,7 +537,11 @@ export function useGameSetup({ // Save state to sessionStorage whenever it changes useEffect(() => { const state: SavedSetupState = { - selectedDeckRecipeId: selectedDeckRecipe?.id || null, + // Until the repository is ready the deck cannot be restored, so keep the + // saved id for the restore effect to read + selectedDeckRecipeId: + selectedDeckRecipe?.id || + (repository ? null : (loadSetupState()?.selectedDeckRecipeId ?? null)), selectedStackRecipeId: selectedStackRecipe?.id || null, selectedPlayerIds, selectedPlayMode, @@ -545,6 +549,7 @@ export function useGameSetup({ }; saveSetupState(state); }, [ + repository, selectedDeckRecipe, selectedStackRecipe, selectedPlayerIds,