import { ReactNode, useCallback, useEffect, useMemo, useState } from 'react'; import { FaChevronDown } from 'react-icons/fa'; const ScrollToBottom = ({ children, className = '' }: { children: ReactNode; className?: string }) => { const [node, setNode] = useState(null); const [show, setShow] = useState(false); const bottom = useCallback((node: HTMLDivElement) => { if (node) { node.scrollIntoView({ behavior: 'smooth' }); setNode(node); } }, []); useMemo(() => { if (node) { const container = document.querySelector('main > div'); container?.addEventListener('DOMNodeInserted', (e) => { if (!(e.target as Element).classList.contains('message')) return; if (node?.getBoundingClientRect() && node.getBoundingClientRect().y >= window.innerHeight) { node?.scrollIntoView({ behavior: 'smooth' }); } }); container?.querySelectorAll('.message > div > img').forEach((img) => { img.addEventListener('load', () => { if (node?.getBoundingClientRect() && node.getBoundingClientRect().y >= window.innerHeight) { node?.scrollIntoView({ behavior: 'smooth' }); } }); }); const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { setShow(false); } else { setShow(true); } }, { threshold: 1 } ); observer.observe(node); return () => observer.disconnect(); } }, [node]); return ( <>
{children}
); }; export default ScrollToBottom;