| ¶Ô±ÈÐÂÎļþ |
| | |
| | | import type { ImManagerChannelApi } from '#/api/im/manager/channel' |
| | | |
| | | import { acceptHMRUpdate, defineStore } from 'pinia' |
| | | |
| | | import { getSimpleChannelList } from '#/api/im/manager/channel' |
| | | |
| | | import { ImConversationType } from '../../utils/constants' |
| | | import { getDb } from '../../utils/db' |
| | | import { useConversationStore } from './conversationStore' |
| | | |
| | | /** |
| | | * IM é¢é Store |
| | | * |
| | | * è´è´£ï¼ç¼åå½åç¨æ·è½çå°çé¢éç²¾ç®å表ï¼å« id / code / name / avatarï¼ï¼ |
| | | * ä¾ä¼è¯å表 / å¡çæ¸²ææ¶åæ¥é¢éåç§° + 头åï¼é¿å
æ¾ç¤ºãé¢é 1ãè¿ç§å ä½ |
| | | */ |
| | | export const useChannelStore = defineStore('imChannelStore', { |
| | | state: () => ({ |
| | | channels: [] as ImManagerChannelApi.Channel[], |
| | | loaded: false |
| | | }), |
| | | |
| | | getters: { |
| | | getChannel(state): (id: number) => ImManagerChannelApi.Channel | undefined { |
| | | return (id: number) => state.channels.find((c) => c.id === id) |
| | | } |
| | | }, |
| | | |
| | | actions: { |
| | | // ==================== æ¬å°ç¼å ==================== |
| | | |
| | | /** ä» IndexedDB æ¢å¤é¢éå表 */ |
| | | async loadChannelList(): Promise<boolean> { |
| | | try { |
| | | const cached = await getDb().getAll<ImManagerChannelApi.Channel>('channels') |
| | | if (!cached || cached.length === 0) { |
| | | return false |
| | | } |
| | | this.channels = cached |
| | | return true |
| | | } catch (error) { |
| | | console.warn('[IM channelStore] æ¬å°é¢éç¼å读å失败', error) |
| | | return false |
| | | } |
| | | }, |
| | | |
| | | /** ä¿åé¢éå表 */ |
| | | saveChannelList(): void { |
| | | void getDb() |
| | | .transaction(['channels'], 'readwrite', async (tx) => { |
| | | const db = getDb() |
| | | await db.clearStore('channels', tx) |
| | | for (const channel of this.channels) { |
| | | await db.put('channels', channel, tx) |
| | | } |
| | | }) |
| | | .catch((error) => console.warn('[IM channelStore] æ¬å°é¢éç¼ååå
¥å¤±è´¥', error)) |
| | | }, |
| | | |
| | | // ==================== è¿ç«¯æå ==================== |
| | | |
| | | /** æåå¯ç¨çé¢éç²¾ç®åè¡¨ï¼æåååå¡«ä¼è¯å表已æçé¢é name / avatarï¼è¦ç IDB æ§å ä½ */ |
| | | async fetchChannelList(force = false) { |
| | | if (this.loaded && !force) { |
| | | return |
| | | } |
| | | try { |
| | | this.channels = (await getSimpleChannelList()) || [] |
| | | this.loaded = true |
| | | this.syncChannelConversationMetadata() |
| | | this.saveChannelList() |
| | | } catch (error) { |
| | | console.warn('[IM channelStore] fetchChannelList 失败', error) |
| | | } |
| | | }, |
| | | |
| | | /** ç¨ææ°çé¢éä¿¡æ¯è¦çå·²æ CHANNEL ä¼è¯ç name / avatar */ |
| | | syncChannelConversationMetadata() { |
| | | const conversationStore = useConversationStore() |
| | | const indexed = new Map(this.channels.map((c) => [c.id, c])) |
| | | conversationStore.conversations.forEach((conversation) => { |
| | | if (conversation.type !== ImConversationType.CHANNEL) { |
| | | return |
| | | } |
| | | const channel = indexed.get(conversation.targetId) |
| | | if (!channel) { |
| | | return |
| | | } |
| | | conversationStore.updateConversation(ImConversationType.CHANNEL, conversation.targetId, { |
| | | name: channel.name, |
| | | avatar: channel.avatar |
| | | }) |
| | | }) |
| | | }, |
| | | |
| | | /** æ¸
空é¢éå
å */ |
| | | clear() { |
| | | this.channels = [] |
| | | this.loaded = false |
| | | } |
| | | } |
| | | }) |
| | | |
| | | if (import.meta.hot) { |
| | | import.meta.hot.accept(acceptHMRUpdate(useChannelStore, import.meta.hot)) |
| | | } |
| | | |
| | | export const useChannelStoreWithOut = () => useChannelStore() |