refactor: separate the migration

This commit is contained in:
MAZE 2024-06-16 18:09:51 +04:30
parent 7658842324
commit c35409ce0a

View file

@ -41,14 +41,24 @@ export const usePresetStore = create<PresetStore>()(
merge: (persisted, current) => merge: (persisted, current) =>
merge(current, persisted as Partial<PresetStore>), merge(current, persisted as Partial<PresetStore>),
migrate: (persistedState, version) => { migrate,
const persisted = persistedState as Partial<PresetStore>; name: 'moodist-presets',
partialize: state => ({ presets: state.presets }),
skipHydration: true,
storage: createJSONStorage(() => localStorage),
version: 1,
},
),
);
function migrate(persistedState: unknown, version: number) {
let persisted = persistedState as Partial<PresetStore>;
/** /**
* In version 0, presets didn't have an ID * In version 0, presets didn't have an ID
*/ */
if (version === 0) { if (version < 1) {
return { persisted = {
...persisted, ...persisted,
presets: (persisted.presets || []).map(preset => { presets: (persisted.presets || []).map(preset => {
if (preset.id) return preset; if (preset.id) return preset;
@ -58,13 +68,4 @@ export const usePresetStore = create<PresetStore>()(
} }
return persisted as PresetStore; return persisted as PresetStore;
}, }
name: 'moodist-presets',
partialize: state => ({ presets: state.presets }),
skipHydration: true,
storage: createJSONStorage(() => localStorage),
version: 1,
},
),
);