2021-03-10 19:34:21 +01:00
|
|
|
// @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';
|
2021-03-24 03:53:33 +01:00
|
|
|
import CommentView from 'component/comment';
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uri: string,
|
|
|
|
claim: ?StreamClaim,
|
|
|
|
activeViewers: number,
|
|
|
|
embed?: boolean,
|
2021-03-16 19:37:19 +01:00
|
|
|
doCommentSocketConnect: (string, string) => void,
|
2021-03-16 21:59:31 +01:00
|
|
|
doCommentSocketDisconnect: (string) => void,
|
2021-03-10 19:34:21 +01:00
|
|
|
doCommentList: (string) => void,
|
|
|
|
comments: Array<Comment>,
|
|
|
|
fetchingComments: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function LivestreamFeed(props: Props) {
|
2021-03-16 21:59:31 +01:00
|
|
|
const {
|
|
|
|
claim,
|
|
|
|
uri,
|
|
|
|
embed,
|
|
|
|
doCommentSocketConnect,
|
|
|
|
doCommentSocketDisconnect,
|
|
|
|
comments,
|
|
|
|
doCommentList,
|
|
|
|
fetchingComments,
|
|
|
|
} = props;
|
2021-03-10 19:34:21 +01:00
|
|
|
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);
|
2021-03-16 19:37:19 +01:00
|
|
|
doCommentSocketConnect(uri, claimId);
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|
2021-03-16 21:59:31 +01:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (claimId) {
|
|
|
|
doCommentSocketDisconnect(claimId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [claimId, uri, doCommentList, doCommentSocketConnect, doCommentSocketDisconnect]);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
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 (
|
|
|
|
<Card
|
|
|
|
title={__('Live discussion')}
|
|
|
|
smallTitle
|
|
|
|
className="livestream__discussion"
|
|
|
|
actions={
|
|
|
|
<>
|
|
|
|
{fetchingComments && (
|
|
|
|
<div className="main--empty">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
ref={commentsRef}
|
|
|
|
className={classnames('livestream__comments-wrapper', {
|
|
|
|
'livestream__comments-wrapper--with-height': commentsLength > 0,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{!fetchingComments && comments.length > 0 ? (
|
|
|
|
<div className="livestream__comments">
|
|
|
|
{comments.map((comment) => (
|
|
|
|
<div key={comment.comment_id} className={classnames('livestream__comment')}>
|
2021-03-24 03:53:33 +01:00
|
|
|
<CommentView
|
|
|
|
livestream
|
|
|
|
isTopLevel
|
|
|
|
uri={uri}
|
|
|
|
authorUri={comment.channel_url}
|
|
|
|
author={comment.channel_name}
|
|
|
|
claimId={comment.claim_id}
|
|
|
|
commentId={comment.comment_id}
|
|
|
|
message={comment.comment}
|
|
|
|
timePosted={comment.timestamp * 1000}
|
|
|
|
/>
|
2021-03-10 19:34:21 +01:00
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="main--empty" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="livestream__comment-create">
|
|
|
|
<CommentCreate livestream bottom embed={embed} uri={uri} />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|