Prechádzať zdrojové kódy

Scroll to bottom of messages on new messages state change (#230)

Co-authored-by: George Balch <george.balch@proton.me>
George Balch 1 rok pred
rodič
commit
98919d15ae
1 zmenil súbory, kde vykonal 7 pridanie a 1 odobranie
  1. 7 1
      frontend/src/components/ChatInterface.tsx

+ 7 - 1
frontend/src/components/ChatInterface.tsx

@@ -1,4 +1,4 @@
-import React, { useState } from "react";
+import React, { useEffect, useRef, useState } from "react";
 import { useSelector } from "react-redux";
 import "./ChatInterface.css";
 import userAvatar from "../assets/user-avatar.png";
@@ -7,8 +7,13 @@ import { RootState } from "../store";
 import { sendChatMessage } from "../services/chatService";
 
 function MessageList(): JSX.Element {
+  const messagesEndRef = useRef<HTMLDivElement>(null);
   const { messages } = useSelector((state: RootState) => state.chat);
 
+  useEffect(() => {
+    messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
+  }, [messages]);
+
   return (
     <div className="message-list">
       {messages.map((msg, index) => (
@@ -25,6 +30,7 @@ function MessageList(): JSX.Element {
           </div>
         </div>
       ))}
+      <div ref={messagesEndRef} />
     </div>
   );
 }