import { DeviceEnum, SIDEBAR_CLOSED, SIDEBAR_OPENED } from "@/constants/app-key" import { pinia } from "@/store" import { getSidebarStatus, setSidebarStatus } from "@/utils/cache/local-storage" import { defineStore } from "pinia" import { reactive, ref, watch } from "vue" interface Sidebar { opened: boolean withoutAnimation: boolean } /** 设置侧边栏状态本地缓存 */ function handleSidebarStatus(opened: boolean) { opened ? setSidebarStatus(SIDEBAR_OPENED) : setSidebarStatus(SIDEBAR_CLOSED) } export const useAppStore = defineStore("app", () => { /** 侧边栏状态 */ const sidebar: Sidebar = reactive({ opened: getSidebarStatus() !== SIDEBAR_CLOSED, withoutAnimation: false }) /** 设备类型 */ const device = ref(DeviceEnum.Desktop) // 监听侧边栏 opened 状态 watch( () => sidebar.opened, opened => handleSidebarStatus(opened) ) /** 切换侧边栏 */ const toggleSidebar = (withoutAnimation: boolean) => { sidebar.opened = !sidebar.opened sidebar.withoutAnimation = withoutAnimation } /** 关闭侧边栏 */ const closeSidebar = (withoutAnimation: boolean) => { sidebar.opened = false sidebar.withoutAnimation = withoutAnimation } /** 切换设备类型 */ const toggleDevice = (value: DeviceEnum) => { device.value = value } return { device, sidebar, toggleSidebar, closeSidebar, toggleDevice } }) /** * 在 SPA 应用中可用于在 pinia 实例被激活前使用 store * 在 SSR 应用中可用于在 setup 外使用 store */ export function useAppStoreHook() { return useAppStore(pinia) }