// @flow import React from 'react'; import classnames from 'classnames'; import Card from 'component/common/card'; import Spinner from 'component/spinner'; import CommentCreate from 'component/commentCreate'; import Button from 'component/button'; import MarkdownPreview from 'component/common/markdown-preview'; type Props = { uri: string, claim: ?StreamClaim, activeViewers: number, embed?: boolean, doCommentSocketConnect: (string, string) => void, doCommentSocketDisconnect: (string) => void, doCommentList: (string) => void, comments: Array, fetchingComments: boolean, }; export default function LivestreamFeed(props: Props) { const { claim, uri, embed, doCommentSocketConnect, doCommentSocketDisconnect, comments, doCommentList, fetchingComments, } = props; const commentsRef = React.createRef(); const hasScrolledComments = React.useRef(); const [performedInitialScroll, setPerformedInitialScroll] = React.useState(false); const claimId = claim && claim.claim_id; const commentsLength = comments && comments.length; React.useEffect(() => { if (claimId) { doCommentList(uri); doCommentSocketConnect(uri, claimId); } return () => { if (claimId) { doCommentSocketDisconnect(claimId); } }; }, [claimId, uri, doCommentList, doCommentSocketConnect, doCommentSocketDisconnect]); React.useEffect(() => { const element = commentsRef.current; function handleScroll() { if (element) { const scrollHeight = element.scrollHeight - element.offsetHeight; const isAtBottom = scrollHeight === element.scrollTop; if (!isAtBottom) { hasScrolledComments.current = true; } else { hasScrolledComments.current = false; } } } if (element) { element.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) { element.scrollTop = element.scrollHeight - element.offsetHeight; if (!performedInitialScroll) { setPerformedInitialScroll(true); } } } } return () => { if (element) { element.removeEventListener('scroll', handleScroll); } }; }, [commentsRef, commentsLength, performedInitialScroll]); if (!claim) { return null; } return ( {fetchingComments && (
)}
0, })} > {!fetchingComments && comments.length > 0 ? (
{comments.map((comment) => (
{comment.channel_url ? (
))}
) : (
)}
} /> ); }