From c159f34b24ba6ef4bc64c2dbdd6f7107ccc52ab8 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Mon, 5 Oct 2020 17:25:23 -0400 Subject: [PATCH] add infinite scroll for comments --- ui/component/commentsList/view.jsx | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/ui/component/commentsList/view.jsx b/ui/component/commentsList/view.jsx index 15f9095c1..e1ea26e6b 100644 --- a/ui/component/commentsList/view.jsx +++ b/ui/component/commentsList/view.jsx @@ -8,6 +8,7 @@ import Card from 'component/common/card'; import CommentCreate from 'component/commentCreate'; import usePersistedState from 'effects/use-persisted-state'; import { ENABLE_COMMENT_REACTIONS } from 'config'; +import { useIsMobile } from 'effects/use-screensize'; type Props = { comments: Array, @@ -37,6 +38,7 @@ function CommentList(props: Props) { } = props; const commentRef = React.useRef(); + const isMobile = useIsMobile(); const [activeChannel] = usePersistedState('comment-channel', ''); const [start] = React.useState(0); const [end, setEnd] = React.useState(9); @@ -79,6 +81,22 @@ function CommentList(props: Props) { } }, [linkedCommentId]); + useEffect(() => { + function handleCommentScroll(e) { + // Use some arbitrary amount so comments start loading before a user actually reaches the real bottom of the page + // $FlowFixMe + if (window.innerHeight + window.scrollY >= document.body.offsetHeight - (isMobile ? 2750 : 300)) { + handleMoreBelow(); + } + } + + if (moreBelow) { + window.addEventListener('scroll', handleCommentScroll); + } + + return () => window.removeEventListener('scroll', handleCommentScroll); + }, [moreBelow, handleMoreBelow, isMobile]); + function prepareComments(arrayOfComments, linkedComment) { let orderedComments = []; @@ -143,9 +161,9 @@ function CommentList(props: Props) { ); })} - {moreBelow && ( -
-