import { getEmailDirAction, getMailboxCountAction, getRootMailboxDirAction } from '@/actions/EmailActions' import { buildTree, isEmpty, olog, sortArrayByOrder } from '@/utils/commons' import { readIndexDB, writeIndexDB, createIndexedDBStore, clean7DaysMailboxLog } from '@/utils/indexedDB'; /** * Email */ const emailSlice = (set, get) => ({ emailMsg: { id: -1, conversationid: '', actionId: '', order_opi: '', coli_sn: '', msgOrigin: {} }, setEmailMsg: (emailMsg) => { const { editorOpen } = get() return editorOpen ? false : set({ emailMsg }) // 已经打开的不更新 }, detailPopupOpen: false, setDetailOpen: (v) => set({ detailPopupOpen: v }), openDetail: () => set(() => ({ detailPopupOpen: true })), closeDetail: () => set(() => ({ detailPopupOpen: false })), editorOpen: false, setEditorOpen: (v) => set({ editorOpen: v }), openEditor: () => set(() => ({ editorOpen: true })), closeEditor: () => set(() => ({ editorOpen: false })), // EmailEditorPopup 组件的 props // @property {string} fromEmail - 发件人邮箱 // @property {string} fromUser - 发件人用户 // @property {string} fromOrder - 发件订单 // @property {string} toEmail - 收件人邮箱 // @property {string} conversationid - 会话ID // @property {string} quoteid - 引用邮件ID // @property {object} draft - 草稿 // @property {string} action - reply / forward / new / edit // @property {string} oid - coli_sn // @property {object} mailData - 邮件内容 // @property {string} receiverName - 收件人称呼 emailEdiorProps: new Map(), setEditorProps: (v) => { const { emailEdiorProps } = get() const uniqueKey = v.quoteid || Date.now().toString(32) const currentEditValue = { ...v, key: `${v.action}-${uniqueKey}` } const news = new Map(emailEdiorProps).set(currentEditValue.key, currentEditValue) for (const [key, value] of news.entries()) { console.log(value) } return set((state) => ({ emailEdiorProps: news, currentEditKey: currentEditValue.key, currentEditValue })) // return set((state) => ({ emailEdiorProps: { ...state.emailEdiorProps, ...v } })) }, closeEditor1: (key) => { const { emailEdiorProps } = get() const newProps = new Map(emailEdiorProps) newProps.delete(key) return set(() => ({ emailEdiorProps: newProps })) }, clearEditor: () => { return set(() => ({ emailEdiorProps: new Map() })) }, currentEditKey: '', setCurrentEditKey: (key) => { const { emailEdiorProps, setCurrentEditValue } = get() const value = emailEdiorProps.get(key) setCurrentEditValue(value) return set(() => ({ currentEditKey: key })) }, currentEditValue: {}, setCurrentEditValue: (v) => { return set(() => ({ currentEditValue: v })) }, // mailboxNestedDirs: new Map(), // setMailboxNestedDirs: (opi, dirs) => { // const { mailboxNestedDirs } = get() // const news = mailboxNestedDirs.set(opi, dirs) // return set(() => ({ mailboxNestedDirs: news })) // }, currentMailboxDEI: 0, setCurrentMailboxDEI: (id) => { return set(() => ({ currentMailboxDEI: id })) }, currentMailboxOPI: 0, setCurrentMailboxOPI: (id) => { return set(() => ({ currentMailboxOPI: id })) }, mailboxNestedDirsActive: [], setMailboxNestedDirsActive: (dir) => { return set(() => ({ mailboxNestedDirsActive: dir })) }, updateCurrentMailboxNestedDirs: (dirs) => { const { mailboxNestedDirsActive } = get() const _Map = new Map(mailboxNestedDirsActive.map((obj) => [obj.key, obj])) dirs.forEach((row) => { _Map.set(row.key, row) }) // const _newValue = sortArrayByOrder(Array.from(_Map.values()), 'key', ['search-orders']) const _newValue = Array.from(_Map.values()) return set(() => ({ mailboxNestedDirsActive: _newValue })) }, mailboxActiveNode: {}, setMailboxActiveNode: (node) => { return set(() => ({ mailboxActiveNode: node })) }, mailboxList: [], setMailboxList: (list) => { return set(() => ({ mailboxList: list })) }, mailboxActiveMAI: 0, setMailboxActiveMAI: (mai) => { return set(() => ({ mailboxActiveMAI: mai })) }, mailboxActiveCOLI: 0, setMailboxActiveCOLI: (coli) => { return set(() => ({ mailboxActiveCOLI: coli })) }, getOPIEmailDir: async (opi_sn = 0, userIdStr = '', refreshNow = false) => { // console.log('🌐requesting opi dir', opi_sn, typeof opi_sn) const { setMailboxNestedDirsActive, updateMailboxCount } = get() const readCache = await readIndexDB(Number(opi_sn), 'dirs', 'mailbox') // console.log(readCache); let isNeedRefresh = refreshNow if (!isEmpty(readCache)) { setMailboxNestedDirsActive(readCache?.tree || []) isNeedRefresh = refreshNow || Date.now() - readCache.timestamp > 1 * 60 * 60 * 1000 // isNeedRefresh = true; // test: 0 } if (isEmpty(readCache) || isNeedRefresh) { // > {4} 更新 const rootTree = await getRootMailboxDirAction({ opi_sn, userIdStr: String(userIdStr || opi_sn) }) // console.log('empty', opi_sn, userIdStr, isEmpty(readCache), isNeedRefresh, rootTree); setMailboxNestedDirsActive(rootTree) } else { // 只更新数量 updateMailboxCount({ opi_sn }) } return false }, // 更新数量 updateMailboxCount: async ({ opi_sn }) => { const { setMailboxNestedDirsActive } = get() await getMailboxCountAction({ opi_sn }, true) const readCache = await readIndexDB(Number(opi_sn), 'dirs', 'mailbox') if (!isEmpty(readCache)) { setMailboxNestedDirsActive(readCache?.tree || []) } }, async initMailbox({ opi_sn, dei_sn, userIdStr }) { olog('initMailbox ---- ') const { setCurrentMailboxOPI, setCurrentMailboxDEI, getOPIEmailDir } = get() createIndexedDBStore(['dirs', 'maillist', 'listrow', 'mailinfo', 'draft'], 'mailbox') setCurrentMailboxOPI(opi_sn) setCurrentMailboxDEI(dei_sn) getOPIEmailDir(opi_sn, userIdStr, true) }, }) export default emailSlice