// @flow import React from 'react'; import Comment from 'component/comment'; import Button from 'component/button'; import * as ICONS from 'constants/icons'; import CommentCreate from 'component/commentCreate'; type Props = { comments: Array, uri: string, claimIsMine: boolean, myChannels: ?Array, linkedComment?: Comment, parentId: string, commentingEnabled: boolean, }; function CommentsReplies(props: Props) { const { uri, comments, claimIsMine, myChannels, linkedComment, parentId, commentingEnabled } = props; const [isReplying, setReplying] = React.useState(false); const [isExpanded, setExpanded] = React.useState(false); const [start, setStart] = React.useState(0); const [end, setEnd] = React.useState(9); const sortedComments = comments ? [...comments].reverse() : []; const numberOfComments = comments ? comments.length : 0; const showMore = () => { if (start > 0) { setStart(0); } else { setEnd(numberOfComments); } }; const linkedCommentId = linkedComment ? linkedComment.comment_id : ''; const commentsIndexOfLInked = comments && sortedComments.findIndex(e => e.comment_id === linkedCommentId); // todo: implement comment_list --mine in SDK so redux can grab with selectCommentIsMine const 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; }; const handleCommentDone = () => { if (!isExpanded) { setExpanded(true); setStart(numberOfComments || 0); } setEnd(numberOfComments + 1); setReplying(false); }; React.useEffect(() => { if ( setStart && setEnd && setExpanded && linkedCommentId && Number.isInteger(commentsIndexOfLInked) && commentsIndexOfLInked > -1 ) { setStart(commentsIndexOfLInked); setEnd(commentsIndexOfLInked + 1); setExpanded(true); } }, [setStart, setEnd, setExpanded, linkedCommentId, commentsIndexOfLInked]); const displayedComments = sortedComments.slice(start, end); return (
  • {comments && displayedComments && isExpanded && (
    {!isReplying && (
    )} {isExpanded && comments && (end < numberOfComments || start > 0) && (
    )} {isReplying ? ( handleCommentDone()} onCancelReplying={() => setReplying(false)} /> ) : ( '' )}
  • ); } export default CommentsReplies;