123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import { MESSAGE_STATUS } from "@/constants";
- import { create } from "zustand";
- interface NotificationStore {
- notifications: any[];
- }
- interface NotificationAction {
- changeNotification: ({
- type,
- notification,
- }: {
- type: string;
- notification?: any;
- }) => void;
- }
- const initialState = {
- notifications: [],
- };
- export const useNotificationStore = create<
- NotificationStore & NotificationAction
- >((set, get) => ({
- ...initialState,
- changeNotification: ({ type, notification }) => {
- let new_notification: any = [];
-
- switch (type) {
- case "new_msg":
- // 新消息添加到数组头部
- new_notification = [...notification, ...get().notifications];
- break;
-
- case "update_msg":
- // 加载更多的消息添加到数组尾部
- new_notification = [...get().notifications, ...notification];
- break;
-
- case "clear":
- if (!notification) {
- // 清空所有消息
- const notifications = get().notifications;
- new_notification = notifications.map((item) => ({
- ...item,
- is_read: MESSAGE_STATUS.HAS_READ,
- }));
- } else {
- // 清空指定消息
- new_notification = get().notifications.map((item) => {
- if (notification.includes(item.id)) {
- return {
- ...item,
- is_read: MESSAGE_STATUS.HAS_READ,
- };
- }
- return item;
- });
- }
- break;
-
- default:
- new_notification = get().notifications;
- }
-
- set({ notifications: new_notification });
- },
- }));
|