减少re-render

dev/chat
Lei OT 2 years ago
parent 2b6cf70f48
commit 3290b2c161

@ -6,12 +6,7 @@ import InputBox from './Components/InputBox';
import ConversationsList from './Components/ConversationsList'; import ConversationsList from './Components/ConversationsList';
import CustomerProfile from './Components/CustomerProfile'; import CustomerProfile from './Components/CustomerProfile';
import { useConversationState, useConversationDispatch } from '@/stores/ConversationContext';
import { sentNewMessage } from '@/actions/ConversationActions';
import { sentMsgTypeMapped } from '@/lib/msgUtils';
import './Conversations.css'; import './Conversations.css';
import { useAuthContext } from '@/stores/AuthContext.js';
const { Sider, Content, Header, Footer } = Layout; const { Sider, Content, Header, Footer } = Layout;
@ -19,27 +14,14 @@ const { Sider, Content, Header, Footer } = Layout;
* *
*/ */
const ChatWindow = () => { const ChatWindow = () => {
const { loginUser } = useAuthContext(); console.log('chat window;;;;;;;;;;;;;;;;;;;;;;;;');
const { userId } = loginUser;
const { websocket, errors } = useConversationState();
const dispatch = useConversationDispatch();
console.log(errors, 'errors;;;;;;;;;;;;;;;;;;;;;;;;');
useEffect(() => { useEffect(() => {
console.log(errors, 'errors 222;;;;;;;;;;;;;;;;;;;;;;;;'); console.log('chat window 222;;;;;;;;;;;;;;;;;;;;;;;;');
return () => {}; return () => {};
}, []); }, []);
const sendMessage = (msgObj) => {
console.log('sendMessage------------------', msgObj);
const contentToSend = sentMsgTypeMapped[msgObj.type].contentToSend(msgObj);
websocket.sendMessage({ ...contentToSend, opi_sn: userId });
const contentToRender = sentMsgTypeMapped[msgObj.type].contentToRender(msgObj);
console.log(contentToRender, 'contentToRender sendMessage------------------');
dispatch(sentNewMessage(contentToRender));
};
return ( return (
<Spin spinning={false} tip={'正在连接...'}> <Spin spinning={false} tip={'正在连接...'}>
<Layout className='h-screen chatwindow-wrapper' style={{ maxHeight: 'calc(100% - 198px)', height: 'calc(100% - 198px)' }}> <Layout className='h-screen chatwindow-wrapper' style={{ maxHeight: 'calc(100% - 198px)', height: 'calc(100% - 198px)' }}>
@ -58,7 +40,7 @@ const ChatWindow = () => {
</div> </div>
</Content> </Content>
<Footer className='ant-layout-sider-light p-1'> <Footer className='ant-layout-sider-light p-1'>
<InputBox onSend={(v) => sendMessage(v)} /> <InputBox />
</Footer> </Footer>
</Layout> </Layout>
</Content> </Content>

@ -1,20 +1,34 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Input, Button, Tabs, List, Space, Popover, Flex } from 'antd'; import { Input, Button, Tabs, List, Space, Popover, Flex } from 'antd';
// import { Input } from 'react-chat-elements'; // import { Input } from 'react-chat-elements';
import { useConversationState } from '@/stores/ConversationContext'; import { useConversationState, useConversationDispatch } from '@/stores/ConversationContext';
import { sentNewMessage } from '@/actions/ConversationActions';
import { useAuthContext } from '@/stores/AuthContext';
import { LikeOutlined, MessageOutlined, StarOutlined, SendOutlined, PlusOutlined, PlusCircleOutlined } from '@ant-design/icons'; import { LikeOutlined, MessageOutlined, StarOutlined, SendOutlined, PlusOutlined, PlusCircleOutlined } from '@ant-design/icons';
import { cloneDeep, getNestedValue, isEmpty } from '@/utils/utils'; import { cloneDeep, getNestedValue, isEmpty } from '@/utils/utils';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import { whatsappTemplatesParamMapped } from '@/lib/msgUtils'; import { whatsappTemplatesParamMapped, sentMsgTypeMapped } from '@/lib/msgUtils';
const InputBox = ({ onSend }) => { const InputBox = () => {
const { currentConversation, templates } = useConversationState(); const { loginUser } = useAuthContext();
const { userId } = loginUser;
const { websocket, currentConversation, templates } = useConversationState();
const dispatch = useConversationDispatch();
const [textContent, setTextContent] = useState(''); const [textContent, setTextContent] = useState('');
const talkabled = !isEmpty(currentConversation.sn); const talkabled = !isEmpty(currentConversation.sn);
const invokeSendMessage = (msgObj) => {
console.log('sendMessage------------------', msgObj);
const contentToSend = sentMsgTypeMapped[msgObj.type].contentToSend(msgObj);
websocket.sendMessage({ ...contentToSend, opi_sn: userId });
const contentToRender = sentMsgTypeMapped[msgObj.type].contentToRender(msgObj);
console.log(contentToRender, 'contentToRender sendMessage------------------');
dispatch(sentNewMessage(contentToRender));
};
const handleSendText = () => { const handleSendText = () => {
if (typeof onSend === 'function' && textContent.trim() !== '') { if (textContent.trim() !== '') {
const msgObj = { const msgObj = {
type: 'text', type: 'text',
text: textContent, text: textContent,
@ -24,14 +38,13 @@ const InputBox = ({ onSend }) => {
date: new Date(), date: new Date(),
status: 'waiting', status: 'waiting',
}; };
onSend(msgObj); invokeSendMessage(msgObj);
setTextContent(''); setTextContent('');
} }
}; };
const handleSendTemplate = (fromTemplate) => { const handleSendTemplate = (fromTemplate) => {
console.log(fromTemplate, 'fromTemplate'); console.log(fromTemplate, 'fromTemplate');
if (typeof onSend === 'function') {
const _conversation = { ...cloneDeep(currentConversation) }; const _conversation = { ...cloneDeep(currentConversation) };
const msgObj = { const msgObj = {
type: 'whatsappTemplate', type: 'whatsappTemplate',
@ -67,9 +80,8 @@ const InputBox = ({ onSend }) => {
}, },
template_origin: fromTemplate, template_origin: fromTemplate,
}; };
onSend(msgObj); invokeSendMessage(msgObj);
setOpenTemplates(false); setOpenTemplates(false);
}
}; };
const [openTemplates, setOpenTemplates] = useState(false); const [openTemplates, setOpenTemplates] = useState(false);

Loading…
Cancel
Save