2020-08-24 19:35:21 +02:00
|
|
|
// @flow
|
2021-11-03 13:58:27 +01:00
|
|
|
import 'scss/component/_comments.scss';
|
2020-08-25 21:54:06 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2021-10-13 14:59:32 +02:00
|
|
|
import Button from 'component/button';
|
2021-10-13 22:28:19 +02:00
|
|
|
import Comment from 'component/comment';
|
|
|
|
import React from 'react';
|
2021-07-15 16:43:28 +02:00
|
|
|
import Spinner from 'component/spinner';
|
2020-08-24 19:35:21 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
claimIsMine: boolean,
|
2021-11-03 13:58:27 +01:00
|
|
|
fetchedReplies: Array<Comment>,
|
|
|
|
hasMore: boolean,
|
|
|
|
isFetchingByParentId: { [string]: boolean },
|
2021-07-15 16:43:28 +02:00
|
|
|
linkedCommentId?: string,
|
2021-08-04 17:01:31 +02:00
|
|
|
numDirectReplies: number, // Total replies for parentId as reported by 'comment[replies]'. Includes blocked items.
|
2021-11-03 13:58:27 +01:00
|
|
|
parentId: string,
|
|
|
|
resolvedReplies: Array<Comment>,
|
2021-08-27 12:29:58 +02:00
|
|
|
supportDisabled: boolean,
|
2021-11-03 13:58:27 +01:00
|
|
|
threadDepth: number,
|
|
|
|
uri: string,
|
|
|
|
userCanComment: boolean,
|
2021-10-13 22:28:19 +02:00
|
|
|
doResolveUris: (Array<string>) => void,
|
|
|
|
onShowMore?: () => void,
|
2020-08-24 19:35:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function CommentsReplies(props: Props) {
|
2021-07-15 16:43:28 +02:00
|
|
|
const {
|
|
|
|
claimIsMine,
|
2021-11-03 13:58:27 +01:00
|
|
|
fetchedReplies,
|
|
|
|
hasMore,
|
|
|
|
isFetchingByParentId,
|
2021-07-15 16:43:28 +02:00
|
|
|
linkedCommentId,
|
|
|
|
numDirectReplies,
|
2021-11-03 13:58:27 +01:00
|
|
|
parentId,
|
|
|
|
resolvedReplies,
|
2021-08-27 12:29:58 +02:00
|
|
|
supportDisabled,
|
2021-11-03 13:58:27 +01:00
|
|
|
threadDepth,
|
|
|
|
uri,
|
|
|
|
userCanComment,
|
2021-10-13 22:28:19 +02:00
|
|
|
doResolveUris,
|
|
|
|
onShowMore,
|
2021-07-15 16:43:28 +02:00
|
|
|
} = props;
|
|
|
|
|
2020-10-07 21:14:52 +02:00
|
|
|
const [isExpanded, setExpanded] = React.useState(true);
|
2021-10-14 13:54:07 +02:00
|
|
|
const [commentsToDisplay, setCommentsToDisplay] = React.useState(fetchedReplies);
|
2021-11-03 13:58:27 +01:00
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
const isResolvingReplies = fetchedReplies && resolvedReplies.length !== fetchedReplies.length;
|
2021-10-14 13:24:21 +02:00
|
|
|
const alreadyResolved = !isResolvingReplies && resolvedReplies.length !== 0;
|
2021-10-14 13:54:07 +02:00
|
|
|
const canDisplayComments = commentsToDisplay && commentsToDisplay.length === fetchedReplies.length;
|
2021-10-10 14:23:03 +02:00
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
// Batch resolve comment channel urls
|
|
|
|
React.useEffect(() => {
|
2021-10-14 13:24:21 +02:00
|
|
|
if (!fetchedReplies || alreadyResolved) return;
|
2021-10-10 14:23:03 +02:00
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
const urisToResolve = [];
|
2021-11-03 13:58:27 +01:00
|
|
|
fetchedReplies.forEach(({ channel_url }) => channel_url !== undefined && urisToResolve.push(channel_url));
|
2021-10-10 14:23:03 +02:00
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
if (urisToResolve.length > 0) doResolveUris(urisToResolve);
|
2021-10-14 13:24:21 +02:00
|
|
|
}, [alreadyResolved, doResolveUris, fetchedReplies]);
|
2020-08-24 19:35:21 +02:00
|
|
|
|
2021-10-14 13:54:07 +02:00
|
|
|
// 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]);
|
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
return !numDirectReplies ? null : (
|
2021-11-03 13:58:27 +01:00
|
|
|
<div className="commentReplies__container">
|
2021-10-13 22:28:19 +02:00
|
|
|
{!isExpanded ? (
|
|
|
|
<div className="comment__actions--nested">
|
|
|
|
<Button
|
|
|
|
className="comment__action"
|
|
|
|
label={__('Show Replies')}
|
|
|
|
onClick={() => setExpanded(!isExpanded)}
|
|
|
|
icon={isExpanded ? ICONS.UP : ICONS.DOWN}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="comment__replies">
|
|
|
|
<Button className="comment__threadline" aria-label="Hide Replies" onClick={() => setExpanded(false)} />
|
2020-08-24 19:35:21 +02:00
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
<ul className="comments--replies">
|
|
|
|
{!isResolvingReplies &&
|
2021-10-14 13:54:07 +02:00
|
|
|
commentsToDisplay &&
|
|
|
|
commentsToDisplay.length > 0 &&
|
2021-11-03 13:58:27 +01:00
|
|
|
commentsToDisplay.map((comment: Comment) => (
|
2021-10-13 22:28:19 +02:00
|
|
|
<Comment
|
|
|
|
claimIsMine={claimIsMine}
|
2021-11-03 13:58:27 +01:00
|
|
|
comment={comment}
|
2021-10-13 22:28:19 +02:00
|
|
|
commentingEnabled={userCanComment}
|
2021-11-03 13:58:27 +01:00
|
|
|
key={comment.comment_id}
|
|
|
|
linkedCommentId={linkedCommentId}
|
2021-10-13 22:28:19 +02:00
|
|
|
supportDisabled={supportDisabled}
|
2021-11-03 13:58:27 +01:00
|
|
|
threadDepth={threadDepth}
|
|
|
|
uri={uri}
|
2021-10-13 22:28:19 +02:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-11-03 13:58:27 +01:00
|
|
|
|
2021-10-13 22:28:19 +02:00
|
|
|
{isExpanded && fetchedReplies && hasMore && (
|
|
|
|
<div className="comment__actions--nested">
|
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
label={__('Show more')}
|
|
|
|
onClick={() => onShowMore && onShowMore()}
|
|
|
|
className="button--uri-indicator"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-11-03 13:58:27 +01:00
|
|
|
|
2021-10-14 13:54:07 +02:00
|
|
|
{(isFetchingByParentId[parentId] || isResolvingReplies || !canDisplayComments) && (
|
2021-11-03 13:58:27 +01:00
|
|
|
<div className="comment__actions--nested">
|
|
|
|
<Spinner type="small" />
|
2021-10-13 22:28:19 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-08-24 19:35:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CommentsReplies;
|