// @flow import * as ICONS from 'constants/icons'; import Button from 'component/button'; import Comment from 'component/comment'; import React from 'react'; import Spinner from 'component/spinner'; type Props = { fetchedReplies: Array, resolvedReplies: Array, uri: string, parentId: string, claimIsMine: boolean, myChannelIds: ?Array, linkedCommentId?: string, userCanComment: boolean, threadDepth: number, numDirectReplies: number, // Total replies for parentId as reported by 'comment[replies]'. Includes blocked items. isFetchingByParentId: { [string]: boolean }, hasMore: boolean, supportDisabled: boolean, doResolveUris: (Array) => void, onShowMore?: () => void, }; function CommentsReplies(props: Props) { const { uri, parentId, fetchedReplies, resolvedReplies, claimIsMine, myChannelIds, linkedCommentId, userCanComment, threadDepth, numDirectReplies, isFetchingByParentId, hasMore, supportDisabled, doResolveUris, onShowMore, } = props; const [isExpanded, setExpanded] = React.useState(true); const [commentsToDisplay, setCommentsToDisplay] = React.useState(fetchedReplies); const isResolvingReplies = fetchedReplies && resolvedReplies.length !== fetchedReplies.length; const alreadyResolved = !isResolvingReplies && resolvedReplies.length !== 0; const canDisplayComments = commentsToDisplay && commentsToDisplay.length === fetchedReplies.length; // Batch resolve comment channel urls React.useEffect(() => { if (!fetchedReplies || alreadyResolved) return; const urisToResolve = []; fetchedReplies.map(({ channel_url }) => channel_url !== undefined && urisToResolve.push(channel_url)); if (urisToResolve.length > 0) doResolveUris(urisToResolve); }, [alreadyResolved, doResolveUris, fetchedReplies]); // Wait to only display topLevelComments after resolved or else // other components will try to resolve again, like channelThumbnail React.useEffect(() => { if (!isResolvingReplies) setCommentsToDisplay(fetchedReplies); }, [isResolvingReplies, fetchedReplies]); return !numDirectReplies ? null : (
{!isExpanded ? (
) : (
)} {isExpanded && fetchedReplies && hasMore && (
)} {(isFetchingByParentId[parentId] || isResolvingReplies || !canDisplayComments) && (
)}
); } export default CommentsReplies;