import { NoticeRep, updateGlobalNoticeApi } from "@/api/home"; import { Result } from "@/types"; import { create } from "zustand"; interface State { sourceMap: Map; unread: number; // 未读数量 userUnred: number; // 未读数量 notices: NoticeRep[]; // 总数据 promotion_count: number; } interface Action { setSourceMap: (key: number, value: NoticeRep) => void; hasValue: (key: number) => boolean; setNotices: (notices: NoticeRep[], unread: number) => void; setReadNotices: (id: number) => Promise>; setUserUnread: (value: number) => void; setPromotionCount: (value: number) => void; } const initialState: State = { sourceMap: new Map(), unread: 0, userUnred: 0, notices: [], promotion_count: 0, }; export const useGlobalNoticeStore = create()((set, get) => { return { ...initialState, setSourceMap: (key, value) => set((state) => { const newMap = new Map(state.sourceMap); newMap.set(key, value); return { sourceMap: newMap }; }), hasValue: (key) => get().sourceMap.has(key), setNotices: (notices, unread: number) => set((state) => ({ ...state, notices, unread })), setUserUnread: (value: number) => set((state) => ({ ...state, userUnred: value })), setPromotionCount: (value: number) => set((state) => ({ ...state, promotion_count: value })), setReadNotices: (id: number) => { return updateGlobalNoticeApi(id); }, reset: () => set(initialState), }; });