Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 31 additions & 3 deletions src/hooks/use-game-setup.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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([]),
Expand All @@ -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 } },
);
Expand Down Expand Up @@ -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);
});
});
7 changes: 6 additions & 1 deletion src/hooks/use-game-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,19 @@ 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,
selectedTatamiSize,
};
saveSetupState(state);
}, [
repository,
selectedDeckRecipe,
selectedStackRecipe,
selectedPlayerIds,
Expand Down
Loading