lbry-desktop/ui/component/commentsReplies/view.jsx

121 lines
3.9 KiB
React
Raw Permalink Normal View History

2020-08-24 19:35:21 +02:00
// @flow
2020-08-25 21:54:06 +02:00
import * as ICONS from 'constants/icons';
import Button from 'component/button';
2022-02-09 16:27:11 +01:00
import Comment from 'component/comment';
import React from 'react';
import Spinner from 'component/spinner';
2020-08-24 19:35:21 +02:00
type Props = {
2022-02-09 16:27:11 +01:00
fetchedReplies: Array<Comment>,
resolvedReplies: Array<Comment>,
2020-08-24 19:35:21 +02:00
uri: string,
parentId: string,
2020-08-24 19:35:21 +02:00
claimIsMine: boolean,
linkedCommentId?: string,
2022-02-09 16:27:11 +01:00
userCanComment: boolean,
2020-10-07 21:14:52 +02:00
threadDepth: number,
numDirectReplies: number, // Total replies for parentId as reported by 'comment[replies]'. Includes blocked items.
isFetchingByParentId: { [string]: boolean },
hasMore: boolean,
2021-08-27 12:29:58 +02:00
supportDisabled: boolean,
2022-02-09 16:27:11 +01:00
doResolveUris: (Array<string>) => void,
onShowMore?: () => void,
2020-08-24 19:35:21 +02:00
};
function CommentsReplies(props: Props) {
const {
uri,
parentId,
fetchedReplies,
2022-02-09 16:27:11 +01:00
resolvedReplies,
claimIsMine,
linkedCommentId,
2022-02-09 16:27:11 +01:00
userCanComment,
threadDepth,
numDirectReplies,
isFetchingByParentId,
hasMore,
2021-08-27 12:29:58 +02:00
supportDisabled,
2022-02-09 16:27:11 +01:00
doResolveUris,
onShowMore,
} = props;
2020-10-07 21:14:52 +02:00
const [isExpanded, setExpanded] = React.useState(true);
2022-02-09 16:27:11 +01:00
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;
2021-10-10 14:23:03 +02:00
2022-02-09 16:27:11 +01:00
// Batch resolve comment channel urls
React.useEffect(() => {
if (!fetchedReplies || alreadyResolved) return;
2021-10-10 14:23:03 +02:00
2022-02-09 16:27:11 +01:00
const urisToResolve = [];
fetchedReplies.map(({ channel_url }) => channel_url !== undefined && urisToResolve.push(channel_url));
2021-10-10 14:23:03 +02:00
2022-02-09 16:27:11 +01:00
if (urisToResolve.length > 0) doResolveUris(urisToResolve);
}, [alreadyResolved, doResolveUris, fetchedReplies]);
2020-08-24 19:35:21 +02:00
2022-02-09 16:27:11 +01: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]);
2020-08-24 19:35:21 +02:00
2022-02-09 16:27:11 +01: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)} />
<ul className="comments--replies">
{!isResolvingReplies &&
commentsToDisplay &&
commentsToDisplay.length > 0 &&
commentsToDisplay.map((comment) => (
<Comment
key={comment.comment_id}
threadDepth={threadDepth}
uri={uri}
comment={comment}
claimIsMine={claimIsMine}
linkedCommentId={linkedCommentId}
commentingEnabled={userCanComment}
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>
)}
{(isFetchingByParentId[parentId] || isResolvingReplies || !canDisplayComments) && (
<div className="comment__replies-container">
<div className="comment__actions--nested">
2022-02-09 16:27:11 +01:00
<Spinner type="small" />
</div>
2022-02-09 16:27:11 +01:00
</div>
)}
</div>
2020-08-24 19:35:21 +02:00
);
}
export default CommentsReplies;