Files
smart-crop-ui/crop-x-new/src/stores/modules/global.ts
peng 87dbc7548a refactor: 优化认证系统和组件类型安全性
- 新增 safeLocalStorage 工具函数增强本地存储安全性
- 简化认证流程,重构用户数据结构和 token 管理
- 修复多个模块的 TypeScript 类型错误和导入问题
- 优化状态管理,重构各模块 store 结构
- 清理冗余代码,移除未使用的组件和函数
- 改进错误处理和边界情况处理
- 更新配置文件以支持最新的类型检查

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 14:52:52 +08:00

130 lines
3.8 KiB
TypeScript

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<Notification, 'id' | 'timestamp'>) => 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<GlobalState>()(
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;