62 lines
1.6 KiB
TypeScript
Raw Normal View History

import { pinia } from "@/pinia"
2023-06-01 09:24:37 +08:00
import { getSidebarStatus, setSidebarStatus } from "@/utils/cache/local-storage"
import { DeviceEnum, SIDEBAR_CLOSED, SIDEBAR_OPENED } from "@@/constants/app-key"
2024-11-18 19:40:44 +08:00
import { defineStore } from "pinia"
import { reactive, ref, watch } from "vue"
interface Sidebar {
2022-08-24 16:52:01 +08:00
opened: boolean
withoutAnimation: boolean
}
2023-05-24 18:24:06 +08:00
/** 设置侧边栏状态本地缓存 */
function handleSidebarStatus(opened: boolean) {
opened ? setSidebarStatus(SIDEBAR_OPENED) : setSidebarStatus(SIDEBAR_CLOSED)
}
2022-08-24 16:52:01 +08:00
export const useAppStore = defineStore("app", () => {
// 侧边栏状态
const sidebar: Sidebar = reactive({
2023-05-24 18:24:06 +08:00
opened: getSidebarStatus() !== SIDEBAR_CLOSED,
2022-08-24 16:52:01 +08:00
withoutAnimation: false
})
// 设备类型
const device = ref<DeviceEnum>(DeviceEnum.Desktop)
2022-08-24 16:52:01 +08:00
2024-11-19 20:14:23 +08:00
// 监听侧边栏 opened 状态
2023-05-24 18:24:06 +08:00
watch(
() => sidebar.opened,
(opened) => {
handleSidebarStatus(opened)
}
2023-05-24 18:24:06 +08:00
)
// 切换侧边栏
2022-08-24 16:52:01 +08:00
const toggleSidebar = (withoutAnimation: boolean) => {
sidebar.opened = !sidebar.opened
sidebar.withoutAnimation = withoutAnimation
}
// 关闭侧边栏
2022-08-24 16:52:01 +08:00
const closeSidebar = (withoutAnimation: boolean) => {
sidebar.opened = false
sidebar.withoutAnimation = withoutAnimation
}
// 切换设备类型
const toggleDevice = (value: DeviceEnum) => {
2022-08-24 16:52:01 +08:00
device.value = value
}
return { device, sidebar, toggleSidebar, closeSidebar, toggleDevice }
})
/**
* @description SPA pinia 使 store
* @description SSR setup 使 store
*/
export function useAppStoreOutside() {
return useAppStore(pinia)
}