import React from 'react'; import PropTypes from 'prop-types'; import {StreamError} from './StreamError'; import Comment from '../components/Comment'; import SuspendedAccount from './SuspendedAccount'; import Slot from 'coral-framework/components/Slot'; import InfoBox from 'talk-plugin-infobox/InfoBox'; import {can} from 'coral-framework/services/perms'; import {ModerationLink} from 'talk-plugin-moderation'; import RestrictedMessageBox from 'coral-framework/components/RestrictedMessageBox'; import t, {timeago} from 'coral-framework/services/i18n'; import {getSlotComponents} from 'coral-framework/helpers/plugins'; import CommentBox from 'talk-plugin-commentbox/CommentBox'; import QuestionBox from 'talk-plugin-questionbox/QuestionBox'; import {isCommentActive} from 'coral-framework/utils'; import {Button, TabBar, Tab, TabCount, TabContent, TabPane} from 'coral-ui'; import cn from 'classnames'; import {getTopLevelParent, attachCommentToParent} from '../graphql/utils'; import AllCommentsPane from './AllCommentsPane'; import AutomaticAssetClosure from '../containers/AutomaticAssetClosure'; import styles from './Stream.css'; class Stream extends React.Component { constructor(props) { super(props); this.state = { keepCommentBox: false, }; } componentWillReceiveProps(next) { // Keep comment box when user was live suspended, banned, ... if (!this.userIsDegraged(this.props) && this.userIsDegraged(next)) { this.setState({keepCommentBox: true}); } this.fallbackAllTab(next); } componentDidMount() { this.fallbackAllTab(); } fallbackAllTab(props = this.props) { if (props.activeStreamTab !== 'all') { const slotPlugins = this.getSlotComponents('streamTabs', props).map((c) => c.talkPluginName); if (slotPlugins.indexOf(props.activeStreamTab) === -1) { props.setActiveStreamTab('all'); } } } getSlotProps({data, root, root: {asset}} = this.props) { return {data, root, asset}; } getSlotComponents(slot, props = this.props) { return getSlotComponents(slot, props.reduxState, this.getSlotProps(props)); } setActiveReplyBox = (id) => { if (!this.props.auth.user) { this.props.showSignInDialog(); } else { this.props.setActiveReplyBox(id); } }; userIsDegraged({auth: {user}} = this.props) { return !can(user, 'INTERACT_WITH_COMMUNITY'); } render() { const { data, root, activeReplyBox, setActiveReplyBox, appendItemArray, commentClassNames, root: {asset, asset: {comment, comments, totalCommentCount}, me}, postComment, addNotification, editComment, postFlag, postDontAgree, deleteAction, showSignInDialog, updateItem, ignoreUser, activeStreamTab, setActiveStreamTab, loadNewReplies, loadMoreComments, viewAllComments, auth: {loggedIn, user}, editName } = this.props; const {keepCommentBox} = this.state; const open = !asset.isClosed; // even though the permalinked comment is the highlighted one, we're displaying its parent + replies let highlightedComment = comment && getTopLevelParent(comment); if (highlightedComment) { // Inactive comments can be viewed by moderators and admins (e.g. using permalinks). const isInactive = !isCommentActive(comment.status); if (comment.parent && isInactive) { // the highlighted comment is not active and as such not in the replies, so we // attach it to the right parent. highlightedComment = attachCommentToParent(highlightedComment, comment); } } const banned = user && user.status === 'BANNED'; const temporarilySuspended = user && user.suspension.until && new Date(user.suspension.until) > new Date(); const commentIsIgnored = (comment) => { return ( me && me.ignoredUsers && me.ignoredUsers.find((u) => u.id === comment.user.id) ); }; const showCommentBox = loggedIn && ((!banned && !temporarilySuspended && !highlightedComment) || keepCommentBox); const slotProps = this.getSlotProps(); if (!comment && !comments) { console.error('Talk: No comments came back from the graph given that query. Please, check the query params.'); return ; } return (
{comment && } {open ?
{!banned && temporarilySuspended && {t( 'stream.temporarily_suspended', root.settings.organizationName, timeago(user.suspension.until) )} } {banned && } {showCommentBox && }
:

{asset.settings.closedMessage}

} {loggedIn && ( )} {/* the highlightedComment is isolated after the user followed a permalink */} {highlightedComment ? (
) :
{this.getSlotComponents('streamTabs').map((PluginComponent) => ( ))} All Comments {totalCommentCount} {this.getSlotComponents('streamTabPanes').map((PluginComponent) => ( ))}
}
); } } Stream.propTypes = { addNotification: PropTypes.func.isRequired, postComment: PropTypes.func.isRequired, // dispatch action to ignore another user ignoreUser: React.PropTypes.func, // edit a comment, passed (id, asset_id, { body }) editComment: React.PropTypes.func }; export default Stream;