import { create, StoreApi } from 'zustand'; import localForageLru from '@plebbit/plebbit-react-hooks/dist/lib/localforage-lru/index.js'; interface ThemeState { themes: Record; setTheme: (subplebbitAddress: string, theme: string) => void; getTheme: (subplebbitAddress: string) => string; loadThemes: () => void; } const themeStore = localForageLru.createInstance({ name: 'themeStore', size: 1000, }); const useThemeStore = create((set: StoreApi['setState'], get: StoreApi['getState']) => ({ themes: {}, setTheme: async (subplebbitAddress: string, theme: string) => { const currentThemes = get().themes; const updatedThemes = { ...currentThemes, [subplebbitAddress]: theme }; await themeStore.setItem(subplebbitAddress, theme); set({ themes: updatedThemes }); }, getTheme: (subplebbitAddress: string) => { const currentThemes = get().themes; return currentThemes[subplebbitAddress] || 'yotsuba-b'; }, loadThemes: async () => { const entries: [string, string][] = await themeStore.entries(); const themes: Record = {}; entries.forEach(([key, value]) => { themes[key] = value; }); set({ themes }); }, })); useThemeStore.getState().loadThemes(); export default useThemeStore;