fix scroll behavior
This commit is contained in:
		
							parent
							
								
									83987d3021
								
							
						
					
					
						commit
						68cd41a6dc
					
				|  | @ -22,17 +22,46 @@ interface Props { | ||||||
| 
 | 
 | ||||||
| export const Chat: FC<Props> = ({ conversation, models, messageIsStreaming, modelError, messageError, loading, lightMode, onSend, onUpdateConversation, stopConversationRef }) => { | export const Chat: FC<Props> = ({ conversation, models, messageIsStreaming, modelError, messageError, loading, lightMode, onSend, onUpdateConversation, stopConversationRef }) => { | ||||||
|   const [currentMessage, setCurrentMessage] = useState<Message>(); |   const [currentMessage, setCurrentMessage] = useState<Message>(); | ||||||
|  |   const [autoScrollEnabled, setAutoScrollEnabled] = useState(true); | ||||||
| 
 | 
 | ||||||
|   const messagesEndRef = useRef<HTMLDivElement>(null); |   const messagesEndRef = useRef<HTMLDivElement>(null); | ||||||
|  |   const chatContainerRef = useRef<HTMLDivElement>(null); | ||||||
| 
 | 
 | ||||||
|   const scrollToBottom = () => { |   const scrollToBottom = () => { | ||||||
|     messagesEndRef.current?.scrollIntoView({ behavior: "auto" }); |     if (autoScrollEnabled) { | ||||||
|  |       messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); | ||||||
|  |     } | ||||||
|  |   }; | ||||||
|  | 
 | ||||||
|  |   const handleScroll = () => { | ||||||
|  |     if (chatContainerRef.current) { | ||||||
|  |       const { scrollTop, scrollHeight, clientHeight } = chatContainerRef.current; | ||||||
|  |       const bottomTolerance = 50; | ||||||
|  | 
 | ||||||
|  |       if (scrollTop + clientHeight < scrollHeight - bottomTolerance) { | ||||||
|  |         setAutoScrollEnabled(false); | ||||||
|  |       } else { | ||||||
|  |         setAutoScrollEnabled(true); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|   }; |   }; | ||||||
| 
 | 
 | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     scrollToBottom(); |     scrollToBottom(); | ||||||
|   }, [conversation.messages]); |   }, [conversation.messages]); | ||||||
| 
 | 
 | ||||||
|  |   useEffect(() => { | ||||||
|  |     const chatContainer = chatContainerRef.current; | ||||||
|  | 
 | ||||||
|  |     if (chatContainer) { | ||||||
|  |       chatContainer.addEventListener("scroll", handleScroll); | ||||||
|  | 
 | ||||||
|  |       return () => { | ||||||
|  |         chatContainer.removeEventListener("scroll", handleScroll); | ||||||
|  |       }; | ||||||
|  |     } | ||||||
|  |   }, []); | ||||||
|  | 
 | ||||||
|   return ( |   return ( | ||||||
|     <div className="relative flex-1 overflow-none dark:bg-[#343541] bg-white"> |     <div className="relative flex-1 overflow-none dark:bg-[#343541] bg-white"> | ||||||
|       {modelError ? ( |       {modelError ? ( | ||||||
|  | @ -43,7 +72,10 @@ export const Chat: FC<Props> = ({ conversation, models, messageIsStreaming, mode | ||||||
|         </div> |         </div> | ||||||
|       ) : ( |       ) : ( | ||||||
|         <> |         <> | ||||||
|           <div className="overflow-scroll max-h-full"> |           <div | ||||||
|  |             className="overflow-scroll max-h-full" | ||||||
|  |             ref={chatContainerRef} | ||||||
|  |           > | ||||||
|             {conversation.messages.length === 0 ? ( |             {conversation.messages.length === 0 ? ( | ||||||
|               <> |               <> | ||||||
|                 <div className="flex flex-col mx-auto pt-12 space-y-10 w-[350px] sm:w-[600px]"> |                 <div className="flex flex-col mx-auto pt-12 space-y-10 w-[350px] sm:w-[600px]"> | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue