index.ts 1.23 KB
import { defineStore } from 'pinia';
import { SystemPermission } from '@/types/system-permission';
import { AppState } from './types';

const useAppStore = defineStore('app', {
  state: (): AppState => ({
    theme: 'light',
    colorWeek: false,
    navbar: true,
    menu: true,
    menuCollapse: false,
    footer: false,
    themeColor: '#165DFF',
    menuWidth: 220,
    globalSettings: false,
    permissions: [],
  }),

  getters: {
    appCurrentSetting(state: AppState): AppState {
      return { ...state };
    },
    appMenu(state: AppState): SystemPermission[] {
      return state.permissions?.filter((item) => item.guard === 'Manage' && item.type === 'Menu') || [];
    },
  },

  actions: {
    // Update app settings
    updateSettings(partial: Partial<AppState>) {
      // @ts-ignore-next-line
      this.$patch(partial);
    },

    // Change theme color
    toggleTheme(dark: boolean) {
      if (dark) {
        this.theme = 'dark';
        document.body.setAttribute('arco-theme', 'dark');
      } else {
        this.theme = 'light';
        document.body.removeAttribute('arco-theme');
      }
    },

    setPermissions(permissions: SystemPermission[]) {
      this.$patch({ permissions });
    },
  },
});

export default useAppStore;