LibreChat/src/components/main/Messages.jsx

67 lines
1.9 KiB
React
Raw Normal View History

import React, { useEffect, useState, useRef } from 'react';
import useDidMountEffect from '~/hooks/useDidMountEffect';
import Message from './Message';
import ScrollToBottom from './ScrollToBottom';
2023-02-13 18:02:29 -05:00
const Messages = ({ messages }) => {
const [showScrollButton, setShowScrollButton] = useState(false);
2023-02-13 18:02:29 -05:00
const scrollableRef = useRef(null);
const messagesEndRef = useRef(null);
useEffect(() => {
const scrollable = scrollableRef.current;
const hasScrollbar = scrollable.scrollHeight > scrollable.clientHeight;
setShowScrollButton(hasScrollbar);
}, [scrollableRef]);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
};
const handleScroll = (e) => {
const bottom = e.target.scrollHeight - e.target.scrollTop === e.target.clientHeight;
if (bottom) {
setShowScrollButton(false);
} else {
setShowScrollButton(true);
}
2023-02-13 18:02:29 -05:00
};
const scrollHandler = (e) => {
e.preventDefault();
scrollToBottom();
};
return (
2023-02-13 18:02:29 -05:00
<div
className="flex-1 overflow-y-auto "
ref={scrollableRef}
onScroll={handleScroll}
>
2023-02-08 22:58:24 -05:00
{/* <div className="flex-1 overflow-hidden"> */}
2023-02-13 18:02:29 -05:00
<div className="h-full dark:bg-gray-800">
<div className="flex h-full flex-col items-center text-sm dark:bg-gray-800">
{messages.map((message, i) => (
<Message
key={i}
sender={message.sender}
text={message.text}
last={i === messages.length - 1}
error={!!message.error ? true : false}
/>
2023-02-13 18:02:29 -05:00
))}
{showScrollButton && <ScrollToBottom scrollHandler={scrollHandler} />}
<div
className="group h-32 w-full flex-shrink-0 dark:border-gray-900/50 dark:bg-gray-800 md:h-48"
ref={messagesEndRef}
/>
</div>
2023-02-13 18:02:29 -05:00
</div>
2023-02-08 22:58:24 -05:00
{/* </div> */}
</div>
);
2023-02-13 18:02:29 -05:00
};
export default Messages