import ChatHeader from '@/new-components/chat/header/ChatHeader'; import { VerticalAlignBottomOutlined, VerticalAlignTopOutlined } from '@ant-design/icons'; import dynamic from 'next/dynamic'; import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react'; const ChatCompletion = dynamic(() => import('@/new-components/chat/content/ChatCompletion'), { ssr: false }); // eslint-disable-next-line no-empty-pattern const ChatContentContainer = ({}, ref: React.ForwardedRef) => { const scrollRef = useRef(null); const [isScrollToTop, setIsScrollToTop] = useState(false); const [showScrollButtons, setShowScrollButtons] = useState(false); const [isAtTop, setIsAtTop] = useState(true); const [isAtBottom, setIsAtBottom] = useState(false); useImperativeHandle(ref, () => { return scrollRef.current; }); const handleScroll = () => { if (!scrollRef.current) return; const container = scrollRef.current; const scrollTop = container.scrollTop; const scrollHeight = container.scrollHeight; const clientHeight = container.clientHeight; const buffer = 20; // Small buffer for better UX // Check if we're at the top setIsAtTop(scrollTop <= buffer); // Check if we're at the bottom setIsAtBottom(scrollTop + clientHeight >= scrollHeight - buffer); // Header visibility if (scrollTop >= 42 + 32) { setIsScrollToTop(true); } else { setIsScrollToTop(false); } // Show scroll buttons when content is scrollable const isScrollable = scrollHeight > clientHeight; setShowScrollButtons(isScrollable); }; useEffect(() => { if (scrollRef.current) { scrollRef.current.addEventListener('scroll', handleScroll); // Check initially if content is scrollable const isScrollable = scrollRef.current.scrollHeight > scrollRef.current.clientHeight; setShowScrollButtons(isScrollable); } return () => { // eslint-disable-next-line react-hooks/exhaustive-deps scrollRef.current && scrollRef.current.removeEventListener('scroll', handleScroll); }; }, []); const scrollToTop = () => { if (scrollRef.current) { scrollRef.current.scrollTo({ top: 0, behavior: 'smooth', }); } }; const scrollToBottom = () => { if (scrollRef.current) { scrollRef.current.scrollTo({ top: scrollRef.current.scrollHeight, behavior: 'smooth', }); } }; return (
{showScrollButtons && (
{!isAtTop && ( )} {!isAtBottom && ( )}
)}
); }; export default forwardRef(ChatContentContainer);