WeKnora/frontend/src/stores/deploymentCapabilities.ts

58 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getDeploymentCapabilities } from '@/api/system'
import { useAuthStore } from '@/stores/auth'
import {
isDeploymentCapabilitySupported,
type DeploymentCapabilityKey,
type DeploymentCapabilityMap,
} from '@/config/deploymentCapabilities'
export const useDeploymentCapabilitiesStore = defineStore('deploymentCapabilities', () => {
const edition = ref('')
const capabilities = ref<DeploymentCapabilityMap>({})
const loaded = ref(false)
const loadError = ref('')
let loadingPromise: Promise<void> | null = null
const ensureLoaded = async (force = false): Promise<void> => {
if (loaded.value && !force) return
if (loadingPromise) return loadingPromise
loadingPromise = (async () => {
try {
const response = await getDeploymentCapabilities()
edition.value = response.data?.edition || ''
capabilities.value = response.data?.capabilities || {}
loadError.value = ''
} catch (error) {
// 能力探测失败时保持 fail-open;权限仍由后端路由最终校验。
capabilities.value = {}
loadError.value = error instanceof Error ? error.message : String(error)
} finally {
loaded.value = true
loadingPromise = null
}
})()
return loadingPromise
}
const isSupported = (key?: DeploymentCapabilityKey) => {
const authStore = useAuthStore()
return isDeploymentCapabilitySupported(capabilities.value, key, {
liteMode: authStore.isLiteMode,
edition: edition.value,
})
}
return {
edition,
capabilities,
loaded,
loadError,
ensureLoaded,
isSupported,
}
})