2023-03-17 12:34:54 -04:00
|
|
|
import React, { useEffect, useState, useRef, useCallback } from 'react';
|
2023-03-29 00:08:02 +08:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2023-03-15 10:42:45 -04:00
|
|
|
import Spinner from '../svg/Spinner';
|
2023-03-19 11:45:03 -04:00
|
|
|
import { throttle } from 'lodash';
|
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';
|
2023-03-15 10:42:45 -04:00
|
|
|
import MultiMessage from './MultiMessage';
|
2023-03-31 16:35:30 +08:00
|
|
|
import MessageHeader from './MessageHeader';
|
2023-02-05 19:41:24 -05:00
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
import store from '~/store';
|
|
|
|
|
|
2023-03-29 00:08:02 +08:00
|
|
|
export default function Messages({ isSearchView = false }) {
|
2023-03-15 10:42:45 -04:00
|
|
|
const [currentEditId, setCurrentEditId] = 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-29 00:08:02 +08:00
|
|
|
const messagesTree = useRecoilValue(store.messagesTree);
|
|
|
|
|
const searchResultMessagesTree = useRecoilValue(store.searchResultMessagesTree);
|
|
|
|
|
|
|
|
|
|
const _messagesTree = isSearchView ? searchResultMessagesTree : messagesTree;
|
|
|
|
|
|
|
|
|
|
const conversation = useRecoilValue(store.conversation) || {};
|
2023-03-31 16:35:30 +08:00
|
|
|
const { conversationId } = conversation;
|
2023-03-29 00:08:02 +08:00
|
|
|
|
2023-03-31 00:20:45 +08:00
|
|
|
// const models = useRecoilValue(store.models) || [];
|
|
|
|
|
// const modelName = models.find(element => element.model == model)?.name;
|
2023-03-16 21:59:20 +08:00
|
|
|
|
2023-02-13 18:02:29 -05:00
|
|
|
useEffect(() => {
|
2023-02-13 20:13:59 -05:00
|
|
|
const timeoutId = setTimeout(() => {
|
2023-03-17 12:34:54 -04:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current;
|
|
|
|
|
const diff = Math.abs(scrollHeight - scrollTop);
|
2023-03-28 22:39:27 +08:00
|
|
|
const percent = Math.abs(clientHeight - diff) / clientHeight;
|
2023-03-17 12:34:54 -04:00
|
|
|
const hasScrollbar = scrollHeight > clientHeight && percent > 0.2;
|
2023-02-13 20:13:59 -05:00
|
|
|
setShowScrollButton(hasScrollbar);
|
2023-02-13 21:15:28 -05:00
|
|
|
}, 650);
|
2023-02-13 20:13:59 -05:00
|
|
|
|
2023-03-17 12:34:54 -04:00
|
|
|
// Add a listener on the window object
|
|
|
|
|
window.addEventListener('scroll', handleScroll);
|
|
|
|
|
|
2023-02-13 20:13:59 -05:00
|
|
|
return () => {
|
|
|
|
|
clearTimeout(timeoutId);
|
2023-03-17 12:34:54 -04:00
|
|
|
window.removeEventListener('scroll', handleScroll);
|
2023-02-13 20:13:59 -05:00
|
|
|
};
|
2023-03-29 00:08:02 +08:00
|
|
|
}, [_messagesTree]);
|
2023-02-05 19:41:24 -05:00
|
|
|
|
2023-03-28 22:39:27 +08:00
|
|
|
const scrollToBottom = useCallback(
|
|
|
|
|
throttle(
|
|
|
|
|
() => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
setShowScrollButton(false);
|
|
|
|
|
},
|
|
|
|
|
750,
|
|
|
|
|
{ leading: true }
|
|
|
|
|
),
|
|
|
|
|
[messagesEndRef]
|
|
|
|
|
);
|
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);
|
2023-03-28 22:39:27 +08:00
|
|
|
const percent = Math.abs(clientHeight - diff) / clientHeight;
|
2023-03-17 12:34:54 -04:00
|
|
|
if (percent <= 0.2) {
|
2023-02-13 16:31:18 -05:00
|
|
|
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-03-28 22:39:27 +08:00
|
|
|
const scrollHandler = e => {
|
2023-02-13 16:31:18 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
};
|
2023-02-05 19:41:24 -05:00
|
|
|
|
|
|
|
|
return (
|
2023-02-13 18:02:29 -05:00
|
|
|
<div
|
2023-03-30 18:18:06 +08:00
|
|
|
className="flex-1 overflow-y-auto pt-0"
|
2023-02-13 18:02:29 -05:00
|
|
|
ref={scrollableRef}
|
2023-02-13 20:13:59 -05:00
|
|
|
onScroll={debouncedHandleScroll}
|
2023-02-13 18:02:29 -05:00
|
|
|
>
|
2023-03-15 10:42:45 -04:00
|
|
|
<div className="dark:gpt-dark-gray h-full">
|
2023-03-17 03:42:55 +08:00
|
|
|
<div className="dark:gpt-dark-gray flex h-full flex-col items-center text-sm">
|
2023-03-31 16:35:30 +08:00
|
|
|
<MessageHeader isSearchView={isSearchView} />
|
2023-03-29 00:08:02 +08:00
|
|
|
{_messagesTree === null ? (
|
2023-03-15 10:42:45 -04:00
|
|
|
<Spinner />
|
2023-03-29 00:18:27 +08:00
|
|
|
) : _messagesTree?.length == 0 && isSearchView ? (
|
|
|
|
|
<div className="flex w-full items-center justify-center gap-1 bg-gray-50 p-3 text-sm text-gray-500 dark:border-gray-900/50 dark:bg-gray-800 dark:text-gray-300">
|
|
|
|
|
Nothing found
|
|
|
|
|
</div>
|
2023-03-15 10:42:45 -04:00
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<MultiMessage
|
|
|
|
|
key={conversationId} // avoid internal state mixture
|
2023-04-06 02:06:39 +08:00
|
|
|
messageId={conversationId}
|
2023-03-28 22:39:27 +08:00
|
|
|
conversation={conversation}
|
2023-03-29 00:08:02 +08:00
|
|
|
messagesTree={_messagesTree}
|
2023-03-15 10:42:45 -04:00
|
|
|
scrollToBottom={scrollToBottom}
|
|
|
|
|
currentEditId={currentEditId}
|
|
|
|
|
setCurrentEditId={setCurrentEditId}
|
2023-03-29 02:29:15 +08:00
|
|
|
isSearchView={isSearchView}
|
2023-03-15 10:42:45 -04:00
|
|
|
/>
|
|
|
|
|
<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
|
2023-03-15 10:42:45 -04:00
|
|
|
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}
|
|
|
|
|
/>
|
2023-02-08 09:15:47 -05:00
|
|
|
</div>
|
2023-02-13 18:02:29 -05:00
|
|
|
</div>
|
2023-02-05 19:41:24 -05:00
|
|
|
</div>
|
|
|
|
|
);
|
2023-03-17 12:34:54 -04:00
|
|
|
}
|