From a6fbf4380de946deddc4cb314bbbe636862ba96a Mon Sep 17 00:00:00 2001 From: ej-shafran Date: Tue, 22 Oct 2024 19:19:04 +0300 Subject: [PATCH] feat: persist store in session storage --- src/game/store.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/game/store.ts b/src/game/store.ts index 28679d7..bb8c009 100644 --- a/src/game/store.ts +++ b/src/game/store.ts @@ -2,6 +2,7 @@ import { assert } from "@/lib/assert"; import * as game from "."; import { create } from "zustand"; +import { persist, createJSONStorage } from "zustand/middleware"; interface GameStore { game: game.Game; @@ -76,7 +77,12 @@ export function restartGame() { useGameStore.setState(initialStore); } -export const useGameStore = create()(() => initialStore); +export const useGameStore = create()( + persist(() => initialStore, { + name: "game", + storage: createJSONStorage(() => sessionStorage), + }), +); export const useCurrentPlayer = () => useGameStore((store) => store.game.currentPlayer);