2020-08-24 19:35:21 +02:00
|
|
|
// @flow
|
2020-08-25 21:54:06 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-08-24 19:35:21 +02:00
|
|
|
import Button from 'component/button';
|
2021-10-10 13:41:51 +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 = {
|
2021-10-10 14:23:03 +02:00
|
|
|
fetchedReplies: Array<Comment>,
|
|
|
|
resolvedReplies: Array<Comment>,
|
2020-08-24 19:35:21 +02:00
|
|
|
uri: string,
|
2021-07-15 16:43:28 +02:00
|
|
|
parentId: string,
|
2020-08-24 19:35:21 +02:00
|
|
|
claimIsMine: boolean,
|
|
|
|
myChannels: ?Array<ChannelClaim>,
|
2021-07-15 16:43:28 +02:00
|
|
|
linkedCommentId?: string,
|
2021-10-10 13:41:51 +02:00
|
|
|
userCanComment: boolean,
|
2020-10-07 21:14:52 +02:00
|
|
|
threadDepth: number,
|
2021-08-04 17:01:31 +02:00
|
|
|
numDirectReplies: number, // Total replies for parentId as reported by 'comment[replies]'. Includes blocked items.
|
2021-07-15 16:43:28 +02:00
|
|
|
isFetchingByParentId: { [string]: boolean },
|
2021-08-04 17:01:31 +02:00
|
|
|
hasMore: boolean,
|
2021-08-27 12:29:58 +02:00
|
|
|
supportDisabled: boolean,
|
2021-10-10 14:23:03 +02:00
|
|
|
doResolveUris: (Array<string>) => void,
|
2021-10-10 13:41:51 +02:00
|
|
|
onShowMore?: () => void,
|
2020-08-24 19:35:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function CommentsReplies(props: Props) {
|
2021-07-15 16:43:28 +02:00
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
parentId,
|
|
|
|
fetchedReplies,
|
2021-10-10 14:23:03 +02:00
|
|
|
resolvedReplies,
|
2021-07-15 16:43:28 +02:00
|
|
|
claimIsMine,
|
|
|
|
myChannels,
|
|
|
|
linkedCommentId,
|
2021-10-10 13:41:51 +02:00
|
|
|
userCanComment,
|
2021-07-15 16:43:28 +02:00
|
|
|
threadDepth,
|
|
|
|
numDirectReplies,
|
|
|
|
isFetchingByParentId,
|
2021-08-04 17:01:31 +02:00
|
|
|
hasMore,
|
2021-08-27 12:29:58 +02:00
|
|
|
supportDisabled,
|
2021-10-10 14:23:03 +02:00
|
|
|
doResolveUris,
|
2021-10-10 13:41:51 +02:00
|
|
|
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-10 14:23:03 +02:00
|
|
|
const isResolvingReplies = fetchedReplies && resolvedReplies.length !== fetchedReplies.length;
|
|
|
|
|
|
|
|
// Batch resolve comment channel urls
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!fetchedReplies) return;
|
|
|
|
|
|
|
|
const urisToResolve = [];
|
|
|
|
fetchedReplies.map(({ channel_url }) => channel_url !== undefined && urisToResolve.push(channel_url));
|
|
|
|
|
|
|
|
if (urisToResolve.length > 0) doResolveUris(urisToResolve);
|
|
|
|
}, [fetchedReplies, doResolveUris]);
|
2020-08-24 19:35:21 +02:00
|
|
|
|
2021-10-10 13:41:51 +02:00
|
|
|
return !numDirectReplies ? null : (
|
|
|
|
<div className="comment__replies-container">
|
|
|
|
{!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-10 13:41:51 +02:00
|
|
|
<ul className="comments--replies">
|
2021-10-10 14:23:03 +02:00
|
|
|
{!isResolvingReplies &&
|
|
|
|
resolvedReplies.length > 0 &&
|
|
|
|
resolvedReplies.map((comment) => (
|
2021-10-10 13:41:51 +02:00
|
|
|
<Comment
|
|
|
|
threadDepth={threadDepth}
|
|
|
|
uri={uri}
|
|
|
|
authorUri={comment.channel_url}
|
|
|
|
author={comment.channel_name}
|
|
|
|
claimId={comment.claim_id}
|
|
|
|
commentId={comment.comment_id}
|
|
|
|
key={comment.comment_id}
|
|
|
|
message={comment.comment}
|
|
|
|
timePosted={comment.timestamp * 1000}
|
|
|
|
claimIsMine={claimIsMine}
|
|
|
|
commentIsMine={
|
|
|
|
comment.channel_id &&
|
|
|
|
myChannels &&
|
|
|
|
myChannels.some(({ claim_id }) => claim_id === comment.channel_id)
|
|
|
|
}
|
|
|
|
linkedCommentId={linkedCommentId}
|
|
|
|
commentingEnabled={userCanComment}
|
|
|
|
supportAmount={comment.support_amount}
|
|
|
|
numDirectReplies={comment.replies}
|
|
|
|
isModerator={comment.is_moderator}
|
|
|
|
isGlobalMod={comment.is_global_mod}
|
|
|
|
supportDisabled={supportDisabled}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isExpanded && fetchedReplies && hasMore && (
|
|
|
|
<div className="comment__actions--nested">
|
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
label={__('Show more')}
|
|
|
|
onClick={() => onShowMore && onShowMore()}
|
|
|
|
className="button--uri-indicator"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2021-10-10 14:23:03 +02:00
|
|
|
{(isFetchingByParentId[parentId] || isResolvingReplies) && (
|
2021-10-10 13:41:51 +02:00
|
|
|
<div className="comment__replies-container">
|
2020-09-11 19:51:31 +02:00
|
|
|
<div className="comment__actions--nested">
|
2021-10-10 13:41:51 +02:00
|
|
|
<Spinner type="small" />
|
2021-07-15 16:43:28 +02:00
|
|
|
</div>
|
2021-10-10 13:41:51 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-08-24 19:35:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CommentsReplies;
|