zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { store } from '@/store';
import { defineStore } from 'pinia';
import { DeviceType } from '@bole-core/core';
import Cookies from 'js-cookie';
import { ThemeColorName } from '@/constants';
 
export interface AppState {
  theme: ThemeColorName;
  deviceType: DeviceType;
  isMobile: boolean;
  clientHeight: number;
  sidebar: {
    opened: boolean;
    withoutAnimation: boolean;
  };
}
 
export const useAppStore = defineStore({
  id: 'app',
  state: (): AppState => ({
    theme: 'default',
    deviceType: DeviceType.Desktop,
    isMobile: false,
    sidebar: {
      opened: Cookies.get('sidebarStatus') ? !!Number(Cookies.get('sidebarStatus')) : true,
      withoutAnimation: false,
    },
    clientHeight: document.body.clientHeight,
  }),
  getters: {
    getSidebarStatus(): boolean {
      return this.sidebar.opened;
    },
    getDevice(): DeviceType {
      return this.deviceType;
    },
    clientHeightGetter(): number {
      return this.clientHeight;
    },
  },
  actions: {
    toggleDevice(deviceType: DeviceType) {
      this.deviceType = deviceType;
      this.isMobile = deviceType !== DeviceType.Desktop;
    },
    toggleSideBar() {
      this.sidebar.opened = !this.sidebar.opened;
      this.sidebar.withoutAnimation = false;
      if (this.sidebar.opened) {
        Cookies.set('sidebarStatus', '1');
      } else {
        Cookies.set('sidebarStatus', '0');
      }
    },
    closeSideBar({ withoutAnimation }: { withoutAnimation: boolean }) {
      Cookies.set('sidebarStatus', '1');
      this.sidebar.opened = false;
      this.sidebar.withoutAnimation = withoutAnimation;
    },
    changeClientHeight(height: number) {
      this.clientHeight = height;
    },
 
    setTheme(themeName: ThemeColorName) {
      this.theme = themeName;
    },
  },
});
 
export function useAppStoreHook() {
  return useAppStore(store);
}