import { useEffect, useRef, useState, forwardRef, memo } from 'react'; import { MessageBox } from 'react-chat-elements'; import { App, Button } from 'antd'; import { DownOutlined, LoadingOutlined } from '@ant-design/icons'; import { useShallow } from 'zustand/react/shallow'; import useConversationStore from '@/stores/ConversationStore'; import { groupBy, isEmpty, } from '@/utils/utils'; const MessagesList = ({ messages, handlePreview, reference, longListLoading, getMoreMessages, shouldScrollBottom, loadNextPage, handleContactClick, ...props }) => { const { message: appMessage } = App.useApp() const setReferenceMsg = useConversationStore(useShallow((state) => state.setReferenceMsg)); // const messagesEndRef = useRef(null); const messageRefs = useRef([]); const [focusMsg, setFocusMsg] = useState(''); useEffect(() => { setTimeout(() => { setFocusMsg(''); }, 3500); return () => ''; }, [focusMsg]) 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' }); setFocusMsg(id); } }; useEffect(scrollToBottom, [messages]); const RenderText = memo(function renderText({ str, className, template }) { let headerObj, footerObj, buttonsArr; if (!isEmpty(template)) { const componentsObj = groupBy(template.components, (item) => item.type); headerObj = componentsObj?.header?.[0]; footerObj = componentsObj?.footer?.[0]; buttonsArr = componentsObj?.buttons?.reduce((r, c) => r.concat(c.buttons), []); } 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 ( {headerObj ? (
{'text' === (headerObj?.parameters?.[0]?.type || '').toLowerCase() &&
{headerObj.text}
} {'image' === (headerObj?.parameters?.[0]?.type || '').toLowerCase() && } {['document', 'video'].includes((headerObj?.parameters?.[0]?.type || '').toLowerCase()) && ( [ {headerObj.parameters[0].type} ] )}
) : null} {(objArr || []).map((part, index) => { if (part.type === 'link') { return ( {part.key} ); } else { // if (part.type === 'emoji') return part.key; } })} {footerObj ?
{footerObj.text}
: null} {buttonsArr && buttonsArr.length > 0 ? (
{buttonsArr.map((btn, index) => btn.type.toLowerCase() === 'url' ? ( ) : btn.type.toLowerCase() === 'phone_number' ? ( ) : ( ) )}
) : null}
); }); const onLoadMore = async () => { const newLen = await getMoreMessages(); }; // eslint-disable-next-line react/display-name const MessageBoxWithRef = forwardRef((props, ref) => (
)); return (
{loadNextPage && messages.length > 0 && (
{!longListLoading ? ( ) : ( )}
)} {messages.map((message, index) => ( (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={} replyButton={['text', 'document', 'image'].includes(message.whatsapp_msg_type)} {...(message.sender === 'me' ? { styles: { backgroundColor: '#ccd4ae' }, notchStyle: { fill: '#ccd4ae' }, replyButton: ['text', 'document', 'image'].includes(message.whatsapp_msg_type) && message.status !== 'failed' ? true : false, } : {})} className={[ 'whitespace-pre-wrap', message.whatsapp_msg_type === 'sticker' ? 'bg-transparent' : '', message.sender === 'me' ? 'whatsappme-container' : '', focusMsg === message.id ? 'message-box-focus' : '', message.status === 'failed' ? 'failed-msg' : '', ].join(' ')} {...(message.type === 'meetingLink' ? { actionButtons: [ ...(message.waBtn ? [ { onClickButton: () => handleContactClick(message.data), Component: () =>
发消息
, }, ] : []), { onClickButton: () => { navigator.clipboard.writeText(message.text); appMessage.success('复制成功😀') }, Component: () =>
复制
, }, ], } : {})} /> ))}
); }; export default MessagesList;