LibreChat/client/src/components/Messages/index.jsx

106 lines
3.5 KiB
React
Raw Normal View History

2023-03-13 21:44:30 +08:00
import React, { useEffect, useState, useRef, useMemo } from 'react';
import Spinner from '../svg/Spinner';
import { CSSTransition } from 'react-transition-group';
import ScrollToBottom from './ScrollToBottom';
import MultiMessage from './MultiMessage';
2023-03-13 21:44:30 +08:00
import { useSelector } from 'react-redux';
const Messages = ({ messages, messageTree }) => {
const [currentEditId, setCurrentEditId] = useState(-1);
2023-03-13 21:44:30 +08:00
const { conversationId } = useSelector((state) => state.convo);
const { model, customModel, chatGptLabel } = useSelector((state) => state.submit);
const { models } = useSelector((state) => state.models);
const [showScrollButton, setShowScrollButton] = useState(false);
2023-02-13 18:02:29 -05:00
const scrollableRef = useRef(null);
const messagesEndRef = useRef(null);
const modelName = models.find(element => element.model==model)?.name
2023-02-13 18:02:29 -05:00
useEffect(() => {
const timeoutId = setTimeout(() => {
const scrollable = scrollableRef.current;
const hasScrollbar = scrollable.scrollHeight > scrollable.clientHeight;
setShowScrollButton(hasScrollbar);
}, 650);
return () => {
clearTimeout(timeoutId);
};
}, [messages]);
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
setShowScrollButton(false);
};
const handleScroll = () => {
const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current;
const diff = Math.abs(scrollHeight - scrollTop);
const bottom =
diff === clientHeight || (diff <= clientHeight + 25 && diff >= clientHeight - 25);
if (bottom) {
setShowScrollButton(false);
} else {
setShowScrollButton(true);
}
2023-02-13 18:02:29 -05:00
};
let timeoutId = null;
const debouncedHandleScroll = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(handleScroll, 100);
};
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={debouncedHandleScroll}
2023-02-13 18:02:29 -05:00
>
2023-02-08 22:58:24 -05:00
{/* <div className="flex-1 overflow-hidden"> */}
<div className="dark:gpt-dark-gray h-full">
<div className="dark:gpt-dark-gray flex h-full flex-col items-center text-sm">
<div className="flex w-full items-center justify-center gap-1 border-b border-black/10 bg-gray-50 p-3 text-gray-500 dark:border-gray-900/50 dark:bg-gray-700 dark:text-gray-300 text-sm">
Model: {modelName} {customModel?`(${customModel})`:null}
</div>
{messageTree.length === 0 ? (
<Spinner />
) : (
<>
<MultiMessage
key={conversationId} // avoid internal state mixture
messageList={messageTree}
messages={messages}
scrollToBottom={scrollToBottom}
currentEditId={currentEditId}
setCurrentEditId={setCurrentEditId}
/>
<CSSTransition
in={showScrollButton}
timeout={400}
classNames="scroll-down"
unmountOnExit={false}
// appear
>
{() => showScrollButton && <ScrollToBottom scrollHandler={scrollHandler} />}
</CSSTransition>
</>
)}
2023-02-13 18:02:29 -05:00
<div
className="dark:gpt-dark-gray group h-32 w-full flex-shrink-0 dark:border-gray-900/50 md:h-48"
2023-02-13 18:02:29 -05:00
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 React.memo(Messages);