feat(plugins): 插件管理 - 区分系统与官方插件权限 (#329)
This commit is contained in:
@@ -3,6 +3,7 @@ import { createJSONStorage, persist } from "zustand/middleware";
|
||||
|
||||
import { localForageStorageForScope } from "@/lib/localforage-storage";
|
||||
import type { PluginInstallation, PluginManifest } from "@/lib/plugins/plugin-types";
|
||||
import type { PluginState } from "@/services/api/plugins";
|
||||
|
||||
export const PLUGIN_STORE_KEY = "infinite-canvas:plugin-store";
|
||||
|
||||
@@ -10,8 +11,10 @@ type PluginStore = {
|
||||
hydrated: boolean;
|
||||
installations: PluginInstallation[];
|
||||
runtimeStatuses: Record<string, string>;
|
||||
pluginStates: Record<string, PluginState>;
|
||||
ensurePlugin: (manifest: PluginManifest) => void;
|
||||
setRuntimeStatuses: (statuses: Record<string, string>) => void;
|
||||
setPluginStates: (states: Record<string, PluginState>) => void;
|
||||
setEnabled: (pluginId: string, enabled: boolean) => void;
|
||||
updateConfig: (pluginId: string, config: Record<string, string | number | boolean>) => void;
|
||||
setError: (pluginId: string, error?: string) => void;
|
||||
@@ -28,6 +31,7 @@ export const usePluginStore = create<PluginStore>()(
|
||||
hydrated: false,
|
||||
installations: [],
|
||||
runtimeStatuses: {},
|
||||
pluginStates: {},
|
||||
ensurePlugin: (manifest) =>
|
||||
set((state) => {
|
||||
const current = state.installations.find((item) => item.manifest.id === manifest.id);
|
||||
@@ -43,6 +47,7 @@ export const usePluginStore = create<PluginStore>()(
|
||||
installations: state.installations.map((item) => item.manifest.id === pluginId ? { ...item, enabled, updatedAt: now(), lastError: undefined } : item),
|
||||
})),
|
||||
setRuntimeStatuses: (runtimeStatuses) => set({ runtimeStatuses }),
|
||||
setPluginStates: (pluginStates) => set({ pluginStates }),
|
||||
updateConfig: (pluginId, config) =>
|
||||
set((state) => ({
|
||||
installations: state.installations.map((item) => item.manifest.id === pluginId ? { ...item, config: { ...item.config, ...config }, updatedAt: now() } : item),
|
||||
@@ -58,8 +63,17 @@ export const usePluginStore = create<PluginStore>()(
|
||||
storage: createJSONStorage(() => localForageStorageForScope()),
|
||||
partialize: (state) => ({ installations: state.installations }),
|
||||
onRehydrateStorage: () => (state) => {
|
||||
if (state) state.hydrated = true;
|
||||
if (state) usePluginStore.setState({ hydrated: true });
|
||||
},
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
export function isPluginEffectivelyEnabled(pluginId: string, fallbackEnabled?: boolean) {
|
||||
const state = usePluginStore.getState();
|
||||
const serverState = state.pluginStates[pluginId];
|
||||
if (serverState) return serverState.effectiveEnabled;
|
||||
if (pluginId in state.runtimeStatuses) return state.runtimeStatuses[pluginId] === "enabled";
|
||||
if (fallbackEnabled !== undefined) return fallbackEnabled;
|
||||
return Boolean(state.installations.find((item) => item.manifest.id === pluginId)?.enabled);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user