el.xwx.moe/store/localSettings.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

import { create } from "zustand";
type LocalSettings = {
2023-11-24 06:50:16 -06:00
theme: string;
};
type LocalSettingsStore = {
settings: LocalSettings;
updateSettings: (settings: LocalSettings) => void;
2023-11-24 06:50:16 -06:00
setSettings: () => void;
};
const useLocalSettingsStore = create<LocalSettingsStore>((set) => ({
settings: {
2023-11-24 06:50:16 -06:00
theme: "",
},
updateSettings: async (newSettings) => {
2023-11-24 06:50:16 -06:00
if (
newSettings.theme &&
newSettings.theme !== localStorage.getItem("theme")
) {
localStorage.setItem("theme", newSettings.theme);
const localTheme = localStorage.getItem("theme");
document
.querySelector("html")
?.setAttribute("data-theme", localTheme || "");
}
set((state) => ({ settings: { ...state.settings, ...newSettings } }));
},
2023-11-24 06:50:16 -06:00
setSettings: async () => {
if (!localStorage.getItem("theme")) {
localStorage.setItem("theme", "dark");
}
const localTheme = localStorage.getItem("theme");
document
.querySelector("html")
?.setAttribute("data-theme", localTheme || "");
},
}));
export default useLocalSettingsStore;