52 lines
1.6 KiB
TypeScript
Raw Normal View History

import type { LayoutsConfig } from "@/layouts/config"
2024-11-18 19:40:44 +08:00
import type { Ref } from "vue"
import { layoutsConfig } from "@/layouts/config"
import { pinia } from "@/pinia"
import { setLayoutsConfig } from "@/utils/cache/local-storage"
2024-11-18 19:40:44 +08:00
import { defineStore } from "pinia"
import { ref, watch } from "vue"
type SettingsStore = {
// 使用映射类型来遍历 LayoutsConfig 对象的键
[Key in keyof LayoutsConfig]: Ref<LayoutsConfig[Key]>
}
type SettingsStoreKey = keyof SettingsStore
export const useSettingsStore = defineStore("settings", () => {
// 状态对象
const state = {} as SettingsStore
// 遍历 LayoutsConfig 对象的键值对
for (const [key, value] of Object.entries(layoutsConfig)) {
// 使用类型断言来指定 key 的类型,将 value 包装在 ref 函数中,创建一个响应式变量
const refValue = ref(value)
2024-11-18 19:40:44 +08:00
// @ts-expect-error ignore
state[key as SettingsStoreKey] = refValue
// 监听每个响应式变量
watch(refValue, () => {
// 缓存
2024-11-25 19:02:36 +08:00
const settings = getCacheData()
setLayoutsConfig(settings)
})
}
// 获取要缓存的数据:将 state 对象转化为 settings 对象
2024-11-25 19:02:36 +08:00
const getCacheData = () => {
const settings = {} as LayoutsConfig
for (const [key, value] of Object.entries(state)) {
2024-11-18 19:40:44 +08:00
// @ts-expect-error ignore
settings[key as SettingsStoreKey] = value.value
}
return settings
}
return state
})
/**
* @description SPA pinia 使 store
* @description SSR setup 使 store
*/
export function useSettingsStoreOutside() {
return useSettingsStore(pinia)
}