moduels setigns rmeove from frotneend
This commit is contained in:
@@ -13,16 +13,13 @@ import {
|
||||
fetchModuleSettings,
|
||||
createModuleSetting,
|
||||
updateModuleSetting,
|
||||
fetchModuleEnableSettings,
|
||||
updateModuleEnableSettings,
|
||||
AccountSetting,
|
||||
ModuleSetting,
|
||||
ModuleEnableSettings,
|
||||
AccountSettingsError,
|
||||
} from '../services/api';
|
||||
|
||||
// Version for cache busting - increment when structure changes
|
||||
const SETTINGS_STORE_VERSION = 2;
|
||||
const SETTINGS_STORE_VERSION = 4;
|
||||
|
||||
const getAccountSettingsErrorMessage = (error: AccountSettingsError): string => {
|
||||
switch (error.type) {
|
||||
@@ -38,7 +35,6 @@ const getAccountSettingsErrorMessage = (error: AccountSettingsError): string =>
|
||||
interface SettingsState {
|
||||
accountSettings: Record<string, AccountSetting>;
|
||||
moduleSettings: Record<string, Record<string, ModuleSetting>>;
|
||||
moduleEnableSettings: ModuleEnableSettings | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
|
||||
@@ -48,9 +44,6 @@ interface SettingsState {
|
||||
updateAccountSetting: (key: string, value: any) => Promise<void>;
|
||||
loadModuleSettings: (moduleName: string) => Promise<void>;
|
||||
updateModuleSetting: (moduleName: string, key: string, value: any) => Promise<void>;
|
||||
loadModuleEnableSettings: () => Promise<void>;
|
||||
updateModuleEnableSettings: (data: Partial<ModuleEnableSettings>) => Promise<void>;
|
||||
isModuleEnabled: (moduleName: string) => boolean;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
@@ -59,11 +52,8 @@ export const useSettingsStore = create<SettingsState>()(
|
||||
(set, get) => ({
|
||||
accountSettings: {},
|
||||
moduleSettings: {},
|
||||
moduleEnableSettings: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
_moduleEnableLastFetched: 0 as number | undefined,
|
||||
_moduleEnableInFlight: null as Promise<ModuleEnableSettings> | null,
|
||||
|
||||
loadAccountSettings: async () => {
|
||||
set({ loading: true, error: null });
|
||||
@@ -183,60 +173,10 @@ export const useSettingsStore = create<SettingsState>()(
|
||||
}
|
||||
},
|
||||
|
||||
loadModuleEnableSettings: async () => {
|
||||
const state = get() as any;
|
||||
const now = Date.now();
|
||||
// Use cached value if fetched within last 60s
|
||||
if (state.moduleEnableSettings && state._moduleEnableLastFetched && now - state._moduleEnableLastFetched < 60000) {
|
||||
return;
|
||||
}
|
||||
// Coalesce concurrent calls
|
||||
if (state._moduleEnableInFlight) {
|
||||
await state._moduleEnableInFlight;
|
||||
return;
|
||||
}
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const inFlight = fetchModuleEnableSettings();
|
||||
(state as any)._moduleEnableInFlight = inFlight;
|
||||
const settings = await inFlight;
|
||||
set({ moduleEnableSettings: settings, loading: false, _moduleEnableLastFetched: Date.now() });
|
||||
} catch (error: any) {
|
||||
// On 429/403, avoid loops; cache the failure timestamp and do not retry automatically
|
||||
if (error?.status === 429 || error?.status === 403) {
|
||||
set({ loading: false, _moduleEnableLastFetched: Date.now() });
|
||||
return;
|
||||
}
|
||||
set({ error: error.message, loading: false, _moduleEnableLastFetched: Date.now() });
|
||||
} finally {
|
||||
(get() as any)._moduleEnableInFlight = null;
|
||||
}
|
||||
},
|
||||
|
||||
updateModuleEnableSettings: async (data: Partial<ModuleEnableSettings>) => {
|
||||
set({ loading: true, error: null });
|
||||
try {
|
||||
const settings = await updateModuleEnableSettings(data);
|
||||
set({ moduleEnableSettings: settings, loading: false });
|
||||
} catch (error: any) {
|
||||
set({ error: error.message, loading: false });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
isModuleEnabled: (moduleName: string): boolean => {
|
||||
const settings = get().moduleEnableSettings;
|
||||
if (!settings) return true; // Default to enabled if not loaded
|
||||
|
||||
const enabledKey = `${moduleName}_enabled` as keyof ModuleEnableSettings;
|
||||
return settings[enabledKey] !== false; // Default to true if not set
|
||||
},
|
||||
|
||||
reset: () => {
|
||||
set({
|
||||
accountSettings: {},
|
||||
moduleSettings: {},
|
||||
moduleEnableSettings: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
});
|
||||
@@ -244,23 +184,11 @@ export const useSettingsStore = create<SettingsState>()(
|
||||
}),
|
||||
{
|
||||
name: 'settings-storage',
|
||||
version: SETTINGS_STORE_VERSION, // Add version for cache busting
|
||||
version: SETTINGS_STORE_VERSION,
|
||||
partialize: (state) => ({
|
||||
accountSettings: state.accountSettings,
|
||||
moduleSettings: state.moduleSettings,
|
||||
moduleEnableSettings: state.moduleEnableSettings,
|
||||
}),
|
||||
// Migrate function to handle version changes
|
||||
migrate: (persistedState: any, version: number) => {
|
||||
if (version < SETTINGS_STORE_VERSION) {
|
||||
// Clear module enable settings on version upgrade
|
||||
return {
|
||||
...persistedState,
|
||||
moduleEnableSettings: null,
|
||||
};
|
||||
}
|
||||
return persistedState;
|
||||
},
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user