add indexeddb storage for zustand

This commit is contained in:
jeffvli
2025-11-13 09:24:26 -08:00
parent a484628e13
commit 9fda3cd49a
+17 -1
View File
@@ -1,5 +1,6 @@
import { del, get, set } from 'idb-keyval';
import mergeWith from 'lodash/mergeWith';
import { StateStorage } from 'zustand/middleware';
/**
* A custom deep merger that will replace all 'columns' items with the persistent
* state, instead of the default merge behavior. This is important to preserve the user's
@@ -17,3 +18,18 @@ export const mergeOverridingColumns = <T>(persistedState: unknown, currentState:
return undefined;
});
};
export const idbStateStorage: StateStorage = {
getItem: async (name: string): Promise<null | string> => {
console.log(name, 'has been retrieved');
return (await get(name)) || null;
},
removeItem: async (name: string): Promise<void> => {
console.log(name, 'has been deleted');
await del(name);
},
setItem: async (name: string, value: string): Promise<void> => {
console.log(name, 'with value', value, 'has been saved');
await set(name, value);
},
};