2019-05-17 20:21:07 +02:00
|
|
|
// @flow
|
2020-10-06 21:35:13 +02:00
|
|
|
import * as REACTION_TYPES from 'constants/reactions';
|
2020-09-30 20:46:17 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-10-06 21:35:13 +02:00
|
|
|
import { SORT_COMMENTS_NEW, SORT_COMMENTS_BEST, SORT_COMMENTS_CONTROVERSIAL } from 'constants/comment';
|
2019-06-27 01:59:27 +02:00
|
|
|
import React, { useEffect } from 'react';
|
2020-10-06 21:35:13 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import CommentView from 'component/comment';
|
2020-05-25 20:45:43 +02:00
|
|
|
import Spinner from 'component/spinner';
|
2020-08-24 19:35:21 +02:00
|
|
|
import Button from 'component/button';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import CommentCreate from 'component/commentCreate';
|
2020-10-01 20:43:44 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-09-30 17:59:05 +02:00
|
|
|
import { ENABLE_COMMENT_REACTIONS } from 'config';
|
2020-10-06 21:35:13 +02:00
|
|
|
import { sortComments } from 'util/comments';
|
2019-05-17 20:21:07 +02:00
|
|
|
|
2019-06-27 01:59:27 +02:00
|
|
|
type Props = {
|
2020-10-06 21:35:13 +02:00
|
|
|
comments: Array<Comment>,
|
2019-06-27 01:59:27 +02:00
|
|
|
fetchComments: string => void,
|
2020-10-06 21:35:13 +02:00
|
|
|
fetchReacts: string => Promise<any>,
|
2019-05-17 20:21:07 +02:00
|
|
|
uri: string,
|
2020-01-30 02:02:21 +01:00
|
|
|
claimIsMine: boolean,
|
|
|
|
myChannels: ?Array<ChannelClaim>,
|
2020-05-25 20:45:43 +02:00
|
|
|
isFetchingComments: boolean,
|
2020-08-24 19:35:21 +02:00
|
|
|
linkedComment: any,
|
2020-09-30 20:46:17 +02:00
|
|
|
totalComments: number,
|
2020-10-02 23:17:12 +02:00
|
|
|
fetchingChannels: boolean,
|
2020-10-06 21:35:13 +02:00
|
|
|
reactionsById: { [string]: { [REACTION_TYPES.LIKE | REACTION_TYPES.DISLIKE]: number } },
|
2019-05-17 20:21:07 +02:00
|
|
|
};
|
|
|
|
|
2019-06-27 01:59:27 +02:00
|
|
|
function CommentList(props: Props) {
|
2020-09-29 16:10:23 +02:00
|
|
|
const {
|
|
|
|
fetchComments,
|
2020-10-01 20:43:44 +02:00
|
|
|
fetchReacts,
|
2020-09-29 16:10:23 +02:00
|
|
|
uri,
|
|
|
|
comments,
|
|
|
|
claimIsMine,
|
|
|
|
myChannels,
|
|
|
|
isFetchingComments,
|
|
|
|
linkedComment,
|
2020-09-30 20:46:17 +02:00
|
|
|
totalComments,
|
2020-10-02 23:17:12 +02:00
|
|
|
fetchingChannels,
|
2020-10-06 21:35:13 +02:00
|
|
|
reactionsById,
|
2020-09-29 16:10:23 +02:00
|
|
|
} = props;
|
2020-10-01 20:43:44 +02:00
|
|
|
const commentRef = React.useRef();
|
2020-10-06 21:35:13 +02:00
|
|
|
const spinnerRef = React.useRef();
|
|
|
|
const [sort, setSort] = usePersistedState('comment-sort', SORT_COMMENTS_BEST);
|
2020-10-01 20:43:44 +02:00
|
|
|
const [activeChannel] = usePersistedState('comment-channel', '');
|
2020-08-24 19:35:21 +02:00
|
|
|
const [start] = React.useState(0);
|
|
|
|
const [end, setEnd] = React.useState(9);
|
2020-10-06 21:35:13 +02:00
|
|
|
// Display comments immediately if not fetching reactions
|
|
|
|
// If not, wait to show comments until reactions are fetched
|
|
|
|
const [readyToDisplayComments, setReadyToDisplayComments] = React.useState(!ENABLE_COMMENT_REACTIONS);
|
2020-10-01 20:43:44 +02:00
|
|
|
const linkedCommentId = linkedComment && linkedComment.comment_id;
|
2020-08-27 06:25:03 +02:00
|
|
|
const hasNoComments = totalComments === 0;
|
2020-08-24 19:35:21 +02:00
|
|
|
const moreBelow = totalComments - end > 0;
|
2020-10-07 22:18:16 +02:00
|
|
|
const isMyComment = (channelId: string): boolean => {
|
2020-01-30 02:02:21 +01:00
|
|
|
if (myChannels != null && channelId != null) {
|
|
|
|
for (let i = 0; i < myChannels.length; i++) {
|
|
|
|
if (myChannels[i].claim_id === channelId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-10-06 21:35:13 +02:00
|
|
|
const handleMoreBelow = React.useCallback(() => {
|
2020-08-24 19:35:21 +02:00
|
|
|
if (moreBelow) {
|
|
|
|
setEnd(end + 10);
|
|
|
|
}
|
2020-10-06 21:35:13 +02:00
|
|
|
}, [end, setEnd, moreBelow]);
|
2020-08-24 19:35:21 +02:00
|
|
|
|
2019-06-27 01:59:27 +02:00
|
|
|
useEffect(() => {
|
|
|
|
fetchComments(uri);
|
|
|
|
}, [fetchComments, uri]);
|
|
|
|
|
2020-10-01 20:43:44 +02:00
|
|
|
useEffect(() => {
|
2020-10-02 23:17:12 +02:00
|
|
|
if (totalComments && ENABLE_COMMENT_REACTIONS && !fetchingChannels) {
|
2020-10-06 21:35:13 +02:00
|
|
|
fetchReacts(uri)
|
|
|
|
.then(() => {
|
|
|
|
setReadyToDisplayComments(true);
|
|
|
|
})
|
|
|
|
.catch(() => setReadyToDisplayComments(true));
|
2020-10-01 20:43:44 +02:00
|
|
|
}
|
2020-10-06 23:34:26 +02:00
|
|
|
}, [fetchReacts, uri, totalComments, activeChannel, fetchingChannels]);
|
2020-09-29 16:10:23 +02:00
|
|
|
|
2020-08-24 19:35:21 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (linkedCommentId && commentRef && commentRef.current) {
|
|
|
|
commentRef.current.scrollIntoView({ block: 'start' });
|
|
|
|
window.scrollBy(0, -100);
|
|
|
|
}
|
|
|
|
}, [linkedCommentId]);
|
2020-02-05 04:55:00 +01:00
|
|
|
|
2020-10-05 23:25:23 +02:00
|
|
|
useEffect(() => {
|
|
|
|
function handleCommentScroll(e) {
|
|
|
|
// $FlowFixMe
|
2020-10-06 21:35:13 +02:00
|
|
|
const rect = spinnerRef.current.getBoundingClientRect();
|
|
|
|
|
|
|
|
const isInViewport =
|
|
|
|
rect.top >= 0 &&
|
|
|
|
rect.left >= 0 &&
|
|
|
|
// $FlowFixMe
|
|
|
|
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
|
|
|
|
// $FlowFixMe
|
|
|
|
rect.right <= (window.innerWidth || document.documentElement.clientWidth);
|
|
|
|
|
|
|
|
if (isInViewport) {
|
2020-10-05 23:25:23 +02:00
|
|
|
handleMoreBelow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 22:33:51 +02:00
|
|
|
if (!isFetchingComments && readyToDisplayComments && moreBelow && spinnerRef && spinnerRef.current) {
|
2020-10-05 23:25:23 +02:00
|
|
|
window.addEventListener('scroll', handleCommentScroll);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => window.removeEventListener('scroll', handleCommentScroll);
|
2020-10-06 22:33:51 +02:00
|
|
|
}, [moreBelow, handleMoreBelow, spinnerRef, isFetchingComments, readyToDisplayComments]);
|
2020-10-05 23:25:23 +02:00
|
|
|
|
2020-10-06 22:13:42 +02:00
|
|
|
function prepareComments(arrayOfComments, linkedComment, isFetchingComments) {
|
2020-08-24 19:35:21 +02:00
|
|
|
let orderedComments = [];
|
2020-02-05 04:55:00 +01:00
|
|
|
|
2020-08-24 19:35:21 +02:00
|
|
|
if (linkedComment) {
|
|
|
|
if (!linkedComment.parent_id) {
|
|
|
|
orderedComments = arrayOfComments.filter(c => c.comment_id !== linkedComment.comment_id);
|
|
|
|
orderedComments.unshift(linkedComment);
|
|
|
|
} else {
|
|
|
|
const parentComment = arrayOfComments.find(c => c.comment_id === linkedComment.parent_id);
|
|
|
|
orderedComments = arrayOfComments.filter(c => c.comment_id !== linkedComment.parent_id);
|
2020-10-06 21:35:13 +02:00
|
|
|
|
|
|
|
if (parentComment) {
|
|
|
|
orderedComments.unshift(parentComment);
|
|
|
|
}
|
2020-08-24 19:35:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
orderedComments = arrayOfComments;
|
|
|
|
}
|
|
|
|
return orderedComments;
|
2020-02-05 04:55:00 +01:00
|
|
|
}
|
|
|
|
|
2020-10-06 21:35:13 +02:00
|
|
|
// Default to newest first for apps that don't have comment reactions
|
2020-10-07 22:18:16 +02:00
|
|
|
const sortedComments = ENABLE_COMMENT_REACTIONS
|
|
|
|
? sortComments({ comments, reactionsById, sort, isMyComment })
|
|
|
|
: comments;
|
2020-10-06 21:35:13 +02:00
|
|
|
const displayedComments = readyToDisplayComments
|
|
|
|
? prepareComments(sortedComments, linkedComment).slice(start, end)
|
|
|
|
: [];
|
|
|
|
|
2019-06-27 01:59:27 +02:00
|
|
|
return (
|
2020-08-24 19:35:21 +02:00
|
|
|
<Card
|
2020-08-31 18:28:28 +02:00
|
|
|
title={
|
|
|
|
totalComments > 0
|
|
|
|
? totalComments === 1
|
|
|
|
? __('1 comment')
|
|
|
|
: __('%total_comments% comments', { total_comments: totalComments })
|
|
|
|
: __('Leave a comment')
|
|
|
|
}
|
2020-10-02 06:47:54 +02:00
|
|
|
titleActions={
|
2020-10-06 21:35:13 +02:00
|
|
|
<>
|
|
|
|
{totalComments > 1 && ENABLE_COMMENT_REACTIONS && (
|
|
|
|
<span className="comment__sort">
|
|
|
|
<Button
|
|
|
|
button="alt"
|
2020-10-07 13:16:13 +02:00
|
|
|
label={__('Best')}
|
2020-10-06 23:34:26 +02:00
|
|
|
icon={ICONS.FIRE}
|
2020-10-06 21:35:13 +02:00
|
|
|
onClick={() => setSort(SORT_COMMENTS_BEST)}
|
|
|
|
className={classnames(`button-toggle`, {
|
|
|
|
'button-toggle--active': sort === SORT_COMMENTS_BEST,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
button="alt"
|
2020-10-07 13:16:13 +02:00
|
|
|
label={__('Controversial')}
|
2020-10-06 21:35:13 +02:00
|
|
|
icon={ICONS.CONTROVERSIAL}
|
|
|
|
onClick={() => setSort(SORT_COMMENTS_CONTROVERSIAL)}
|
|
|
|
className={classnames(`button-toggle`, {
|
|
|
|
'button-toggle--active': sort === SORT_COMMENTS_CONTROVERSIAL,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
button="alt"
|
2020-10-07 13:16:13 +02:00
|
|
|
label={__('New')}
|
2020-10-06 21:35:13 +02:00
|
|
|
icon={ICONS.NEW}
|
|
|
|
onClick={() => setSort(SORT_COMMENTS_NEW)}
|
|
|
|
className={classnames(`button-toggle`, {
|
|
|
|
'button-toggle--active': sort === SORT_COMMENTS_NEW,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
<Button button="alt" icon={ICONS.REFRESH} title={__('Refresh')} onClick={() => fetchComments(uri)} />
|
|
|
|
</>
|
2020-10-02 06:47:54 +02:00
|
|
|
}
|
2020-08-24 19:35:21 +02:00
|
|
|
actions={
|
|
|
|
<>
|
2020-08-27 22:16:23 +02:00
|
|
|
<CommentCreate uri={uri} />
|
2020-10-07 21:14:52 +02:00
|
|
|
|
2020-08-27 06:25:03 +02:00
|
|
|
{!isFetchingComments && hasNoComments && <div className="main--empty">{__('Be the first to comment!')}</div>}
|
2020-10-07 21:14:52 +02:00
|
|
|
|
2020-08-24 19:35:21 +02:00
|
|
|
<ul className="comments" ref={commentRef}>
|
|
|
|
{!isFetchingComments &&
|
|
|
|
comments &&
|
|
|
|
displayedComments &&
|
|
|
|
displayedComments.map(comment => {
|
|
|
|
return (
|
2020-10-06 21:35:13 +02:00
|
|
|
<CommentView
|
2020-09-11 19:51:31 +02:00
|
|
|
isTopLevel
|
2020-10-07 21:14:52 +02:00
|
|
|
threadDepth={3}
|
2020-09-11 19:51:31 +02:00
|
|
|
key={comment.comment_id}
|
|
|
|
uri={uri}
|
|
|
|
authorUri={comment.channel_url}
|
|
|
|
author={comment.channel_name}
|
|
|
|
claimId={comment.claim_id}
|
|
|
|
commentId={comment.comment_id}
|
|
|
|
message={comment.comment}
|
|
|
|
timePosted={comment.timestamp * 1000}
|
|
|
|
claimIsMine={claimIsMine}
|
|
|
|
commentIsMine={comment.channel_id && isMyComment(comment.channel_id)}
|
|
|
|
linkedComment={linkedComment}
|
2020-10-08 17:31:36 +02:00
|
|
|
isPinned={comment.is_pinned}
|
2020-09-11 19:51:31 +02:00
|
|
|
/>
|
2020-08-24 19:35:21 +02:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
2020-10-07 21:14:52 +02:00
|
|
|
|
2020-10-06 21:35:13 +02:00
|
|
|
{(isFetchingComments || moreBelow) && (
|
|
|
|
<div className="main--empty" ref={spinnerRef}>
|
2020-10-05 23:25:23 +02:00
|
|
|
<Spinner type="small" />
|
2020-08-24 19:35:21 +02:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
2019-06-27 01:59:27 +02:00
|
|
|
);
|
2019-05-17 20:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default CommentList;
|