// @flow import React from 'react'; import classnames from 'classnames'; import Spinner from 'component/spinner'; import CommentCreate from 'component/commentCreate'; 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'; import * as ICONS from 'constants/icons'; import OptimizedImage from 'component/optimizedImage'; import { parseSticker } from 'util/comments'; type Props = { uri: string, claim: ?StreamClaim, activeViewers: number, embed?: boolean, doCommentSocketConnect: (string, string) => void, doCommentSocketDisconnect: (string) => void, doCommentList: (string, string, number, number) => void, comments: Array, pinnedComments: Array, fetchingComments: boolean, doSuperChatList: (string) => void, superChats: Array, myChannelIds: ?Array, }; const VIEW_MODE_CHAT = 'view_chat'; const VIEW_MODE_SUPER_CHAT = 'view_superchat'; const COMMENT_SCROLL_TIMEOUT = 25; export default function LivestreamComments(props: Props) { const { claim, uri, embed, doCommentSocketConnect, doCommentSocketDisconnect, comments: commentsByChronologicalOrder, pinnedComments, doCommentList, fetchingComments, doSuperChatList, myChannelIds, superChats: superChatsByTipAmount, } = props; let superChatsFiatAmount, superChatsLBCAmount, superChatsTotalAmount, hasSuperChats; const commentsRef = React.createRef(); const [viewMode, setViewMode] = React.useState(VIEW_MODE_CHAT); const [scrollPos, setScrollPos] = React.useState(0); const [showPinned, setShowPinned] = React.useState(true); const claimId = claim && claim.claim_id; const commentsLength = commentsByChronologicalOrder && commentsByChronologicalOrder.length; // which kind of superchat to display, either const commentsToDisplay = viewMode === VIEW_MODE_CHAT ? commentsByChronologicalOrder : superChatsByTipAmount; const stickerSuperChats = superChatsByTipAmount && superChatsByTipAmount.filter(({ comment }) => Boolean(parseSticker(comment))); const discussionElement = document.querySelector('.livestream__comments'); const pinnedComment = pinnedComments.length > 0 ? pinnedComments[0] : null; const restoreScrollPos = React.useCallback(() => { if (discussionElement) { discussionElement.scrollTop = 0; } }, [discussionElement]); React.useEffect(() => { if (claimId) { doCommentList(uri, '', 1, 75); doSuperChatList(uri); doCommentSocketConnect(uri, claimId); } return () => { if (claimId) { doCommentSocketDisconnect(claimId); } }; }, [claimId, uri, doCommentList, doSuperChatList, doCommentSocketConnect, doCommentSocketDisconnect]); // Register scroll handler (TODO: Should throttle/debounce) React.useEffect(() => { function handleScroll() { if (discussionElement) { const scrollTop = discussionElement.scrollTop; if (scrollTop !== scrollPos) { setScrollPos(scrollTop); } } } if (discussionElement) { discussionElement.addEventListener('scroll', handleScroll); return () => discussionElement.removeEventListener('scroll', handleScroll); } }, [discussionElement, scrollPos]); // Retain scrollPos=0 when receiving new messages. React.useEffect(() => { if (discussionElement && commentsLength > 0) { // Only update comment scroll if the user hasn't scrolled up to view old comments if (scrollPos >= 0) { // +ve scrollPos: not scrolled (Usually, there'll be a few pixels beyond 0). // -ve scrollPos: user scrolled. const timer = setTimeout(() => { // Use a timer here to ensure we reset after the new comment has been rendered. discussionElement.scrollTop = 0; }, COMMENT_SCROLL_TIMEOUT); return () => clearTimeout(timer); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [commentsLength]); // (Just respond to 'commentsLength' updates and nothing else) // 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; superChatsLBCAmount = LBCAmount; superChatsTotalAmount = superChatsFiatAmount + superChatsLBCAmount; hasSuperChats = (superChatsTotalAmount || 0) > 0; } let superChatsReversed; // array of superchats organized by fiat or not first, then support amount if (superChatsByTipAmount) { const clonedSuperchats = JSON.parse(JSON.stringify(superChatsByTipAmount)); // for top to bottom display, oldest superchat on top most recent on bottom superChatsReversed = clonedSuperchats.sort((a, b) => { return b.timestamp - a.timestamp; }); } // todo: implement comment_list --mine in SDK so redux can grab with selectCommentIsMine function isMyComment(channelId: string) { return myChannelIds ? myChannelIds.includes(channelId) : false; } if (!claim) { return null; } function getStickerUrl(comment: string) { const stickerFromComment = parseSticker(comment); return stickerFromComment && stickerFromComment.url; } return (
{__('Live discussion')}
{hasSuperChats && (
{/* the superchats in chronological order button */}
)}
<> {fetchingComments && !commentsByChronologicalOrder && (
)}
{viewMode === VIEW_MODE_CHAT && superChatsByTipAmount && hasSuperChats && (
{superChatsByTipAmount.map((superChat: Comment) => { const isSticker = stickerSuperChats && stickerSuperChats.includes(superChat); const SuperChatWrapper = !isSticker ? ({ children }) => {children} : ({ children }) => <>{children}; return (
{stickerSuperChats.includes(superChat) && getStickerUrl(superChat.comment) && (
)}
); })}
)} {pinnedComment && showPinned && viewMode === VIEW_MODE_CHAT && (
)} {/* top to bottom comment display */} {!fetchingComments && commentsByChronologicalOrder.length > 0 ? (
{viewMode === VIEW_MODE_CHAT && commentsToDisplay.map((comment) => ( ))} {/* listing comments on top of eachother */} {viewMode === VIEW_MODE_SUPER_CHAT && superChatsReversed && superChatsReversed.map((comment) => ( ))}
) : (
)} {scrollPos < 0 && viewMode === VIEW_MODE_CHAT && (
); }