|
|
|
import { useEffect, useRef, useState, forwardRef, memo } from 'react';
|
|
|
|
import { MessageBox } from 'react-chat-elements';
|
|
|
|
import { Button } from 'antd';
|
|
|
|
import { DownOutlined, LoadingOutlined } from '@ant-design/icons';
|
|
|
|
import { useShallow } from 'zustand/react/shallow';
|
|
|
|
import useConversationStore from '@/stores/ConversationStore';
|
|
|
|
import { isEmpty, } from '@/utils/utils';
|
|
|
|
|
|
|
|
const MessagesList = ({ messages, handlePreview, reference, longListLoading, getMoreMessages, shouldScrollBottom, loadNextPage, ...props }) => {
|
|
|
|
const setReferenceMsg = useConversationStore(useShallow((state) => state.setReferenceMsg));
|
|
|
|
|
|
|
|
// const messagesEndRef = useRef(null);
|
|
|
|
const messageRefs = useRef([]);
|
|
|
|
const prevProps = useRef(props)
|
|
|
|
|
|
|
|
const scrollToBottom = (force = false) => {
|
|
|
|
if (reference.current && (shouldScrollBottom || force)) {
|
|
|
|
reference.current.scrollTop = reference.current.scrollHeight;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const scrollToMessage = (id, index) => {
|
|
|
|
const _i = index || messages.findIndex((msg) => msg.id === id);
|
|
|
|
if (_i >= 0) {
|
|
|
|
messageRefs.current[_i].scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(scrollToBottom, [messages]);
|
|
|
|
|
|
|
|
const RenderText = memo(function renderText({ str }) {
|
|
|
|
const parts = str.split(/(https?:\/\/[^\s]+|\p{Emoji_Presentation})/gmu).filter((s) => s !== '');
|
|
|
|
const links = str.match(/https?:\/\/[\S]+/gi) || [];
|
|
|
|
const emojis = str.match(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g) || [];
|
|
|
|
const extraClass = isEmpty(emojis) ? '' : '';
|
|
|
|
const objArr = parts.reduce((prev, curr, index) => {
|
|
|
|
if (links.includes(curr)) {
|
|
|
|
prev.push({ type: 'link', key: curr });
|
|
|
|
} else if (emojis.includes(curr)) {
|
|
|
|
prev.push({ type: 'emoji', key: curr });
|
|
|
|
} else {
|
|
|
|
prev.push({ type: 'text', key: curr });
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
}, []);
|
|
|
|
return (
|
|
|
|
<span className={`text-base leading-5 emoji-text ${extraClass}`}>
|
|
|
|
{(objArr || []).map((part, index) => {
|
|
|
|
if (part.type === 'link') {
|
|
|
|
return (
|
|
|
|
<a href={part.key} target='_blank' key={`${part.key}${index}`} rel='noreferrer' className='text-base'>
|
|
|
|
{part.key}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// if (part.type === 'emoji')
|
|
|
|
return part.key;
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const onLoadMore = async () => {
|
|
|
|
const newLen = await getMoreMessages();
|
|
|
|
};
|
|
|
|
// eslint-disable-next-line react/display-name
|
|
|
|
const MessageBoxWithRef = forwardRef((props, ref) => (
|
|
|
|
<div ref={ref}>
|
|
|
|
<MessageBox {...props} />
|
|
|
|
</div>
|
|
|
|
));
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='relative h-full overflow-y-auto overflow-x-hidden flex flex-1'>
|
|
|
|
<div ref={reference} className='relative overflow-y-auto overflow-x-hidden block flex-1'>
|
|
|
|
{loadNextPage && (
|
|
|
|
<div className='text-center pt-3 mb-3 h-8 leading-8 '>
|
|
|
|
{!longListLoading ? <Button onClick={onLoadMore} type={'dashed'}>loading more</Button> : <LoadingOutlined className='text-primary' />}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{messages.map((message, index) => (
|
|
|
|
<MessageBoxWithRef
|
|
|
|
ref={(el) => (messageRefs.current[index] = el)}
|
|
|
|
key={message.id}
|
|
|
|
{...message}
|
|
|
|
position={message.sender === 'me' ? 'right' : 'left'}
|
|
|
|
onReplyClick={() => setReferenceMsg(message)}
|
|
|
|
onReplyMessageClick={() => scrollToMessage(message.reply.id)}
|
|
|
|
onOpen={() => handlePreview(message)}
|
|
|
|
onTitleClick={() => handlePreview(message)}
|
|
|
|
text={<RenderText str={message?.text || ''} />}
|
|
|
|
{...(message.sender === 'me'
|
|
|
|
? {
|
|
|
|
styles: { backgroundColor: '#ccd4ae' },
|
|
|
|
notchStyle: { fill: '#ccd4ae' },
|
|
|
|
replyButton: ['text', 'document', 'image'].includes(message.whatsapp_msg_type) && message.status !== 'failed' ? true : false,
|
|
|
|
className: 'whatsappme-container whitespace-pre-wrap',
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
replyButton: ['text', 'document', 'image'].includes(message.whatsapp_msg_type) ? true : false,
|
|
|
|
className: ['whitespace-pre-wrap', message.whatsapp_msg_type === 'sticker' ? 'bg-transparent' : ''].join(' '),
|
|
|
|
})}
|
|
|
|
{...(message.type === 'meetingLink'
|
|
|
|
? {
|
|
|
|
actionButtons: [
|
|
|
|
{
|
|
|
|
onClickButton: () => {
|
|
|
|
navigator.clipboard.writeText(message.text);
|
|
|
|
},
|
|
|
|
Component: () => <div>复制</div>,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
: {})}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
<Button onClick={() => scrollToBottom(true)} ghost type={'dashed'} shape={'circle'} className=' absolute bottom-1 right-4' icon={<DownOutlined />} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MessagesList;
|