import React, { useRef, useEffect, useState, useCallback } from "react"; import { motion, AnimatePresence } from "framer-motion"; import LoadingAnimation from "./LoadingAnimation"; interface MessagesProps { currentUser: IUser; } export const Messages: React.FC = ({ currentUser, }) => { const messagesEndRef = useRef(null); const scrollContainerRef = useRef(null); const ref: any = useRef(null); const [isScrolledToBottom, setIsScrolledToBottom] = useState(true); const handleScroll = useCallback(() => { if (scrollContainerRef.current) { const { scrollTop, scrollHeight, clientHeight } = scrollContainerRef.current; const isAtBottom = scrollTop + clientHeight >= scrollHeight - 10; // 10px threshold setIsScrolledToBottom(isAtBottom); } }, []); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; useEffect(() => { const scrollContainer = scrollContainerRef.current; if (scrollContainer) { scrollContainer.addEventListener("scroll", handleScroll); return () => scrollContainer.removeEventListener("scroll", handleScroll); } }, [handleScroll]); return (
); };