import { create } from 'zustand'; import { persist } from 'zustand/middleware'; // 租户信息接口 export interface Tenant { id: string; name: string; code: string; } // 面包屑接口 export interface Breadcrumb { title: string; path?: string; } // 通知接口 export interface Notification { id: string; type: 'success' | 'error' | 'warning' | 'info'; title: string; message: string; duration?: number; timestamp: number; } // Global state interface export interface GlobalState { theme: 'light' | 'dark' | 'auto'; language: 'zh-CN' | 'en-US'; sidebarCollapsed: boolean; globalLoading: boolean; breadcrumbs: Breadcrumb[]; notifications: Notification[]; currentTenant: Tenant | null; // Actions setTheme: (theme: 'light' | 'dark' | 'auto') => void; setLanguage: (language: 'zh-CN' | 'en-US') => void; toggleSidebar: () => void; setSidebarCollapsed: (collapsed: boolean) => void; setGlobalLoading: (loading: boolean) => void; setBreadcrumbs: (breadcrumbs: Breadcrumb[]) => void; addBreadcrumb: (breadcrumb: Breadcrumb) => void; addNotification: (notification: Omit) => void; removeNotification: (id: string) => void; clearNotifications: () => void; setCurrentTenant: (tenant: Tenant | null) => void; // Getters getTheme: () => 'light' | 'dark' | 'auto'; getLanguage: () => 'zh-CN' | 'en-US'; getSidebarCollapsed: () => boolean; getGlobalLoading: () => boolean; getBreadcrumbs: () => Breadcrumb[]; getNotifications: () => Notification[]; getCurrentTenant: () => Tenant | null; } // Create Global store export const useGlobalStore = create()( persist( (set, get) => ({ // 初始状态 theme: 'light', language: 'zh-CN', sidebarCollapsed: false, globalLoading: false, breadcrumbs: [], notifications: [], currentTenant: null, // Actions setTheme: (theme) => set({ theme }), setLanguage: (language) => set({ language }), toggleSidebar: () => set((state) => ({ sidebarCollapsed: !state.sidebarCollapsed })), setSidebarCollapsed: (collapsed) => set({ sidebarCollapsed: collapsed }), setGlobalLoading: (loading) => set({ globalLoading: loading }), setBreadcrumbs: (breadcrumbs) => set({ breadcrumbs }), addBreadcrumb: (breadcrumb) => set((state) => ({ breadcrumbs: [...state.breadcrumbs, breadcrumb] })), addNotification: (notification) => { const id = Date.now().toString(); const newNotification = { ...notification, id, timestamp: Date.now(), duration: notification.duration || 4500 }; set((state) => ({ notifications: [...state.notifications, newNotification] })); // 自动移除通知 if (newNotification.duration && newNotification.duration > 0) { setTimeout(() => { get().removeNotification(id); }, newNotification.duration); } }, removeNotification: (id) => set((state) => ({ notifications: state.notifications.filter(n => n.id !== id) })), clearNotifications: () => set({ notifications: [] }), setCurrentTenant: (tenant) => set({ currentTenant: tenant }), // Getters getTheme: () => get().theme, getLanguage: () => get().language, getSidebarCollapsed: () => get().sidebarCollapsed, getGlobalLoading: () => get().globalLoading, getBreadcrumbs: () => get().breadcrumbs, getNotifications: () => get().notifications, getCurrentTenant: () => get().currentTenant, }), { name: 'global-storage', partialize: (state) => ({ theme: state.theme, language: state.language, sidebarCollapsed: state.sidebarCollapsed, currentTenant: state.currentTenant }) } ) ); export default useGlobalStore;