From 11108b62d5f6d9282c03d50ae125f4442fa6ba46 Mon Sep 17 00:00:00 2001 From: sohrab <4444588+sohrab-@users.noreply.github.com> Date: Mon, 24 Apr 2023 18:47:44 +1000 Subject: [PATCH] chore: align to new Zustand interface --- src/hooks/useConfigStore.ts | 7 ++++--- src/hooks/useSessionStore.ts | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/hooks/useConfigStore.ts b/src/hooks/useConfigStore.ts index 3a3443c..939fcf0 100644 --- a/src/hooks/useConfigStore.ts +++ b/src/hooks/useConfigStore.ts @@ -1,9 +1,9 @@ import produce from "immer"; import { PersistentState } from "types/state"; import { DEFAULT_PERSISTENT_STATE } from "utils/state"; -import create from "zustand"; -import { persist } from "zustand/middleware"; -import shallow from "zustand/shallow"; +import { create } from "zustand"; +import { createJSONStorage, persist } from "zustand/middleware"; +import { shallow } from "zustand/shallow"; /** * Provides access to the LocalStorage Zustand store @@ -19,6 +19,7 @@ export const useConfigStore = create()( { name: "bcsol-config", version: 1, + storage: createJSONStorage(() => localStorage), } ) ); diff --git a/src/hooks/useSessionStore.ts b/src/hooks/useSessionStore.ts index aa794f0..83a90ce 100644 --- a/src/hooks/useSessionStore.ts +++ b/src/hooks/useSessionStore.ts @@ -4,9 +4,13 @@ import { DEFAULT_SESSION_STATE_WITH_UNDO, DEFAULT_SESSION_STATE_WITHOUT_UNDO, } from "utils/state"; -import create from "zustand"; -import { persist, subscribeWithSelector } from "zustand/middleware"; -import shallow from "zustand/shallow"; +import { create } from "zustand"; +import { + createJSONStorage, + persist, + subscribeWithSelector, +} from "zustand/middleware"; +import { shallow } from "zustand/shallow"; // TODO implement undo/redo once this is resolved: https://github.com/charkour/zundo/issues/38 @@ -23,12 +27,11 @@ export const useSessionStoreWithUndo = create()( }), { name: "bscol-with-undo", - getStorage: () => sessionStorage, - serialize: ({ state, ...theRest }) => - JSON.stringify({ - ...theRest, - state: { ...state, lastUpdatedAt: new Date().getTime() }, - }), + storage: createJSONStorage(() => sessionStorage), + partialize: (state) => ({ + ...state, + lastUpdatedAt: new Date().getTime(), + }), } ) ); @@ -50,7 +53,7 @@ export const useSessionStoreWithoutUndo = create()( })), { name: "bscol-without-undo", - getStorage: () => sessionStorage, + storage: createJSONStorage(() => sessionStorage), } ) );