diff --git a/ui/component/livestreamComments/view.jsx b/ui/component/livestreamComments/view.jsx index a9e65da00..5d570355b 100644 --- a/ui/component/livestreamComments/view.jsx +++ b/ui/component/livestreamComments/view.jsx @@ -45,7 +45,7 @@ export default function LivestreamComments(props: Props) { myChannels, } = props; const commentsRef = React.createRef(); - const hasScrolledComments = React.useRef(); + const [scrollBottom, setScrollBottom] = React.useState(true); const [viewMode, setViewMode] = React.useState(VIEW_MODE_CHAT); const [performedInitialScroll, setPerformedInitialScroll] = React.useState(false); const claimId = claim && claim.claim_id; @@ -79,48 +79,43 @@ export default function LivestreamComments(props: Props) { }, [claimId, uri, doCommentList, doSuperChatList, doCommentSocketConnect, doCommentSocketDisconnect]); React.useEffect(() => { - const element = commentsRef.current; + const discussionElement = document.querySelector('.livestream__comments'); + const commentElement = document.querySelector('.livestream-comment'); function handleScroll() { - if (element) { - const scrollHeight = element.scrollHeight - element.offsetHeight; - const isAtBottom = scrollHeight <= element.scrollTop + 100; + if (discussionElement) { + const negativeCommentHeight = commentElement && (-1 * commentElement.offsetHeight); + const isAtRecent = negativeCommentHeight && (discussionElement.scrollTop >= negativeCommentHeight); - if (!isAtBottom) { - hasScrolledComments.current = true; - } else { - hasScrolledComments.current = false; - } + setScrollBottom(isAtRecent); } } - if (element) { - element.addEventListener('scroll', handleScroll); + if (discussionElement) { + discussionElement.addEventListener('scroll', handleScroll); if (commentsLength > 0) { // Only update comment scroll if the user hasn't scrolled up to view old comments // If they have, do nothing - if (!hasScrolledComments.current || !performedInitialScroll) { - setTimeout(() => (element.scrollTop = element.scrollHeight - element.offsetHeight + 100), 20); - - if (!performedInitialScroll) { - setPerformedInitialScroll(true); - } + if (!performedInitialScroll) { + setTimeout(() => (discussionElement.scrollTop = discussionElement.scrollHeight - discussionElement.offsetHeight + 100), 20); + setPerformedInitialScroll(true); } } - } - return () => { - if (element) { - element.removeEventListener('scroll', handleScroll); - } - }; - }, [commentsRef, commentsLength, performedInitialScroll]); + return () => discussionElement.removeEventListener('scroll', handleScroll); + } + }, [commentsLength, performedInitialScroll, setPerformedInitialScroll, setScrollBottom]); if (!claim) { return null; } + function scrollBack() { + const element = document.querySelector('.livestream__comments'); + if (element) element.scrollTop = 0; + } + return (
@@ -199,6 +194,15 @@ export default function LivestreamComments(props: Props) {
)} + {!scrollBottom && ( +