|
|
|
@ -1,37 +1,20 @@
|
|
|
|
|
import { useEffect, useRef, useState, forwardRef, memo } from 'react';
|
|
|
|
|
import { MessageBox } from 'react-chat-elements';
|
|
|
|
|
import { Button } from 'antd';
|
|
|
|
|
import { DownOutlined } from '@ant-design/icons';
|
|
|
|
|
import { DownOutlined, LoadingOutlined } from '@ant-design/icons';
|
|
|
|
|
import { useShallow } from 'zustand/react/shallow';
|
|
|
|
|
import useConversationStore from '@/stores/ConversationStore';
|
|
|
|
|
import { isEmpty, olog } from '@/utils/utils';
|
|
|
|
|
import { isEmpty, } from '@/utils/utils';
|
|
|
|
|
|
|
|
|
|
const MessagesList = ({ messages, handlePreview, reference }) => {
|
|
|
|
|
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 [page, setPage] = useState(1);
|
|
|
|
|
let timeout = null;
|
|
|
|
|
const prevProps = useRef(props)
|
|
|
|
|
|
|
|
|
|
const fetchNextPage = async () => {
|
|
|
|
|
olog('fetchNextPage')
|
|
|
|
|
setPage(page + 1);
|
|
|
|
|
// Fetch next page of messages here
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleScroll = (e) => {
|
|
|
|
|
const { scrollTop } = e.target;
|
|
|
|
|
const delay = 1000; // 1 second
|
|
|
|
|
|
|
|
|
|
if (scrollTop === 0) {
|
|
|
|
|
if (timeout) clearTimeout(timeout);
|
|
|
|
|
timeout = setTimeout(fetchNextPage, delay);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
if (reference.current) {
|
|
|
|
|
const scrollToBottom = (force = false) => {
|
|
|
|
|
if (reference.current && (shouldScrollBottom || force)) {
|
|
|
|
|
reference.current.scrollTop = reference.current.scrollHeight;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -45,18 +28,6 @@ const MessagesList = ({ messages, handlePreview, reference }) => {
|
|
|
|
|
|
|
|
|
|
useEffect(scrollToBottom, [messages]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const messageList = reference.current;
|
|
|
|
|
if (messageList) {
|
|
|
|
|
messageList.addEventListener('scroll', handleScroll);
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
if (messageList) {
|
|
|
|
|
messageList.removeEventListener('scroll', handleScroll);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
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) || [];
|
|
|
|
@ -90,6 +61,9 @@ const MessagesList = ({ messages, handlePreview, reference }) => {
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const onLoadMore = async () => {
|
|
|
|
|
const newLen = await getMoreMessages();
|
|
|
|
|
};
|
|
|
|
|
// eslint-disable-next-line react/display-name
|
|
|
|
|
const MessageBoxWithRef = forwardRef((props, ref) => (
|
|
|
|
|
<div ref={ref}>
|
|
|
|
@ -100,6 +74,11 @@ const MessagesList = ({ messages, handlePreview, reference }) => {
|
|
|
|
|
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)}
|
|
|
|
@ -137,7 +116,7 @@ const MessagesList = ({ messages, handlePreview, reference }) => {
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
<Button onClick={scrollToBottom} ghost type={'dashed'} shape={'circle'} className=' absolute bottom-1 right-4' icon={<DownOutlined />} />
|
|
|
|
|
<Button onClick={() => scrollToBottom(true)} ghost type={'dashed'} shape={'circle'} className=' absolute bottom-1 right-4' icon={<DownOutlined />} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|