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';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import MarkdownPreview from 'component/common/markdown-preview';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
uri: string,
|
|
|
|
claim: ?StreamClaim,
|
|
|
|
activeViewers: number,
|
|
|
|
embed?: boolean,
|
2021-03-16 19:37:19 +01:00
|
|
|
doCommentSocketConnect: (string, string) => void,
|
2021-03-10 19:34:21 +01:00
|
|
|
doCommentList: (string) => void,
|
|
|
|
comments: Array<Comment>,
|
|
|
|
fetchingComments: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function LivestreamFeed(props: Props) {
|
|
|
|
const { claim, uri, embed, doCommentSocketConnect, 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);
|
2021-03-16 19:37:19 +01:00
|
|
|
doCommentSocketConnect(uri, claimId);
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|
|
|
|
}, [claimId, uri]);
|
|
|
|
|
|
|
|
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')}>
|
|
|
|
{comment.channel_url ? (
|
|
|
|
<Button
|
|
|
|
target="_blank"
|
|
|
|
className={classnames('livestream__comment-author', {
|
2021-03-11 17:26:11 +01:00
|
|
|
'livestream__comment-author--streamer':
|
|
|
|
claim.signing_channel && claim.signing_channel.claim_id === comment.channel_id,
|
2021-03-10 19:34:21 +01:00
|
|
|
})}
|
|
|
|
navigate={comment.channel_url}
|
|
|
|
label={comment.channel_name}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="livestream__comment-author">{comment.channel_name}</div>
|
|
|
|
)}
|
|
|
|
<MarkdownPreview content={comment.comment} simpleLinks />
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="main--empty" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="livestream__comment-create">
|
|
|
|
<CommentCreate livestream bottom embed={embed} uri={uri} />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|