mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-11-03 00:13:09 +00:00
106 lines
3.6 KiB
TypeScript
106 lines
3.6 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';
|
|
import { apiFetch } from '@/lib/browser-navigation';
|
|
import { IS_LITE, IS_LITE_STALWART, LITE_POLICY_PATH, withLiteBuildId } from '@/lib/lite';
|
|
import { applyLitePolicy } from '@/lib/lite-config';
|
|
|
|
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;
|
|
getForcedThemeId: (availableThemeIds?: string[]) => string | null;
|
|
isThemeDisabled: (themeId: string, isBuiltIn: boolean) => boolean;
|
|
isPluginForceEnabled: (pluginId: string) => boolean;
|
|
isPluginApproved: (pluginId: string) => boolean;
|
|
isThemeForceEnabled: (themeId: string) => boolean;
|
|
}
|
|
|
|
export const usePolicyStore = create<PolicyState>()((set, get) => ({
|
|
policy: { ...DEFAULT_POLICY },
|
|
loaded: false,
|
|
|
|
fetchPolicy: async () => {
|
|
// Static Lite build: an optional policy.json next to index.html stands in
|
|
// for the admin server; missing means defaults, and the gates that need
|
|
// the server stay pinned off either way (lib/lite-config.ts).
|
|
const fallback = IS_LITE ? { policy: applyLitePolicy({}) } : {};
|
|
try {
|
|
// Stalwart serves bundle files immutably: a build-id URL instead of no-store.
|
|
const res = IS_LITE_STALWART
|
|
? await apiFetch(withLiteBuildId(LITE_POLICY_PATH))
|
|
: await apiFetch(IS_LITE ? LITE_POLICY_PATH : '/api/admin/policy', IS_LITE ? { cache: 'no-store' } : undefined);
|
|
if (res.ok) {
|
|
const data = await res.json();
|
|
set({ policy: IS_LITE ? applyLitePolicy(data) : data, loaded: true });
|
|
} else {
|
|
set({ ...fallback, loaded: true });
|
|
}
|
|
} catch {
|
|
set({ ...fallback, 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 };
|
|
},
|
|
|
|
getForcedThemeId: (availableThemeIds) => {
|
|
const forceEnabledThemes = get().policy.forceEnabledThemes || [];
|
|
if (!availableThemeIds || availableThemeIds.length === 0) {
|
|
return forceEnabledThemes[0] || null;
|
|
}
|
|
|
|
const available = new Set(availableThemeIds);
|
|
return forceEnabledThemes.find((themeId) => available.has(themeId)) || null;
|
|
},
|
|
|
|
isThemeDisabled: (themeId, isBuiltIn) => {
|
|
const tp = get().policy.themePolicy || DEFAULT_THEME_POLICY;
|
|
if (isBuiltIn) {
|
|
return (tp.disabledBuiltinThemes || []).includes(themeId);
|
|
}
|
|
return (tp.disabledThemes || []).includes(themeId);
|
|
},
|
|
|
|
isPluginForceEnabled: (pluginId) => {
|
|
return (get().policy.forceEnabledPlugins || []).includes(pluginId);
|
|
},
|
|
|
|
isPluginApproved: (pluginId) => {
|
|
return (get().policy.approvedPlugins || []).includes(pluginId);
|
|
},
|
|
|
|
isThemeForceEnabled: (themeId) => {
|
|
return (get().policy.forceEnabledThemes || []).includes(themeId);
|
|
},
|
|
}));
|