2021-03-10 19:34:21 +01:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Spinner from 'component/spinner';
|
|
|
|
import CommentCreate from 'component/commentCreate';
|
2021-04-23 21:59:48 +02:00
|
|
|
import LivestreamComment from 'component/livestreamComment';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import UriIndicator from 'component/uriIndicator';
|
|
|
|
import CreditAmount from 'component/common/credit-amount';
|
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
|
|
|
import Tooltip from 'component/common/tooltip';
|
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-07-15 16:43:28 +02:00
|
|
|
doCommentList: (string, string, number, number) => void,
|
2021-03-10 19:34:21 +01:00
|
|
|
comments: Array<Comment>,
|
|
|
|
fetchingComments: boolean,
|
2021-04-23 21:59:48 +02:00
|
|
|
doSuperChatList: (string) => void,
|
|
|
|
superChats: Array<Comment>,
|
2021-05-04 17:08:36 +02:00
|
|
|
myChannels: ?Array<ChannelClaim>,
|
2021-03-10 19:34:21 +01:00
|
|
|
};
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
const VIEW_MODE_CHAT = 'view_chat';
|
|
|
|
const VIEW_MODE_SUPER_CHAT = 'view_superchat';
|
2021-06-22 16:42:52 +02:00
|
|
|
const COMMENT_SCROLL_OFFSET = 100;
|
|
|
|
const COMMENT_SCROLL_TIMEOUT = 25;
|
2021-04-23 21:59:48 +02:00
|
|
|
|
|
|
|
export default function LivestreamComments(props: Props) {
|
2021-03-16 21:59:31 +01:00
|
|
|
const {
|
|
|
|
claim,
|
|
|
|
uri,
|
|
|
|
embed,
|
|
|
|
doCommentSocketConnect,
|
|
|
|
doCommentSocketDisconnect,
|
2021-08-11 22:58:55 +02:00
|
|
|
comments: commentsByChronologicalOrder,
|
2021-03-16 21:59:31 +01:00
|
|
|
doCommentList,
|
|
|
|
fetchingComments,
|
2021-04-23 21:59:48 +02:00
|
|
|
doSuperChatList,
|
2021-05-04 17:08:36 +02:00
|
|
|
myChannels,
|
2021-08-11 22:58:55 +02:00
|
|
|
superChats: superChatsByTipAmount,
|
2021-03-16 21:59:31 +01:00
|
|
|
} = props;
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-08-11 22:58:55 +02:00
|
|
|
let superChatsFiatAmount, superChatsTotalAmount;
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
const commentsRef = React.createRef();
|
2021-06-21 15:51:07 +02:00
|
|
|
const [scrollBottom, setScrollBottom] = React.useState(true);
|
2021-04-23 21:59:48 +02:00
|
|
|
const [viewMode, setViewMode] = React.useState(VIEW_MODE_CHAT);
|
2021-03-10 19:34:21 +01:00
|
|
|
const [performedInitialScroll, setPerformedInitialScroll] = React.useState(false);
|
|
|
|
const claimId = claim && claim.claim_id;
|
2021-08-11 22:58:55 +02:00
|
|
|
const commentsLength = commentsByChronologicalOrder && commentsByChronologicalOrder.length;
|
|
|
|
const commentsToDisplay = viewMode === VIEW_MODE_CHAT ? commentsByChronologicalOrder : superChatsByTipAmount;
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2021-07-16 15:55:47 +02:00
|
|
|
const discussionElement = document.querySelector('.livestream__comments');
|
|
|
|
const commentElement = document.querySelector('.livestream-comment');
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (claimId) {
|
2021-07-15 16:43:28 +02:00
|
|
|
doCommentList(uri, '', 1, 75);
|
2021-04-23 21:59:48 +02:00
|
|
|
doSuperChatList(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);
|
|
|
|
}
|
|
|
|
};
|
2021-04-23 21:59:48 +02:00
|
|
|
}, [claimId, uri, doCommentList, doSuperChatList, doCommentSocketConnect, doCommentSocketDisconnect]);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2021-07-16 15:55:47 +02:00
|
|
|
const handleScroll = React.useCallback(() => {
|
|
|
|
if (discussionElement) {
|
|
|
|
const negativeCommentHeight = commentElement && -1 * commentElement.offsetHeight;
|
|
|
|
const isAtRecent = negativeCommentHeight && discussionElement.scrollTop >= negativeCommentHeight;
|
2021-06-21 15:51:07 +02:00
|
|
|
|
2021-07-16 15:55:47 +02:00
|
|
|
setScrollBottom(isAtRecent);
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|
2021-07-16 15:55:47 +02:00
|
|
|
}, [commentElement, discussionElement]);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2021-07-16 15:55:47 +02:00
|
|
|
React.useEffect(() => {
|
2021-06-21 15:51:07 +02:00
|
|
|
if (discussionElement) {
|
|
|
|
discussionElement.addEventListener('scroll', handleScroll);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
|
|
|
if (commentsLength > 0) {
|
|
|
|
// Only update comment scroll if the user hasn't scrolled up to view old comments
|
|
|
|
// If they have, do nothing
|
2021-06-18 15:40:52 +02:00
|
|
|
if (!performedInitialScroll) {
|
2021-06-22 16:42:52 +02:00
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
(discussionElement.scrollTop =
|
|
|
|
discussionElement.scrollHeight - discussionElement.offsetHeight + COMMENT_SCROLL_OFFSET),
|
|
|
|
COMMENT_SCROLL_TIMEOUT
|
|
|
|
);
|
2021-06-18 15:40:52 +02:00
|
|
|
setPerformedInitialScroll(true);
|
2021-03-10 19:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 15:51:07 +02:00
|
|
|
return () => discussionElement.removeEventListener('scroll', handleScroll);
|
2021-06-18 15:40:52 +02:00
|
|
|
}
|
2021-07-16 15:55:47 +02:00
|
|
|
}, [commentsLength, discussionElement, handleScroll, performedInitialScroll, setPerformedInitialScroll]);
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2021-08-11 22:58:55 +02:00
|
|
|
// sum total amounts for fiat tips and lbc tips
|
|
|
|
if (superChatsByTipAmount) {
|
|
|
|
let fiatAmount = 0;
|
|
|
|
let LBCAmount = 0;
|
|
|
|
for (const superChat of superChatsByTipAmount) {
|
|
|
|
if (superChat.is_fiat) {
|
|
|
|
fiatAmount = fiatAmount + superChat.support_amount;
|
|
|
|
} else {
|
|
|
|
LBCAmount = LBCAmount + superChat.support_amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
superChatsFiatAmount = fiatAmount;
|
|
|
|
superChatsTotalAmount = LBCAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
let superChatsReversed;
|
|
|
|
// array of superchats organized by fiat or not first, then support amount
|
|
|
|
if (superChatsByTipAmount) {
|
|
|
|
const clonedSuperchats = JSON.parse(JSON.stringify(superChatsByTipAmount));
|
|
|
|
|
|
|
|
// sort by fiat first then by support amount
|
|
|
|
superChatsReversed = clonedSuperchats.sort(function(a, b) {
|
|
|
|
// if both are fiat, organize by support
|
|
|
|
if (a.is_fiat === b.is_fiat) {
|
|
|
|
return b.support_amount - a.support_amount;
|
|
|
|
// otherwise, if they are not both fiat, put the fiat transaction first
|
|
|
|
} else {
|
|
|
|
return (a.is_fiat === b.is_fiat) ? 0 : a.is_fiat ? -1 : 1;
|
|
|
|
}
|
|
|
|
}).reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: implement comment_list --mine in SDK so redux can grab with selectCommentIsMine
|
|
|
|
function isMyComment(channelId: string) {
|
|
|
|
if (myChannels != null && channelId != null) {
|
|
|
|
for (let i = 0; i < myChannels.length; i++) {
|
|
|
|
if (myChannels[i].claim_id === channelId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
if (!claim) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-18 15:40:52 +02:00
|
|
|
function scrollBack() {
|
2021-07-16 15:55:47 +02:00
|
|
|
if (discussionElement) {
|
|
|
|
discussionElement.scrollTop = 0;
|
|
|
|
setScrollBottom(true);
|
|
|
|
}
|
2021-06-18 15:40:52 +02:00
|
|
|
}
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
return (
|
2021-04-23 21:59:48 +02:00
|
|
|
<div className="card livestream__discussion">
|
|
|
|
<div className="card__header--between livestream-discussion__header">
|
|
|
|
<div className="livestream-discussion__title">{__('Live discussion')}</div>
|
2021-08-11 22:58:55 +02:00
|
|
|
{(superChatsTotalAmount || 0) > 0 && (
|
2021-04-23 21:59:48 +02:00
|
|
|
<div className="recommended-content__toggles">
|
2021-08-11 22:58:55 +02:00
|
|
|
|
|
|
|
{/* the superchats in chronological order button */}
|
2021-04-23 21:59:48 +02:00
|
|
|
<Button
|
|
|
|
className={classnames('button-toggle', {
|
|
|
|
'button-toggle--active': viewMode === VIEW_MODE_CHAT,
|
|
|
|
})}
|
|
|
|
label={__('Chat')}
|
2021-08-11 22:58:55 +02:00
|
|
|
onClick={function() {
|
|
|
|
setViewMode(VIEW_MODE_CHAT);
|
|
|
|
const livestreamCommentsDiv = document.getElementsByClassName('livestream__comments')[0];
|
|
|
|
const divHeight = livestreamCommentsDiv.scrollHeight;
|
|
|
|
livestreamCommentsDiv.scrollTop = divHeight;
|
|
|
|
}}
|
2021-04-23 21:59:48 +02:00
|
|
|
/>
|
|
|
|
|
2021-08-11 22:58:55 +02:00
|
|
|
{/* the button to show superchats listed by most to least support amount */}
|
2021-04-23 21:59:48 +02:00
|
|
|
<Button
|
|
|
|
className={classnames('button-toggle', {
|
|
|
|
'button-toggle--active': viewMode === VIEW_MODE_SUPER_CHAT,
|
|
|
|
})}
|
|
|
|
label={
|
|
|
|
<>
|
2021-08-11 22:58:55 +02:00
|
|
|
<CreditAmount amount={superChatsTotalAmount || 0} size={8} /> /
|
|
|
|
<CreditAmount amount={superChatsFiatAmount || 0} size={8} isFiat /> {' '}{__('Tipped')}
|
2021-04-23 21:59:48 +02:00
|
|
|
</>
|
|
|
|
}
|
2021-08-11 22:58:55 +02:00
|
|
|
onClick={function() {
|
|
|
|
setViewMode(VIEW_MODE_SUPER_CHAT);
|
|
|
|
const livestreamCommentsDiv = document.getElementsByClassName('livestream__comments')[0];
|
|
|
|
const divHeight = livestreamCommentsDiv.scrollHeight;
|
|
|
|
livestreamCommentsDiv.scrollTop = divHeight * -1;
|
|
|
|
}}
|
2021-04-23 21:59:48 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<>
|
2021-08-11 22:58:55 +02:00
|
|
|
{fetchingComments && !commentsByChronologicalOrder && (
|
2021-04-23 21:59:48 +02:00
|
|
|
<div className="main--empty">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div ref={commentsRef} className="livestream__comments-wrapper">
|
2021-08-11 22:58:55 +02:00
|
|
|
{viewMode === VIEW_MODE_CHAT && superChatsByTipAmount && (superChatsTotalAmount || 0) > 0 && (
|
2021-04-23 21:59:48 +02:00
|
|
|
<div className="livestream-superchats__wrapper">
|
|
|
|
<div className="livestream-superchats__inner">
|
2021-08-11 22:58:55 +02:00
|
|
|
{superChatsByTipAmount.map((superChat: Comment) => (
|
2021-04-23 21:59:48 +02:00
|
|
|
<Tooltip key={superChat.comment_id} label={superChat.comment}>
|
|
|
|
<div className="livestream-superchat">
|
|
|
|
<div className="livestream-superchat__thumbnail">
|
|
|
|
<ChannelThumbnail uri={superChat.channel_url} xsmall />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="livestream-superchat__info">
|
|
|
|
<UriIndicator uri={superChat.channel_url} link />
|
|
|
|
<CreditAmount
|
|
|
|
size={10}
|
|
|
|
className="livestream-superchat__amount-large"
|
|
|
|
amount={superChat.support_amount}
|
2021-07-06 22:28:29 +02:00
|
|
|
isFiat={superChat.is_fiat}
|
2021-04-23 21:59:48 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Tooltip>
|
2021-03-10 19:34:21 +01:00
|
|
|
))}
|
|
|
|
</div>
|
2021-04-23 21:59:48 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2021-08-11 22:58:55 +02:00
|
|
|
{/* top to bottom comment display */}
|
|
|
|
{!fetchingComments && commentsByChronologicalOrder.length > 0 ? (
|
2021-04-23 21:59:48 +02:00
|
|
|
<div className="livestream__comments">
|
2021-08-11 22:58:55 +02:00
|
|
|
{viewMode === VIEW_MODE_CHAT && commentsToDisplay.map((comment) => (
|
|
|
|
<LivestreamComment
|
|
|
|
key={comment.comment_id}
|
|
|
|
uri={uri}
|
|
|
|
authorUri={comment.channel_url}
|
|
|
|
commentId={comment.comment_id}
|
|
|
|
message={comment.comment}
|
|
|
|
supportAmount={comment.support_amount}
|
|
|
|
isFiat={comment.is_fiat}
|
|
|
|
commentIsMine={comment.channel_id && isMyComment(comment.channel_id)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
|
|
|
{viewMode === VIEW_MODE_SUPER_CHAT && superChatsReversed && superChatsReversed.map((comment) => (
|
2021-04-23 21:59:48 +02:00
|
|
|
<LivestreamComment
|
|
|
|
key={comment.comment_id}
|
|
|
|
uri={uri}
|
|
|
|
authorUri={comment.channel_url}
|
|
|
|
commentId={comment.comment_id}
|
|
|
|
message={comment.comment}
|
|
|
|
supportAmount={comment.support_amount}
|
2021-07-06 22:28:29 +02:00
|
|
|
isFiat={comment.is_fiat}
|
2021-05-04 17:08:36 +02:00
|
|
|
commentIsMine={comment.channel_id && isMyComment(comment.channel_id)}
|
2021-04-23 21:59:48 +02:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="main--empty" style={{ flex: 1 }} />
|
|
|
|
)}
|
2021-03-10 19:34:21 +01:00
|
|
|
|
2021-06-21 15:51:07 +02:00
|
|
|
{!scrollBottom && (
|
2021-06-18 15:40:52 +02:00
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
className="livestream__comments-scroll__down"
|
|
|
|
label={__('Recent Comments')}
|
2021-07-16 15:55:47 +02:00
|
|
|
onClick={scrollBack}
|
2021-06-18 15:40:52 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2021-03-10 19:34:21 +01:00
|
|
|
<div className="livestream__comment-create">
|
|
|
|
<CommentCreate livestream bottom embed={embed} uri={uri} />
|
|
|
|
</div>
|
2021-04-23 21:59:48 +02:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
</div>
|
2021-03-10 19:34:21 +01:00
|
|
|
);
|
|
|
|
}
|