| 1 | import { create } from 'zustand'; |
| 2 | import { combine } from 'zustand/middleware'; |
| 3 | import lodash from 'lodash'; |
| 4 | import { PublishProgressRes } from '../../electron/main/plat/pub/PubItemVideo'; |
| 5 | import { onVideoPublishProgress } from '../icp/receiveMsg'; |
| 6 | import { PubStatus } from '../../commont/publish/PublishEnum'; |
| 7 | |
| 8 | export interface NoticeItem { |
| 9 | title: string; |
| 10 | time: Date; |
| 11 | // 以下为不同类型通知独有的字段 |
| 12 | // 发布 |
| 13 | pub?: { |
| 14 | // 发布状态 |
| 15 | status: PubStatus; |
| 16 | // 发布记录进度 |
| 17 | progressList: PublishProgressRes[]; |
| 18 | }; |
| 19 | id: string | number; |
| 20 | } |
| 21 | |
| 22 | export enum NoticeType { |
| 23 | // 发布通知 |
| 24 | PubNotice = '1', |
| 25 | } |
| 26 | |
| 27 | export interface IBellMessageStroe { |
| 28 | noticeMap: Map<NoticeType, NoticeItem[]>; |
| 29 | } |
| 30 | |
| 31 | const store: IBellMessageStroe = { |
| 32 | noticeMap: new Map<NoticeType, NoticeItem[]>(), |
| 33 | }; |
| 34 | |
| 35 | const getStore = () => { |
| 36 | return lodash.cloneDeep(store); |
| 37 | }; |
| 38 | |
| 39 | // 视频发布所有组件的共享状态和方法 |
| 40 | export const useBellMessageStroe = create( |
| 41 | combine( |
| 42 | { |
| 43 | ...getStore(), |
| 44 | }, |
| 45 | (set, get, storeApi) => { |
| 46 | const methods = { |
| 47 | // 添加数据 |
| 48 | addNotice(type: NoticeType, data: NoticeItem[]) { |
| 49 | const noticeMap = new Map(get().noticeMap); |
| 50 | noticeMap.set(type, data); |
| 51 | |
| 52 | set({ noticeMap }); |
| 53 | }, |
| 54 | |
| 55 | // 发布进度监听 |
| 56 | videoPublishProgressInit() { |
| 57 | onVideoPublishProgress((progressData) => { |
| 58 | const noticeMap = new Map(get().noticeMap); |
| 59 | const noticeList = noticeMap.get(NoticeType.PubNotice) || []; |
| 60 | const noticeItem = noticeList.find((v) => v.id === progressData.id); |
| 61 | |
| 62 | noticeItem?.pub?.progressList.find((v, i) => { |
| 63 | if (v.account.id === progressData.account.id) { |
| 64 | noticeItem!.pub!.progressList[i] = progressData; |
| 65 | return true; |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | set({ noticeMap }); |
| 70 | }); |
| 71 | }, |
| 72 | }; |
| 73 | return methods; |
| 74 | }, |
| 75 | ), |
| 76 | ); |
| 77 |