@@ -29,18 +29,20 @@ interface IMessagesList { loading: boolean } +const SCROLL_THRESHOLD = 5 + export const ChatMessagesList: React.FC = memo( ({ messageResponse, onLoadImage, setResendValue, modelTitle, device, modelType, botParams, getMessagesPagination, deleteMessage, loading }) => { const paginationScroll = React.useRef() const [isPaginating, setIsPaginating] = React.useState(false) const [chatScrollHeight, setChatScrollHeight] = React.useState(0) - const [scrollBottom, setScrollBottom] = React.useState(0) + const lastScrollTop = useRef(0) + const [showScrollDown, setShowScrollDown] = useState(false) const desktop = device === 'desktop' const { status } = useSession() const [modal, setModal] = useState(false) const [isNewMessage, setIsNewMessage] = useState(false) - const [currentSrc, setCurrentSrc] = useState(null) const onlyImageMessage = useMemo(() => { @@ -64,17 +66,15 @@ export const ChatMessagesList: React.FC = memo( const time = setTimeout(() => { if (block) { - //@ts-ignore block.scrollTo({ top: block.scrollHeight, - behavior: 'smooth', // добавляем плавную прокрутку + behavior: 'smooth', }) } }, 250) return () => clearTimeout(time) } else if (messageResponse != undefined && isPaginating) { if (block) { - //@ts-ignore block.scrollTop = block.scrollHeight - chatScrollHeight setChatScrollHeight(paginationScroll.current.scrollHeight) } @@ -87,15 +87,31 @@ export const ChatMessagesList: React.FC = memo( }, [messageResponse]) const handleScroll = () => { - setScrollBottom(paginationScroll.current?.scrollHeight - paginationScroll.current?.scrollTop - paginationScroll.current?.clientHeight) - - if (paginationScroll.current && messageResponse?.length !== 0) { - const { scrollTop, scrollHeight, clientHeight } = paginationScroll.current - if (scrollTop === 0) { - if (getMessagesPagination) { - setIsPaginating(true) - getMessagesPagination() - } + const block = paginationScroll.current + if (!block) return + + const { scrollTop, scrollHeight, clientHeight } = block + const distanceFromBottom = scrollHeight - scrollTop - clientHeight + + const isScrollingDown = scrollTop > lastScrollTop.current + SCROLL_THRESHOLD + const isScrollingUp = scrollTop < lastScrollTop.current - SCROLL_THRESHOLD + + if (isScrollingDown) { + setShowScrollDown(true) + } else if (isScrollingUp) { + setShowScrollDown(false) + } + + if (distanceFromBottom < 50) { + setShowScrollDown(false) + } + + lastScrollTop.current = scrollTop + + if (scrollTop === 0 && messageResponse?.length !== 0) { + if (getMessagesPagination) { + setIsPaginating(true) + getMessagesPagination() } } } @@ -130,7 +146,7 @@ export const ChatMessagesList: React.FC = memo( ref={paginationScroll} onScroll={handleScroll} > - {scrollBottom > 500 && ( + {showScrollDown && ( = memo( right: 0 }} onClick={() => { - //@ts-ignore const block = paginationScroll.current - block.scrollTo({ top: block.scrollHeight, - behavior: 'smooth', // добавляем плавную прокрутку + behavior: 'smooth', }) }} > @@ -196,4 +210,4 @@ export const ChatMessagesList: React.FC = memo( } ) -ChatMessagesList.displayName = 'ChatMessagesList' +ChatMessagesList.displayName = 'ChatMessagesList' \ No newline at end of file