mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-11-23 21:48:15 +00:00
feat(chat): prevent auto-scroll unless message sent
This commit is contained in:
@@ -7,11 +7,12 @@ import MessageBoxLoading from './MessageBoxLoading';
|
|||||||
import { useChat } from '@/lib/hooks/useChat';
|
import { useChat } from '@/lib/hooks/useChat';
|
||||||
|
|
||||||
const Chat = () => {
|
const Chat = () => {
|
||||||
const { sections, chatTurns, loading, messageAppeared } = useChat();
|
const { sections, loading, messageAppeared, messages } = useChat();
|
||||||
|
|
||||||
const [dividerWidth, setDividerWidth] = useState(0);
|
const [dividerWidth, setDividerWidth] = useState(0);
|
||||||
const dividerRef = useRef<HTMLDivElement | null>(null);
|
const dividerRef = useRef<HTMLDivElement | null>(null);
|
||||||
const messageEnd = useRef<HTMLDivElement | null>(null);
|
const messageEnd = useRef<HTMLDivElement | null>(null);
|
||||||
|
const lastScrolledRef = useRef<number>(0);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const updateDividerWidth = () => {
|
const updateDividerWidth = () => {
|
||||||
@@ -22,35 +23,40 @@ const Chat = () => {
|
|||||||
|
|
||||||
updateDividerWidth();
|
updateDividerWidth();
|
||||||
|
|
||||||
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
|
updateDividerWidth();
|
||||||
|
});
|
||||||
|
|
||||||
|
const currentRef = dividerRef.current;
|
||||||
|
if (currentRef) {
|
||||||
|
resizeObserver.observe(currentRef);
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener('resize', updateDividerWidth);
|
window.addEventListener('resize', updateDividerWidth);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
|
if (currentRef) {
|
||||||
|
resizeObserver.unobserve(currentRef);
|
||||||
|
}
|
||||||
|
resizeObserver.disconnect();
|
||||||
window.removeEventListener('resize', updateDividerWidth);
|
window.removeEventListener('resize', updateDividerWidth);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [sections.length]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const scroll = () => {
|
const scroll = () => {
|
||||||
messageEnd.current?.scrollIntoView({ behavior: 'auto' });
|
messageEnd.current?.scrollIntoView({ behavior: 'auto' });
|
||||||
};
|
};
|
||||||
|
|
||||||
if (chatTurns.length === 1) {
|
if (messages.length === 1) {
|
||||||
document.title = `${chatTurns[0].content.substring(0, 30)} - Perplexica`;
|
document.title = `${messages[0].query.substring(0, 30)} - Perplexica`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const messageEndBottom =
|
if (sections.length > lastScrolledRef.current) {
|
||||||
messageEnd.current?.getBoundingClientRect().bottom ?? 0;
|
|
||||||
|
|
||||||
const distanceFromMessageEnd = window.innerHeight - messageEndBottom;
|
|
||||||
|
|
||||||
if (distanceFromMessageEnd >= -100) {
|
|
||||||
scroll();
|
scroll();
|
||||||
|
lastScrolledRef.current = sections.length;
|
||||||
}
|
}
|
||||||
|
}, [messages]);
|
||||||
if (chatTurns[chatTurns.length - 1]?.role === 'user') {
|
|
||||||
scroll();
|
|
||||||
}
|
|
||||||
}, [chatTurns]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col space-y-6 pt-8 pb-44 lg:pb-32 sm:mx-4 md:mx-8">
|
<div className="flex flex-col space-y-6 pt-8 pb-44 lg:pb-32 sm:mx-4 md:mx-8">
|
||||||
@@ -58,7 +64,7 @@ const Chat = () => {
|
|||||||
const isLast = i === sections.length - 1;
|
const isLast = i === sections.length - 1;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Fragment key={section.userMessage.messageId}>
|
<Fragment key={section.message.messageId}>
|
||||||
<MessageBox
|
<MessageBox
|
||||||
section={section}
|
section={section}
|
||||||
sectionIndex={i}
|
sectionIndex={i}
|
||||||
|
|||||||
Reference in New Issue
Block a user