| 1 | import { create } from 'zustand'; |
| 2 | import { combine } from 'zustand/middleware'; |
| 3 | import lodash from 'lodash'; |
| 4 | |
| 5 | interface IAICreateTitleStore { |
| 6 | /** |
| 7 | * 视频列表 |
| 8 | * key=视频名称+视频size,value=oss地址 |
| 9 | */ |
| 10 | aiCreateVideoMap: Map<string, string>; |
| 11 | } |
| 12 | |
| 13 | const store: IAICreateTitleStore = { |
| 14 | aiCreateVideoMap: new Map([]), |
| 15 | }; |
| 16 | |
| 17 | const getStore = () => { |
| 18 | return lodash.cloneDeep(store); |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * 这个store是为了记录 [AICreateTitle.tsx] 组件上传的视频 |
| 23 | * 防止同一个视频被上传多次 |
| 24 | * 注意要在引用该组件的页面销毁后调用这个store的clear方法 |
| 25 | */ |
| 26 | export const useAICreateTitleStore = create( |
| 27 | combine( |
| 28 | { |
| 29 | ...store, |
| 30 | }, |
| 31 | (set, get, storeApi) => { |
| 32 | const methods = { |
| 33 | clear() { |
| 34 | set({ |
| 35 | ...getStore(), |
| 36 | }); |
| 37 | }, |
| 38 | |
| 39 | getVideo(key: string) { |
| 40 | return get().aiCreateVideoMap.get(key) || ''; |
| 41 | }, |
| 42 | |
| 43 | setVideo(key: string, value: string) { |
| 44 | const aiCreateVideoMap = new Map(get().aiCreateVideoMap); |
| 45 | aiCreateVideoMap.set(key, value); |
| 46 | |
| 47 | set({ |
| 48 | aiCreateVideoMap, |
| 49 | }); |
| 50 | }, |
| 51 | }; |
| 52 | return methods; |
| 53 | }, |
| 54 | ), |
| 55 | ); |
| 56 |