moduel setgins fixed

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-20 22:49:31 +00:00
parent 5c9ef81aba
commit 646095da65
7 changed files with 220 additions and 54 deletions

View File

@@ -0,0 +1,59 @@
/**
* Module Settings Store (Zustand)
* Manages global module enable/disable state (platform-wide)
* Settings are controlled via Django Admin only - NOT account-specific
* No persistence needed since this is the same for all users
*/
import { create } from 'zustand';
import { fetchGlobalModuleSettings, GlobalModuleSettings } from '../services/api';
interface ModuleState {
settings: GlobalModuleSettings | null;
loading: boolean;
error: string | null;
// Actions
loadModuleSettings: () => Promise<void>;
isModuleEnabled: (moduleName: string) => boolean;
reset: () => void;
}
export const useModuleStore = create<ModuleState>()((set, get) => ({
settings: null,
loading: false,
error: null,
loadModuleSettings: async () => {
set({ loading: true, error: null });
try {
const settings = await fetchGlobalModuleSettings();
console.log('Loaded global module settings:', settings);
set({ settings, loading: false });
} catch (error: any) {
console.error('Failed to load global module settings:', error);
set({
error: error.message || 'Failed to load module settings',
loading: false
});
}
},
isModuleEnabled: (moduleName: string): boolean => {
const { settings } = get();
// Default to true while settings are loading (better UX)
// Once settings load, they will control visibility
if (!settings) {
return true;
}
const fieldName = `${moduleName.toLowerCase()}_enabled` as keyof GlobalModuleSettings;
const enabled = settings[fieldName] === true;
console.log(`Module check for '${moduleName}' (${fieldName}): ${enabled}`);
return enabled;
},
reset: () => {
set({ settings: null, loading: false, error: null });
},
}));