Files
SRCmail/stores/policy-store.ts
T
Linus Rath 29a222eef4 feat: add plugin/theme disable gates and move policy controls to their admin pages
- Add `pluginsEnabled` and `themesEnabled` master feature gates to FeatureGates
- Move theme policy UI (default theme, built-in/admin theme toggles, user uploads toggle) from policy page to themes admin page
- Add plugin policy UI (plugins enabled toggle) to plugins admin page
- Remove theme policy section and plugin/theme gates from policy page (with note directing to respective pages)
- Hide Themes and Plugins settings tabs when their feature gate is disabled
- Fix dark mode visibility of all admin toggle switches (bg-white → bg-background, increase off-state track opacity)
2026-03-25 00:44:04 +01:00

70 lines
1.9 KiB
TypeScript

import { create } from 'zustand';
import type { SettingsPolicy, FeatureGates, SettingRestriction, ThemePolicy } from '@/lib/admin/types';
import { DEFAULT_POLICY, DEFAULT_THEME_POLICY } from '@/lib/admin/types';
interface PolicyState {
policy: SettingsPolicy;
loaded: boolean;
fetchPolicy: () => Promise<void>;
isSettingLocked: (key: string) => boolean;
isSettingHidden: (key: string) => boolean;
isFeatureEnabled: (feature: keyof FeatureGates) => boolean;
getRestriction: (key: string) => SettingRestriction | undefined;
getEffectiveDefault: (key: string) => unknown;
getThemePolicy: () => ThemePolicy;
isThemeDisabled: (themeId: string, isBuiltIn: boolean) => boolean;
}
export const usePolicyStore = create<PolicyState>()((set, get) => ({
policy: { ...DEFAULT_POLICY },
loaded: false,
fetchPolicy: async () => {
try {
const res = await fetch('/api/admin/policy');
if (res.ok) {
const data = await res.json();
set({ policy: data, loaded: true });
} else {
set({ loaded: true });
}
} catch {
set({ loaded: true });
}
},
isSettingLocked: (key) => {
const r = get().policy.restrictions[key];
return r?.locked === true;
},
isSettingHidden: (key) => {
const r = get().policy.restrictions[key];
return r?.hidden === true;
},
isFeatureEnabled: (feature) => {
return get().policy.features[feature] ?? true;
},
getRestriction: (key) => {
return get().policy.restrictions[key];
},
getEffectiveDefault: (key) => {
return get().policy.defaults[key];
},
getThemePolicy: () => {
return get().policy.themePolicy || { ...DEFAULT_THEME_POLICY };
},
isThemeDisabled: (themeId, isBuiltIn) => {
const tp = get().policy.themePolicy || DEFAULT_THEME_POLICY;
if (isBuiltIn) {
return (tp.disabledBuiltinThemes || []).includes(themeId);
}
return (tp.disabledThemes || []).includes(themeId);
},
}));