feat(subplebbit): add automatic theme based on nsfw or sfw tags

This commit is contained in:
Tom (plebeius.eth)
2024-06-17 12:17:36 +02:00
parent 77ccf0c849
commit 22c1acdd3d
20 changed files with 189 additions and 87 deletions
+31 -7
View File
@@ -1,16 +1,40 @@
import { create, StoreApi } from 'zustand';
import localForageLru from '@plebbit/plebbit-react-hooks/dist/lib/localforage-lru/index.js';
interface ThemeState {
theme: string;
setTheme: (theme: string) => void;
themes: Record<string, string>;
setTheme: (subplebbitAddress: string, theme: string) => void;
getTheme: (subplebbitAddress: string) => string;
loadThemes: () => void;
}
const useThemeStore = create<ThemeState>((set: StoreApi<ThemeState>['setState']) => ({
theme: localStorage.getItem('theme') || 'yotsuba',
setTheme: (theme: string) => {
localStorage.setItem('theme', theme);
set({ theme });
const themeStore = localForageLru.createInstance({
name: 'themeStore',
size: 1000,
});
const useThemeStore = create<ThemeState>((set: StoreApi<ThemeState>['setState'], get: StoreApi<ThemeState>['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<string, string> = {};
entries.forEach(([key, value]) => {
themes[key] = value;
});
set({ themes });
},
}));
useThemeStore.getState().loadThemes();
export default useThemeStore;