import { create } from 'zustand'; import { AppConfig } from '../types/config'; import * as configService from '../services/configService'; interface ConfigState { config: AppConfig | null; loading: boolean; error: string | null; // Actions loadConfig: () => Promise; saveConfig: (config: AppConfig) => Promise; updateTheme: (theme: string) => Promise; updateLanguage: (language: string) => Promise; } export const useConfigStore = create((set, get) => ({ config: null, loading: false, error: null, loadConfig: async () => { set({ loading: true, error: null }); try { const config = await configService.loadConfig(); set({ config, loading: false }); } catch (error) { set({ error: String(error), loading: false }); } }, saveConfig: async (config: AppConfig) => { set({ loading: true, error: null }); try { await configService.saveConfig(config); set({ config, loading: false }); } catch (error) { set({ error: String(error), loading: false }); throw error; } }, updateTheme: async (theme: string) => { const { config } = get(); if (!config) return; const newConfig = { ...config, theme }; await get().saveConfig(newConfig); }, updateLanguage: async (language: string) => { const { config } = get(); if (!config) return; const newConfig = { ...config, language }; await get().saveConfig(newConfig); }, }));