Skip to content

Commit

Permalink
Fix conversation scrolling during generation (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
anastasiya1155 authored Aug 11, 2023
1 parent 57030db commit b4eeadc
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions client/src/components/Chat/Conversation.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useRef } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import {
ChatMessage,
ChatMessageAuthor,
Expand Down Expand Up @@ -26,6 +26,17 @@ const Conversation = ({
onMessageEdit,
}: Props) => {
const messagesRef = useRef<HTMLDivElement>(null);
const [userScrolledUp, setUserScrolledUp] = useState(false);

const handleScroll = useCallback(() => {
if (messagesRef.current) {
const scrollTop = messagesRef.current.scrollTop;
const scrollHeight = messagesRef.current.scrollHeight;
const clientHeight = messagesRef.current.clientHeight;

setUserScrolledUp(scrollTop < scrollHeight - clientHeight);
}
}, []);

const scrollToBottom = useCallback(() => {
if (messagesRef.current) {
Expand All @@ -38,15 +49,18 @@ const Conversation = ({
}, []);

useEffect(() => {
scrollToBottom();
}, [conversation, scrollToBottom]);
if (!userScrolledUp) {
scrollToBottom();
}
}, [conversation, scrollToBottom, userScrolledUp]);

return (
<div
className={`w-full flex flex-col gap-3 pb-3 overflow-auto ${
!isHistory ? 'max-h-60' : ''
}`}
ref={messagesRef}
onScroll={handleScroll}
>
{conversation.map((m, i) => (
<Message
Expand Down

0 comments on commit b4eeadc

Please sign in to comment.