|
|
|
@ -1,11 +1,12 @@
|
|
|
|
|
import { useState, useEffect, useCallback } from 'react'
|
|
|
|
|
import { isEmpty, objectMapper, olog, } from '@/utils/commons'
|
|
|
|
|
import { readIndexDB } from '@/utils/indexedDB'
|
|
|
|
|
import { getEmailDetailAction, postResendEmailAction, getSalesSignatureAction, getEmailOrderAction, queryEmailListAction, getEmailTemplateAction, saveEmailDraftOrSendAction, updateEmailAction } from '@/actions/EmailActions'
|
|
|
|
|
import { getEmailDetailAction, postResendEmailAction, getSalesSignatureAction, getEmailOrderAction, queryEmailListAction, getEmailTemplateAction, saveEmailDraftOrSendAction, updateEmailAction, getEmailChangesChannel, EMAIL_CHANNEL_NAME } from '@/actions/EmailActions'
|
|
|
|
|
import { App } from 'antd'
|
|
|
|
|
import useConversationStore from '@/stores/ConversationStore';
|
|
|
|
|
import { msgStatusRenderMapped } from '@/channel/bubbleMsgUtils';
|
|
|
|
|
import { POPUP_FEATURES } from '@/config';
|
|
|
|
|
import { internalEventEmitter } from '@/utils/EventEmitterService';
|
|
|
|
|
|
|
|
|
|
export const useEmailSignature = (opi_sn) => {
|
|
|
|
|
|
|
|
|
@ -41,7 +42,7 @@ export const useEmailSignature = (opi_sn) => {
|
|
|
|
|
* - If `number`: 直接传递, 直接获取订单详情
|
|
|
|
|
* - If `false`: 不需要获取订单信息
|
|
|
|
|
*/
|
|
|
|
|
export const useEmailDetail = (mai_sn, data={}, oid=0) => {
|
|
|
|
|
export const useEmailDetail = (mai_sn=0, data={}, oid=0) => {
|
|
|
|
|
const {notification} = App.useApp()
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
const [mailData, setMailData] = useState({ loading, info: { MAI_COLI_SN: 0 }, content: '', attachments: [], AttachList: [] })
|
|
|
|
@ -128,7 +129,9 @@ export const useEmailDetail = (mai_sn, data={}, oid=0) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id: savedID } = await saveEmailDraftOrSendAction(body, isDraft)
|
|
|
|
|
setMaiSN(savedID)
|
|
|
|
|
refresh()
|
|
|
|
|
if (isDraft) {
|
|
|
|
|
refresh()
|
|
|
|
|
}
|
|
|
|
|
return savedID
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
@ -151,31 +154,46 @@ export const useEmailList = (mailboxDirNode) => {
|
|
|
|
|
const [mailList, setMailList] = useState([])
|
|
|
|
|
const [error, setError] = useState(null)
|
|
|
|
|
const [isFreshData, setIsFreshData] = useState(false)
|
|
|
|
|
const [refreshTrigger, setRefreshTrigger] = useState(0);
|
|
|
|
|
const [refreshTrigger, setRefreshTrigger] = useState(0)
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(() => {
|
|
|
|
|
setRefreshTrigger(prev => prev + 1);
|
|
|
|
|
}, []);
|
|
|
|
|
setRefreshTrigger((prev) => prev + 1)
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const { OPI_SN: opi_sn, COLI_SN, VKey, VParent, ApplyDate, OrderSourceType, IsTrue } = mailboxDirNode
|
|
|
|
|
|
|
|
|
|
const markAsRead = useCallback((sn_list) => {
|
|
|
|
|
updateEmailAction({
|
|
|
|
|
opi_sn: opi_sn,
|
|
|
|
|
mai_sn_list: sn_list,
|
|
|
|
|
set: { read: 1}
|
|
|
|
|
});
|
|
|
|
|
refresh()
|
|
|
|
|
}, []);
|
|
|
|
|
const markAsRead = useCallback(
|
|
|
|
|
async (sn_list) => {
|
|
|
|
|
await updateEmailAction({
|
|
|
|
|
opi_sn: opi_sn,
|
|
|
|
|
mai_sn_list: sn_list,
|
|
|
|
|
set: { read: 1 },
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
[VKey],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const markAsProcessed = useCallback(
|
|
|
|
|
async (sn_list) => {
|
|
|
|
|
await updateEmailAction({
|
|
|
|
|
opi_sn: opi_sn,
|
|
|
|
|
mai_sn_list: sn_list,
|
|
|
|
|
set: { processed: 1 },
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
[VKey],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const markAsProcessed = useCallback((sn_list) => {
|
|
|
|
|
updateEmailAction({
|
|
|
|
|
opi_sn: opi_sn,
|
|
|
|
|
mai_sn_list: sn_list,
|
|
|
|
|
set: { processed: 1}
|
|
|
|
|
});
|
|
|
|
|
refresh()
|
|
|
|
|
}, []);
|
|
|
|
|
const loadMailListFromCache = useCallback(async (payload) => {
|
|
|
|
|
const cacheKey = isEmpty(COLI_SN) ? `dir-${VKey}` : `order-${VKey}`
|
|
|
|
|
const readCacheIDList = await readIndexDB(cacheKey, 'maillist', 'mailbox')
|
|
|
|
|
if (!isEmpty(readCacheIDList)) {
|
|
|
|
|
const readCacheListRowsMap = await readIndexDB(readCacheIDList.data, 'listrow', 'mailbox')
|
|
|
|
|
const _x = readCacheIDList.data.map((ele) => readCacheListRowsMap.get(ele).data || {})
|
|
|
|
|
setMailList(_x)
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}, [VKey])
|
|
|
|
|
|
|
|
|
|
const getMailList = useCallback(async () => {
|
|
|
|
|
console.log('getMailList', mailboxDirNode)
|
|
|
|
@ -191,15 +209,11 @@ export const useEmailList = (mailboxDirNode) => {
|
|
|
|
|
setError(null)
|
|
|
|
|
setIsFreshData(false)
|
|
|
|
|
|
|
|
|
|
const cacheKey = isEmpty(COLI_SN) ? `dir-${VKey}` : `order-${VKey}`
|
|
|
|
|
const readCacheIDList = await readIndexDB(cacheKey, 'maillist', 'mailbox')
|
|
|
|
|
if (!isEmpty(readCacheIDList)) {
|
|
|
|
|
const readCacheListRowsMap = await readIndexDB(readCacheIDList.data, 'listrow', 'mailbox')
|
|
|
|
|
const _x = readCacheIDList.data.map((ele) => readCacheListRowsMap.get(ele).data || {})
|
|
|
|
|
setMailList(_x)
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
// const cacheKey = isEmpty(COLI_SN) ? `dir-${VKey}` : `order-${VKey}`
|
|
|
|
|
try {
|
|
|
|
|
// 1. 先从缓存读取
|
|
|
|
|
await loadMailListFromCache();
|
|
|
|
|
// 2. 从接口获取最新的列表
|
|
|
|
|
const nodeParam = { coli_sn: COLI_SN, order_source_type: OrderSourceType, vkey: VKey, vparent: VParent, mai_senddate1: ApplyDate }
|
|
|
|
|
const x = await queryEmailListAction({ opi_sn, node: nodeParam })
|
|
|
|
|
// 配合List的结构
|
|
|
|
@ -217,6 +231,32 @@ export const useEmailList = (mailboxDirNode) => {
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getMailList()
|
|
|
|
|
// --- Setup Internal Event Listener ---
|
|
|
|
|
const handleInternalUpdate = (event) => {
|
|
|
|
|
console.log(`[useEmailList] Received internal event. `, event.detail)
|
|
|
|
|
if (event.detail && event.detail.type === 'listrow') {
|
|
|
|
|
loadMailListFromCache()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
internalEventEmitter.on(EMAIL_CHANNEL_NAME, handleInternalUpdate)
|
|
|
|
|
|
|
|
|
|
// --- Setup BroadcastChannel Listener ---
|
|
|
|
|
const channel = getEmailChangesChannel()
|
|
|
|
|
const handleMessage = (event) => {
|
|
|
|
|
console.log(`[useEmailList] Received channel event. `, event.data)
|
|
|
|
|
const cacheKey = isEmpty(COLI_SN) ? `dir-${VKey}` : `order-${VKey}`
|
|
|
|
|
if (event.data.type === 'listrow' && cacheKey === event.data.listKey) {
|
|
|
|
|
// cacheKey 不相同时, 不需要更新; 邮箱目录不相同
|
|
|
|
|
loadMailListFromCache(event.data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
channel.addEventListener('message', handleMessage)
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
return () => {
|
|
|
|
|
internalEventEmitter.off(EMAIL_CHANNEL_NAME, handleInternalUpdate)
|
|
|
|
|
channel.removeEventListener('message', handleMessage)
|
|
|
|
|
}
|
|
|
|
|
}, [getMailList])
|
|
|
|
|
|
|
|
|
|
return { loading, isFreshData, error, mailList, refresh, markAsRead, markAsProcessed }
|
|
|
|
|