|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import { useRef, useState, useEffect } from 'react';
|
|
|
|
|
import { useRef, useState, useEffect, } from 'react';
|
|
|
|
|
import useConversationStore from '@/stores/ConversationStore';
|
|
|
|
|
import { useShallow } from 'zustand/react/shallow';
|
|
|
|
|
import { Image, Modal, Button } from 'antd';
|
|
|
|
@ -11,10 +11,13 @@ import ConversationNewItem from './ConversationsNewItem';
|
|
|
|
|
import EmailEditorPopup from './Input/EmailEditorPopup';
|
|
|
|
|
import EmailDetail from './Components/EmailDetail';
|
|
|
|
|
import { useOrderStore, } from "@/stores/OrderStore";
|
|
|
|
|
import { isEmpty } from '@/utils/commons';
|
|
|
|
|
import useStyleStore from '@/stores/StyleStore';
|
|
|
|
|
|
|
|
|
|
const MessagesWrapper = ({ updateRead = true, forceGetMessages }) => {
|
|
|
|
|
const userId = useAuthStore((state) => state.loginUser.userId);
|
|
|
|
|
|
|
|
|
|
const [mobile] = useStyleStore(state => [state.mobile]);
|
|
|
|
|
const [currentConversation, updateCurrentConversation, setCurrentConversation] = useConversationStore((state) => [state.currentConversation, state.updateCurrentConversation, state.setCurrentConversation]);
|
|
|
|
|
// const conversationsList = useConversationStore((state) => state.conversationsList);
|
|
|
|
|
const activeMessages = useConversationStore(useShallow((state) => (state.currentConversation.sn && state.activeConversations[state.currentConversation.sn] ? state.activeConversations[state.currentConversation.sn]: [])));
|
|
|
|
@ -35,26 +38,45 @@ const MessagesWrapper = ({ updateRead = true, forceGetMessages }) => {
|
|
|
|
|
const [longListLoading, setLongListLoading] = useState(false);
|
|
|
|
|
const [shouldScrollBottom, setShouldScrollBottom] = useState(true);
|
|
|
|
|
|
|
|
|
|
const prevSN = useRef(currentConversationSN);
|
|
|
|
|
const prevColiSN = useRef(currentConversationColiSN);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
setCurrentConversation({})
|
|
|
|
|
if (!mobile) {
|
|
|
|
|
prevSN.current = currentConversationSN
|
|
|
|
|
prevColiSN.current = currentConversationColiSN
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// console.log('effect sn', currentConversationSN);
|
|
|
|
|
// console.log('effect sn', currentConversationSN, prevSN);
|
|
|
|
|
// if (isEmpty(prevSN) || currentConversationSN === prevSN) {
|
|
|
|
|
// return () => {};
|
|
|
|
|
// }
|
|
|
|
|
// console.log('effect sn', currentConversationSN, prevSN.current, 'llllllllllllllllll');
|
|
|
|
|
// console.log('if fetch', (activeMessages.length))
|
|
|
|
|
|
|
|
|
|
if (currentConversationSN && (activeMessages.length < 20 || forceGetMessages !== undefined)) {
|
|
|
|
|
if (currentConversationSN && (mobile || prevSN.current !== currentConversationSN) && (activeMessages.length < 20 || forceGetMessages !== undefined)) {
|
|
|
|
|
// console.log('effect sn NOT empty GO fetch', currentConversationSN);
|
|
|
|
|
getFirstPageMessages(currentConversation);
|
|
|
|
|
} else if (currentConversationSN) {
|
|
|
|
|
// console.log('--------------------- set ref longlist', currentConversationSN, currentConversationColiSN)
|
|
|
|
|
prevSN.current = currentConversationSN;
|
|
|
|
|
prevColiSN.current = currentConversationColiSN;
|
|
|
|
|
}
|
|
|
|
|
setShouldScrollBottom(true);
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [currentConversationSN]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// console.log('effect coli_sn', currentConversationSN);
|
|
|
|
|
// console.log('effect coli_sn', currentConversationSN, currentConversationColiSN, prevColiSN.current, 'xxxxxxxxxxxx');
|
|
|
|
|
// console.log('effect coli_sn After if', currentConversationSN, currentConversationNeedLoaded);
|
|
|
|
|
|
|
|
|
|
if (currentConversation.sn) {
|
|
|
|
|
if (currentConversation.sn && (mobile || prevColiSN.current !== currentConversationColiSN)) {
|
|
|
|
|
// console.log('effect coli_sn NOT empty GO fetch', currentConversationSN);
|
|
|
|
|
getFirstPageMessages({...currentConversation, });
|
|
|
|
|
}
|
|
|
|
|
setShouldScrollBottom(true);
|
|
|
|
@ -65,7 +87,7 @@ const MessagesWrapper = ({ updateRead = true, forceGetMessages }) => {
|
|
|
|
|
setLongList(activeMessages);
|
|
|
|
|
const thisLastTime = activeMessages.length > 0 ? activeMessages[0].msgtime : '';
|
|
|
|
|
const loadNextPage = !(activeMessages.length === 0 || activeMessages.length < MESSAGE_PAGE_SIZE);
|
|
|
|
|
updateCurrentConversation({ lasttime: thisLastTime, loadNextPage });
|
|
|
|
|
updateCurrentConversation({ lasttime: thisLastTime, loadNextPage, });
|
|
|
|
|
return () => {};
|
|
|
|
|
}, [activeMessages]);
|
|
|
|
|
|
|
|
|
@ -88,14 +110,18 @@ const MessagesWrapper = ({ updateRead = true, forceGetMessages }) => {
|
|
|
|
|
setMsgLoading(true);
|
|
|
|
|
const data = await fetchMessages({
|
|
|
|
|
conversationid: item.sn,
|
|
|
|
|
opisn: forceGetMessages ? currentConversation.opi_sn || '' : userId,
|
|
|
|
|
opisn: forceGetMessages ? item.opi_sn || '' : userId,
|
|
|
|
|
whatsappid: item.whatsapp_phone_number,
|
|
|
|
|
lasttime: '',
|
|
|
|
|
coli_sn: currentConversation.coli_sn || '',
|
|
|
|
|
coli_sn: item.coli_sn || '',
|
|
|
|
|
})
|
|
|
|
|
prevSN.current = item.sn;
|
|
|
|
|
prevColiSN.current = item.coli_sn;
|
|
|
|
|
|
|
|
|
|
const thisLastTime = data.length > 0 ? data[0].msgtime : ''; // orgmsgtime
|
|
|
|
|
const loadNextPage = !(data.length === 0 || data.length < MESSAGE_PAGE_SIZE);
|
|
|
|
|
updateCurrentConversation({ lasttime: thisLastTime, loadNextPage });
|
|
|
|
|
updateCurrentConversation({ lasttime: thisLastTime, loadNextPage, });
|
|
|
|
|
// setPrevSN(item.sn);
|
|
|
|
|
|
|
|
|
|
setMsgLoading(false);
|
|
|
|
|
receivedMessageList(item.sn, data);
|
|
|
|
@ -116,7 +142,7 @@ const MessagesWrapper = ({ updateRead = true, forceGetMessages }) => {
|
|
|
|
|
setLongList(prevValue => data.concat(prevValue));
|
|
|
|
|
const thisLastTime = data.length > 0 ? data[0].msgtime : '';
|
|
|
|
|
const loadNextPage = !(data.length === 0 || data.length < MESSAGE_PAGE_SIZE);
|
|
|
|
|
updateCurrentConversation({ lasttime: thisLastTime, loadNextPage });
|
|
|
|
|
updateCurrentConversation({ lasttime: thisLastTime, loadNextPage, });
|
|
|
|
|
return data.length;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|