// ============================================================ // Redux Toolkit — UI Slice // 大厂模式:RTK 管理全局通知队列、异步 loading 等跨模块状态 // ============================================================ import { createSlice, PayloadAction } from '@reduxjs/toolkit'; export type NotificationType = 'success' | 'error' | 'warning' | 'info'; export interface Notification { id: string; type: NotificationType; message: string; duration?: number; // ms, default 3000 } interface UISliceState { notifications: Notification[]; globalLoading: boolean; } const initialState: UISliceState = { notifications: [], globalLoading: false, }; export const uiSlice = createSlice({ name: 'ui', initialState, reducers: { addNotification: (state, action: PayloadAction>) => { state.notifications.push({ ...action.payload, id: `${Date.now()}-${Math.random().toString(36).slice(2)}`, }); }, removeNotification: (state, action: PayloadAction) => { state.notifications = state.notifications.filter((n) => n.id !== action.payload); }, clearNotifications: (state) => { state.notifications = []; }, setGlobalLoading: (state, action: PayloadAction) => { state.globalLoading = action.payload; }, }, }); export const { addNotification, removeNotification, clearNotifications, setGlobalLoading } = uiSlice.actions;