2023-02-13 16:31:18 -05:00
|
|
|
import React, { useEffect, useState, useRef } from 'react';
|
2023-02-13 20:13:59 -05:00
|
|
|
import { CSSTransition } from 'react-transition-group';
|
2023-02-13 21:15:28 -05:00
|
|
|
import ScrollToBottom from './ScrollToBottom';
|
|
|
|
|
import Message from './Message';
|
2023-02-05 19:41:24 -05:00
|
|
|
|
2023-02-13 18:02:29 -05:00
|
|
|
const Messages = ({ messages }) => {
|
2023-03-13 05:26:17 +08:00
|
|
|
const [currentEditIdx, setCurrentEditIdx] = useState(-1)
|
2023-02-13 16:31:18 -05:00
|
|
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
2023-02-13 18:02:29 -05:00
|
|
|
const scrollableRef = useRef(null);
|
|
|
|
|
const messagesEndRef = useRef(null);
|
|
|
|
|
|
2023-03-13 05:26:17 +08:00
|
|
|
|
2023-02-13 18:02:29 -05:00
|
|
|
useEffect(() => {
|
2023-02-13 20:13:59 -05:00
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
const scrollable = scrollableRef.current;
|
|
|
|
|
const hasScrollbar = scrollable.scrollHeight > scrollable.clientHeight;
|
|
|
|
|
setShowScrollButton(hasScrollbar);
|
2023-02-13 21:15:28 -05:00
|
|
|
}, 650);
|
2023-02-13 20:13:59 -05:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
};
|
2023-02-14 21:23:14 -05:00
|
|
|
}, [messages]);
|
2023-02-05 19:41:24 -05:00
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
2023-02-13 20:13:59 -05:00
|
|
|
setShowScrollButton(false);
|
2023-02-05 19:41:24 -05:00
|
|
|
};
|
|
|
|
|
|
2023-02-13 20:13:59 -05:00
|
|
|
const handleScroll = () => {
|
|
|
|
|
const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current;
|
|
|
|
|
const diff = Math.abs(scrollHeight - scrollTop);
|
|
|
|
|
const bottom =
|
|
|
|
|
diff === clientHeight || (diff <= clientHeight + 25 && diff >= clientHeight - 25);
|
2023-02-13 16:31:18 -05:00
|
|
|
if (bottom) {
|
|
|
|
|
setShowScrollButton(false);
|
|
|
|
|
} else {
|
|
|
|
|
setShowScrollButton(true);
|
|
|
|
|
}
|
2023-02-13 18:02:29 -05:00
|
|
|
};
|
2023-02-13 16:31:18 -05:00
|
|
|
|
2023-02-13 20:13:59 -05:00
|
|
|
let timeoutId = null;
|
|
|
|
|
const debouncedHandleScroll = () => {
|
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
|
timeoutId = setTimeout(handleScroll, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-13 16:31:18 -05:00
|
|
|
const scrollHandler = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
};
|
2023-02-05 19:41:24 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-02-13 18:02:29 -05:00
|
|
|
<div
|
|
|
|
|
className="flex-1 overflow-y-auto "
|
|
|
|
|
ref={scrollableRef}
|
2023-02-13 20:13:59 -05:00
|
|
|
onScroll={debouncedHandleScroll}
|
2023-02-13 18:02:29 -05:00
|
|
|
>
|
2023-02-08 22:58:24 -05:00
|
|
|
{/* <div className="flex-1 overflow-hidden"> */}
|
2023-03-05 13:31:12 -05:00
|
|
|
<div className="h-full dark:gpt-dark-gray">
|
|
|
|
|
<div className="flex h-full flex-col items-center text-sm dark:gpt-dark-gray">
|
2023-02-13 18:02:29 -05:00
|
|
|
{messages.map((message, i) => (
|
|
|
|
|
<Message
|
|
|
|
|
key={i}
|
2023-03-13 05:26:17 +08:00
|
|
|
message={message}
|
|
|
|
|
messages={messages}
|
2023-02-13 18:02:29 -05:00
|
|
|
last={i === messages.length - 1}
|
2023-02-13 20:13:59 -05:00
|
|
|
scrollToBottom={i === messages.length - 1 ? scrollToBottom : null}
|
2023-03-13 05:26:17 +08:00
|
|
|
edit={i===currentEditIdx}
|
|
|
|
|
currentEditIdx={currentEditIdx}
|
|
|
|
|
enterEdit={(cancel) => setCurrentEditIdx(cancel?-1:i)}
|
2023-02-08 09:15:47 -05:00
|
|
|
/>
|
2023-02-13 18:02:29 -05:00
|
|
|
))}
|
2023-02-13 20:13:59 -05:00
|
|
|
<CSSTransition
|
|
|
|
|
in={showScrollButton}
|
2023-02-13 21:15:28 -05:00
|
|
|
timeout={400}
|
2023-02-13 20:13:59 -05:00
|
|
|
classNames="scroll-down"
|
|
|
|
|
unmountOnExit={false}
|
2023-02-14 21:23:14 -05:00
|
|
|
// appear
|
2023-02-13 20:13:59 -05:00
|
|
|
>
|
2023-02-21 19:40:28 -05:00
|
|
|
{() => showScrollButton && <ScrollToBottom scrollHandler={scrollHandler} />}
|
2023-02-13 20:13:59 -05:00
|
|
|
</CSSTransition>
|
|
|
|
|
|
2023-02-13 18:02:29 -05:00
|
|
|
<div
|
2023-03-05 13:31:12 -05:00
|
|
|
className="group h-32 w-full flex-shrink-0 dark:border-gray-900/50 dark:gpt-dark-gray md:h-48"
|
2023-02-13 18:02:29 -05:00
|
|
|
ref={messagesEndRef}
|
|
|
|
|
/>
|
2023-02-08 09:15:47 -05:00
|
|
|
</div>
|
2023-02-13 18:02:29 -05:00
|
|
|
</div>
|
2023-02-08 22:58:24 -05:00
|
|
|
{/* </div> */}
|
2023-02-05 19:41:24 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
2023-02-13 18:02:29 -05:00
|
|
|
};
|
|
|
|
|
|
2023-02-13 20:13:59 -05:00
|
|
|
export default Messages;
|