Files
ttstd-infinite-canvas/web/src/lib/localforage-storage.ts

30 lines
989 B
TypeScript

import localforage from "localforage";
import type { StateStorage } from "zustand/middleware";
import { scopedStorageKey } from "@/lib/user-scope";
localforage.config({
name: "infinite-canvas",
storeName: "app_state",
});
export function localForageStorageForScope(scope?: string): StateStorage {
const keyFor = (name: string) => scopedStorageKey(name, scope);
return {
getItem: async (name) => {
if (typeof window === "undefined") return null;
return (await localforage.getItem<string>(keyFor(name))) || null;
},
setItem: async (name, value) => {
if (typeof window === "undefined") return;
await localforage.setItem(keyFor(name), value);
},
removeItem: async (name) => {
if (typeof window === "undefined") return;
await localforage.removeItem(keyFor(name));
},
};
}
export const localForageStorage: StateStorage = localForageStorageForScope();