termix / src /modules /settings /preferences.ts
lekmikdok's picture
Prepare Terax for web deployment
50fe3c9 verified
Raw
History Blame Contribute Delete
712 Bytes
import { create } from "zustand";
import {
DEFAULT_PREFERENCES,
loadPreferences,
onPreferencesChange,
type Preferences,
} from "./store";
type State = Preferences & {
hydrated: boolean;
/** Subscribe & hydrate. Idempotent — safe to call from multiple windows. */
init: () => Promise<void>;
};
let initialized = false;
export const usePreferencesStore = create<State>((set) => ({
...DEFAULT_PREFERENCES,
hydrated: false,
init: async () => {
if (initialized) return;
initialized = true;
const prefs = await loadPreferences();
set({ ...prefs, hydrated: true });
void onPreferencesChange((key, value) => {
set({ [key]: value } as Partial<State>);
});
},
}));