Skip to content

Commit

Permalink
feat: persist store in session storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ej-shafran committed Oct 22, 2024
1 parent 0aceb3b commit a6fbf43
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/game/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,7 +77,12 @@ export function restartGame() {
useGameStore.setState(initialStore);
}

export const useGameStore = create<GameStore>()(() => initialStore);
export const useGameStore = create<GameStore>()(
persist(() => initialStore, {
name: "game",
storage: createJSONStorage(() => sessionStorage),
}),
);

export const useCurrentPlayer = () =>
useGameStore((store) => store.game.currentPlayer);
Expand Down

0 comments on commit a6fbf43

Please sign in to comment.