|
|
|
@ -1,12 +1,13 @@
|
|
|
|
|
import React, { useEffect, useState, useRef, useCallback } from 'react';
|
|
|
|
|
import { useParams, useNavigate, useLocation } from 'react-router-dom';
|
|
|
|
|
import { Input, Button, Empty, Tooltip, List } from 'antd';
|
|
|
|
|
import { App, Input, Button, Empty, Tooltip, List } from 'antd';
|
|
|
|
|
import { PlusOutlined, LoadingOutlined, HistoryOutlined, FireOutlined,AudioTwoTone } from '@ant-design/icons';
|
|
|
|
|
import { fetchConversationsList, fetchOrderConversationsList, CONVERSATION_PAGE_SIZE } from '@/actions/ConversationActions';
|
|
|
|
|
import ConversationsNewItem from './ConversationsNewItem';
|
|
|
|
|
import { debounce, flush, isEmpty, isNotEmpty, pick } from '@/utils/commons';
|
|
|
|
|
import useConversationStore from '@/stores/ConversationStore';
|
|
|
|
|
import useAuthStore from '@/stores/AuthStore';
|
|
|
|
|
import { useOrderStore, OrderLabelDefaultOptions, OrderStatusDefaultOptions } from "@/stores/OrderStore";
|
|
|
|
|
import { useVisibilityState } from '@/hooks/useVisibilityState';
|
|
|
|
|
import ChatListItem from './Components/ChatListItem';
|
|
|
|
|
import ChatListFilter from './Components/ChatListFilter';
|
|
|
|
@ -14,10 +15,12 @@ import useStyleStore from '@/stores/StyleStore';
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import { DATETIME_FORMAT } from '@/config';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* []
|
|
|
|
|
*/
|
|
|
|
|
const Conversations = () => {
|
|
|
|
|
const { message } = App.useApp();
|
|
|
|
|
const [mobile] = useStyleStore((state) => [state.mobile]);
|
|
|
|
|
const routerReplace = mobile === false ? true : false; // : true;
|
|
|
|
|
const routePrefix = mobile === false ? `/order/chat` : `/m/chat`;
|
|
|
|
@ -26,8 +29,12 @@ const Conversations = () => {
|
|
|
|
|
const { order_sn } = useParams();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const userId = useAuthStore((state) => state.loginUser.userId);
|
|
|
|
|
|
|
|
|
|
const initialState = useConversationStore((state) => state.initialState);
|
|
|
|
|
const [currentConversation, setCurrentConversation] = useConversationStore((state) => [state.currentConversation, state.setCurrentConversation]);
|
|
|
|
|
|
|
|
|
|
const [customerDetail] = useOrderStore((s) => [s.customerDetail])
|
|
|
|
|
|
|
|
|
|
const [currentConversation, setCurrentConversation, updateCurrentConversation] = useConversationStore((state) => [state.currentConversation, state.setCurrentConversation, state.updateCurrentConversation]);
|
|
|
|
|
const [topList, pageList, conversationsList] = useConversationStore((state) => [state.topList, state.pageList, state.conversationsList]);
|
|
|
|
|
const [conversationsListLoading, setConversationsListLoading] = useConversationStore((state) => [state.conversationsListLoading, state.setConversationsListLoading]);
|
|
|
|
|
const addToConversationList = useConversationStore((state) => state.addToConversationList);
|
|
|
|
@ -35,16 +42,20 @@ const Conversations = () => {
|
|
|
|
|
|
|
|
|
|
const isVisible = useVisibilityState();
|
|
|
|
|
|
|
|
|
|
const [tabSelectedConversation, setTabSelectedConversation] = useState({});
|
|
|
|
|
const [tabCnt, setTabCnt] = useState(-1);
|
|
|
|
|
|
|
|
|
|
const [{ search: searchContent, loadNextPage, ...filterState }, setSearchContent, setFilter] = useConversationStore((state) => [state.filter, state.setFilterSearch, state.setFilter])
|
|
|
|
|
|
|
|
|
|
const [activeList, setActiveList] = useState(true);
|
|
|
|
|
const [currentLoading, setCurrentLoading] = useState(false);
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
let refreshing = false;
|
|
|
|
|
async function refreshConversationList(current='', append=false) {
|
|
|
|
|
// console.log('invoke -- refreshConversationList', current, conversationsListLoading, refreshing);
|
|
|
|
|
if (refreshing !== false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
refreshing = true;
|
|
|
|
|
// setConversationsListLoading(mobile !== undefined ? true : false);
|
|
|
|
|
setConversationsListLoading(true);
|
|
|
|
|
setCurrentLoading(current==='');
|
|
|
|
@ -67,6 +78,7 @@ const Conversations = () => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const _list = await fetchConversationsList({ ...searchParams, opisn: userId });
|
|
|
|
|
// const _list = DebounceFetchList({ ...searchParams, opisn: userId });
|
|
|
|
|
|
|
|
|
|
// 搜索时, 清空列表
|
|
|
|
|
if (current) {
|
|
|
|
@ -86,18 +98,27 @@ const Conversations = () => {
|
|
|
|
|
// } : {}),
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
refreshing = false;
|
|
|
|
|
setConversationsListLoading(false);
|
|
|
|
|
setCurrentLoading(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (mobile !== undefined) {
|
|
|
|
|
setCurrentConversation({});
|
|
|
|
|
setCurrentConversation({ });
|
|
|
|
|
}
|
|
|
|
|
return () => {};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const [activeList, setActiveList] = useState(true);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (order_sn && isEmpty(currentConversation.sn)) {
|
|
|
|
|
setCurrentConversation({ coli_sn: order_sn })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {}
|
|
|
|
|
}, [order_sn])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isVisible && initialState) {
|
|
|
|
@ -117,6 +138,10 @@ const Conversations = () => {
|
|
|
|
|
|
|
|
|
|
const [dataSource, setDataSource] = useState(conversationsList);
|
|
|
|
|
const [listUpdateFlag, setListUpdateFlag] = useState();
|
|
|
|
|
|
|
|
|
|
const [switchToC, setSwitchToC] = useState({});
|
|
|
|
|
const [shouldFetchCList, setShouldFetchCList] = useState(true);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setDataSource([...topList, ...pageList]);
|
|
|
|
|
// setDataSource(activeList ? conversationsList: closedConversationsList);
|
|
|
|
@ -129,45 +154,50 @@ const Conversations = () => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {}
|
|
|
|
|
}, [conversationsList, initialState])
|
|
|
|
|
}, [order_sn, initialState, customerDetail.email])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const [switchToC, setSwitchToC] = useState({});
|
|
|
|
|
const [shouldFetchCList, setShouldFetchCList] = useState(true);
|
|
|
|
|
// useEffect(() => {
|
|
|
|
|
// console.log('shouldFetchCList', order_sn , shouldFetchCList , initialState);
|
|
|
|
|
const getOrderConversationList = async (colisn) => {
|
|
|
|
|
|
|
|
|
|
// if (order_sn && shouldFetchCList && initialState) {
|
|
|
|
|
// getOrderConversationList(order_sn);
|
|
|
|
|
// }
|
|
|
|
|
const { whatsapp_phone_number } = switchToC;
|
|
|
|
|
const whatsappID = coli_guest_WhatsApp || whatsapp_phone_number || '';
|
|
|
|
|
// if (isEmpty(conversationsList)) {
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return () => {};
|
|
|
|
|
// }, [order_sn, shouldFetchCList, initialState]);
|
|
|
|
|
let findCurrentOrderChats;
|
|
|
|
|
let findCurrentIndex;
|
|
|
|
|
let findCurrent;
|
|
|
|
|
|
|
|
|
|
const getOrderConversationList = async (colisn, WHATSAPP_ID = null) => {
|
|
|
|
|
const { whatsapp_phone_number } = switchToC;
|
|
|
|
|
const whatsappID = WHATSAPP_ID || coli_guest_WhatsApp || whatsapp_phone_number || '';
|
|
|
|
|
// let findCurrentOrderChats = conversationsList.filter((item) => `${item.coli_sn}` === `${colisn}`);
|
|
|
|
|
// 使用opisn + whatsappID 判断, 解决订单修改whatsappID号码之后获取新会话, 登录账号此处省略
|
|
|
|
|
if (isEmpty(conversationsList)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let findCurrentOrderChats = conversationsList.filter((item) => `${item.whatsapp_phone_number}` === `${whatsappID}` && `${item.coli_sn}` === `${colisn}`)
|
|
|
|
|
let findCurrentIndex = isEmpty(findCurrentOrderChats) ? -1 : 0; // findCurrentOrderChats.length-1;
|
|
|
|
|
let findCurrent = findCurrentOrderChats[findCurrentIndex];
|
|
|
|
|
if (findCurrentIndex !== -1) {
|
|
|
|
|
addToConversationList(findCurrentOrderChats);
|
|
|
|
|
} else if (!isEmpty(whatsappID)) {
|
|
|
|
|
const data = await fetchOrderConversationsList({ opisn: userId, colisn: colisn, whatsappid: whatsappID });
|
|
|
|
|
if (!isEmpty(data)) {
|
|
|
|
|
addToConversationList(data);
|
|
|
|
|
findCurrentIndex = 0; // data.length-1; // data.lastIndexOf((item) => item.coli_sn === Number(colisn));
|
|
|
|
|
findCurrent = data[findCurrentIndex];
|
|
|
|
|
} else {
|
|
|
|
|
// findCurrentIndex = conversationsList.findIndex((item) => item.coli_sn === Number(colisn)); // data.findIndex((item) => item.sn === currentConversation.sn);
|
|
|
|
|
if (!isEmpty(whatsappID) || !isEmpty(customerDetail.email)) {
|
|
|
|
|
|
|
|
|
|
findCurrentOrderChats = conversationsList.filter(
|
|
|
|
|
(item) => `${item.coli_sn}` === `${colisn}` && (`${item.whatsapp_phone_number}` === `${whatsappID}` || `${item.channels?.email}` === `${customerDetail.email}`)
|
|
|
|
|
)
|
|
|
|
|
findCurrentIndex = isEmpty(findCurrentOrderChats) ? -1 : 0; // findCurrentOrderChats.length-1;
|
|
|
|
|
findCurrent = findCurrentOrderChats[findCurrentIndex];
|
|
|
|
|
|
|
|
|
|
if (findCurrentIndex !== -1) {
|
|
|
|
|
addToConversationList(findCurrentOrderChats);
|
|
|
|
|
} else // if (!isEmpty(whatsappID))
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
const data = await fetchOrderConversationsList({ opisn: userId, colisn: colisn, whatsappid: whatsappID, email: customerDetail.email });
|
|
|
|
|
if (!isEmpty(data)) {
|
|
|
|
|
addToConversationList(data);
|
|
|
|
|
findCurrentIndex = 0; // data.length-1; // data.lastIndexOf((item) => item.coli_sn === Number(colisn));
|
|
|
|
|
findCurrent = data[findCurrentIndex];
|
|
|
|
|
} else {
|
|
|
|
|
// findCurrentIndex = conversationsList.findIndex((item) => item.coli_sn === Number(colisn)); // data.findIndex((item) => item.sn === currentConversation.sn);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
// message.warning('获取订单会话失败')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (isEmpty(whatsappID)) {
|
|
|
|
|
}
|
|
|
|
|
else if (isEmpty(whatsappID)) {
|
|
|
|
|
// 刷新页面
|
|
|
|
|
findCurrentIndex = conversationsList.findIndex((item) => `${item.coli_sn}` === `${colisn}`);
|
|
|
|
|
findCurrent = conversationsList[findCurrentIndex];
|
|
|
|
@ -306,7 +336,6 @@ const Conversations = () => {
|
|
|
|
|
refreshConversationList,
|
|
|
|
|
setListUpdateFlag,
|
|
|
|
|
onSwitchConversation,
|
|
|
|
|
tabSelectedConversation,
|
|
|
|
|
setNewChatModalVisible,
|
|
|
|
|
setEditingChat,
|
|
|
|
|
}}
|
|
|
|
|