import { useState, useEffect, useCallback } from 'react' import { isEmpty, readIndexDB, } from '@/utils/commons' import { getEmailDetailAction, postResendEmailAction, getSalesSignatureAction, getEmailOrderAction, queryEmailListAction } from '@/actions/EmailActions' import { App } from 'antd' import useConversationStore from '@/stores/ConversationStore'; import { msgStatusRenderMapped } from '@/channel/bubbleMsgUtils'; import { POPUP_FEATURES } from '@/config'; export const useEmailSignature = (opi_sn) => { const [signature, setSignature] = useState('') const getSignature = useCallback(async () => { if (isEmpty(opi_sn)) { return false } try { const data = await getSalesSignatureAction({ opi_sn }) setSignature(data) } catch (err) { console.error(err) } }, [opi_sn]) useEffect(() => { getSignature() }, [opi_sn]) return { signature, getSignature } } /** * @param mai_sn 邮件编号ID * @param data 直接传递, 不重复获取 * * 在详情点击`回复`呼出编辑时 */ export const useEmailDetail = (mai_sn, data) => { const {notification} = App.useApp() const [loading, setLoading] = useState(false) const [mailData, setMailData] = useState({ loading, info: {}, content: '', attachments: [] }) const [coliSN, setColiSN] = useState(''); const [orderDetail, setOrderDetail] = useState({}); const [updateMessageItem] = useConversationStore(state => [state.updateMessageItem]); useEffect(() => { const getEmailDetail = async () => { if (isEmpty(mai_sn)) { return false } try { setLoading(true) const data = await getEmailDetailAction({ mai_sn }) // console.log(data) setMailData(data) setColiSN(data.info.MAI_COLI_SN) // if (!isEmpty(data.info.MAI_COLI_SN)) { // const orderData = await getEmailOrderAction({ colisn: data.info.MAI_COLI_SN }) // setOrderDetail(orderData) // // console.log(orderData) // } setLoading(false) } catch (err) { setLoading(false) notification.error({ message: "请求失败", description: err.message || '网络异常', placement: "top", duration: 3, }); } } if (isEmpty(data)) getEmailDetail() else setMailData(data) }, [mai_sn]) useEffect(() => { console.log(coliSN, '====colisn======') const getOrderDetail = async () => { if (isEmpty(coliSN)) { return false } try { setLoading(true) const data = await getEmailOrderAction({ colisn: coliSN }) setOrderDetail(data) setLoading(false) } catch (err) { setLoading(false) notification.error({ message: "请求失败", description: err.message || '网络异常', placement: "top", duration: 3, }); } } getOrderDetail() }, [coliSN]) const postEmailResend = async ({ mai_sn, conversationid: externalid, actionId: actionid, ...body }) => { if (isEmpty(mai_sn)) { return false } await postResendEmailAction({ mai_sn, externalid, actionid, token: 0 }) // 重发没有状态返回值, 此处前端处理为'待发送', todo: 刷新页面后消息仍为上一个状态'失败' updateMessageItem({ conversationid: externalid, actionid, id: actionid, status: msgStatusRenderMapped['accepted'] }) } return { loading, mailData, orderDetail, postEmailResend } } export const EmailBuilder = ({subject, content}) => { return `${content}`; } export const openPopup = (url, target,) => { window.open(url, target, POPUP_FEATURES); }; export const useEmailList = (mailboxDirNode) => { const [loading, setLoading] = useState(false) const [mailList, setMailList] = useState([]) const [error, setError] = useState(null) const [isFreshData, setIsFreshData] = useState(false) const [refreshTrigger, setRefreshTrigger] = useState(0); const refresh = useCallback(() => { setRefreshTrigger(prev => prev + 1); }, []); const { OPI_SN: opi_sn, COLI_SN, VKey, VParent, ApplyDate, OrderSourceType, IsTrue } = mailboxDirNode const getMailList = useCallback(async () => { console.log('getMailList', mailboxDirNode) if (!opi_sn || !VKey || (!IsTrue && !COLI_SN)) { setMailList([]) setLoading(false) setError(null) setIsFreshData(false) return } setLoading(true) setError(null) setIsFreshData(false) const cacheKey = isEmpty(COLI_SN) ? `dir-${VKey}` : `order-${VKey}` const readCache = await readIndexDB(cacheKey, 'maillist', 'mailbox') if (!isEmpty(readCache)) { const _x = readCache.data.map((ele) => ({ ...ele, key: ele.MAI_SN, })) setMailList(_x) } try { 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的结构 const _x = x.map((ele) => ({ ...ele, key: ele.MAI_SN, })) setMailList(_x) } catch (networkError) { setError(new Error(`Failed to get mail list: ${networkError.message}.`)) } finally { setLoading(false) } }, [VKey, refreshTrigger]) useEffect(() => { getMailList() }, [getMailList]) return { loading, isFreshData, error, mailList, refresh } }