Messages list loading

dev/chat
Lei OT 2 years ago
parent ac09cce4a8
commit e769555e3d

@ -26,7 +26,6 @@ const initialConversationState = {
// referenceMsg: {}, // referenceMsg: {},
}; };
olog('initialConversationState');
export const templatesSlice = (set) => ({ export const templatesSlice = (set) => ({
templates: [], templates: [],
@ -167,8 +166,11 @@ export const conversationSlice = (set, get) => ({
}); });
export const messageSlice = (set, get) => ({ export const messageSlice = (set, get) => ({
msgListLoading: false,
activeConversations: {}, activeConversations: {},
setMsgLoading: (msgListLoading) => set({ msgListLoading }),
receivedMessageList: (conversationid, msgList) => set((state) => ({ receivedMessageList: (conversationid, msgList) => set((state) => ({
msgListLoading: false,
activeConversations: { ...state.activeConversations, [String(conversationid)]: msgList } activeConversations: { ...state.activeConversations, [String(conversationid)]: msgList }
})), })),
updateMessageItem: (message) => { // msgUpdate updateMessageItem: (message) => { // msgUpdate
@ -253,7 +255,6 @@ export const useConversationStore = create(devtools((set, get) => ({
// side effects // side effects
fetchInitialData: async (userId) => { fetchInitialData: async (userId) => {
olog('fetch init');
const { addToConversationList, setTemplates } = get(); const { addToConversationList, setTemplates } = get();
const conversationsList = await fetchConversationsList({ opisn: userId }); const conversationsList = await fetchConversationsList({ opisn: userId });
@ -263,5 +264,5 @@ export const useConversationStore = create(devtools((set, get) => ({
setTemplates(templates); setTemplates(templates);
}, },
}))); })));
// window.store = useConversationStore; // debug:
export default useConversationStore; export default useConversationStore;

@ -46,7 +46,7 @@ const Conversations = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const { loginUser } = useAuthContext(); const { loginUser } = useAuthContext();
const { userId } = loginUser; const { userId } = loginUser;
const { activeConversations, currentConversation, conversationsList, addToConversationList, setCurrentConversation, receivedMessageList, } = useConversationStore(); const { activeConversations, currentConversation, conversationsList, addToConversationList, setCurrentConversation, receivedMessageList, setMsgLoading } = useConversationStore();
const [chatlist, setChatlist] = useState([]); const [chatlist, setChatlist] = useState([]);
useEffect(() => { useEffect(() => {
setChatlist( setChatlist(
@ -97,11 +97,15 @@ const Conversations = () => {
return false; return false;
} }
}; };
const getMessages = async (item) => {
setMsgLoading(true);
const data = await fetchMessages({ opisn: userId, whatsappid: item.whatsapp_phone_number });
receivedMessageList(item.sn, data);
};
const switchConversation = async (item) => { const switchConversation = async (item) => {
const messagesList = activeConversations[`${item.sn}`] || []; const messagesList = activeConversations[`${item.sn}`] || [];
if (messagesList.length < 20) { if (messagesList.length < 20) {
const data = await fetchMessages({ opisn: userId, whatsappid: item.whatsapp_phone_number }); await getMessages(item);
receivedMessageList(item.sn, data);
} }
if (String(item.sn) === String(currentConversation.sn)) { if (String(item.sn) === String(currentConversation.sn)) {
return false; return false;

@ -1,11 +1,11 @@
import { useEffect, useState, useRef, useMemo } from 'react'; import { useEffect, useState, useRef, useMemo } from 'react';
import { Image, Alert } from 'antd'; import { Image, Spin } from 'antd';
import { MessageBox } from 'react-chat-elements'; import { MessageBox } from 'react-chat-elements';
import useConversationStore from '@/stores/ConversationStore'; import useConversationStore from '@/stores/ConversationStore';
import { useShallow } from 'zustand/react/shallow'; import { useShallow } from 'zustand/react/shallow';
const Messages = () => { const Messages = () => {
const { currentConversation, setReferenceMsg } = useConversationStore(); const { currentConversation, setReferenceMsg, msgListLoading, } = useConversationStore();
const activeMessages = useConversationStore(useShallow((state) => (currentConversation.sn ? state.activeConversations[currentConversation.sn] : []))); const activeMessages = useConversationStore(useShallow((state) => (currentConversation.sn ? state.activeConversations[currentConversation.sn] : [])));
const messagesList = useMemo( const messagesList = useMemo(
@ -50,11 +50,13 @@ const Messages = () => {
return ( return (
<div> <div>
<Spin spinning={msgListLoading} tip={'正在读取...'} wrapperClassName='pt-8 ' >
{messagesList.map((message, index) => ( {messagesList.map((message, index) => (
<MessageBox key={message.key} {...message} onReplyClick={() => setReferenceMsg(message)} onOpen={() => handlePreview(message)} /> <MessageBox key={message.key} {...message} onReplyClick={() => setReferenceMsg(message)} onOpen={() => handlePreview(message)} />
))} ))}
<Image src={previewSrc} preview={{ visible: previewVisible, src: previewSrc, onClose: onPreviewClose }} /> <Image src={previewSrc} preview={{ visible: previewVisible, src: previewSrc, onClose: onPreviewClose }} />
<div ref={messagesEndRef}></div> <div ref={messagesEndRef}></div>
</Spin>
</div> </div>
); );
}; };

Loading…
Cancel
Save