2019-06-27 01:59:27 +02:00
|
|
|
// @flow
|
2020-07-24 16:13:42 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-09-11 19:51:31 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2020-07-24 16:13:42 +02:00
|
|
|
import { FF_MAX_CHARS_IN_COMMENT } from 'constants/form-field';
|
2020-09-30 17:59:05 +02:00
|
|
|
import { SITE_NAME, SIMPLE_SITE, ENABLE_COMMENT_REACTIONS } from 'config';
|
2020-01-30 02:02:21 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-10-08 17:31:36 +02:00
|
|
|
import { parseURI } from 'lbry-redux';
|
2020-05-11 09:57:03 +02:00
|
|
|
import DateTime from 'component/dateTime';
|
2019-07-21 22:46:30 +02:00
|
|
|
import Button from 'component/button';
|
2020-09-11 19:51:31 +02:00
|
|
|
import Expandable from 'component/expandable';
|
2019-10-13 06:04:16 +02:00
|
|
|
import MarkdownPreview from 'component/common/markdown-preview';
|
2019-10-23 09:04:40 +02:00
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
2021-02-11 06:12:41 +01:00
|
|
|
import { Menu, MenuButton } from '@reach/menu-button';
|
2020-01-30 02:02:21 +01:00
|
|
|
import Icon from 'component/common/icon';
|
|
|
|
import { FormField, Form } from 'component/common/form';
|
2020-02-05 04:55:00 +01:00
|
|
|
import classnames from 'classnames';
|
2020-05-21 10:53:21 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-09-11 19:51:31 +02:00
|
|
|
import CommentReactions from 'component/commentReactions';
|
|
|
|
import CommentsReplies from 'component/commentsReplies';
|
|
|
|
import { useHistory } from 'react-router';
|
2020-10-07 21:14:52 +02:00
|
|
|
import CommentCreate from 'component/commentCreate';
|
2021-02-11 06:12:41 +01:00
|
|
|
import CommentMenuList from 'component/commentMenuList';
|
2019-06-27 01:59:27 +02:00
|
|
|
|
|
|
|
type Props = {
|
2021-01-26 22:20:06 +01:00
|
|
|
closeInlinePlayer: () => void,
|
2020-02-05 04:55:00 +01:00
|
|
|
uri: string,
|
2020-01-30 02:02:21 +01:00
|
|
|
author: ?string, // LBRY Channel Name, e.g. @channel
|
|
|
|
authorUri: string, // full LBRY Channel URI: lbry://@channel#123...
|
|
|
|
commentId: string, // sha256 digest identifying the comment
|
|
|
|
message: string, // comment body
|
|
|
|
timePosted: number, // Comment timestamp
|
|
|
|
channelIsBlocked: boolean, // if the channel is blacklisted in the app
|
|
|
|
claimIsMine: boolean, // if you control the claim which this comment was posted on
|
|
|
|
commentIsMine: boolean, // if this comment was signed by an owned channel
|
|
|
|
updateComment: (string, string) => void,
|
2021-02-11 06:12:41 +01:00
|
|
|
commentModBlock: string => void,
|
2020-08-24 19:35:21 +02:00
|
|
|
linkedComment?: any,
|
2020-09-11 19:51:31 +02:00
|
|
|
myChannels: ?Array<ChannelClaim>,
|
|
|
|
commentingEnabled: boolean,
|
|
|
|
doToast: ({ message: string }) => void,
|
|
|
|
isTopLevel?: boolean,
|
2020-10-07 21:14:52 +02:00
|
|
|
threadDepth: number,
|
2020-10-08 17:31:36 +02:00
|
|
|
isPinned: boolean,
|
2020-10-08 21:55:16 +02:00
|
|
|
othersReacts: ?{
|
|
|
|
like: number,
|
|
|
|
dislike: number,
|
|
|
|
},
|
2020-10-20 05:20:38 +02:00
|
|
|
commentIdentityChannel: any,
|
2021-02-09 17:05:56 +01:00
|
|
|
activeChannelClaim: ?ChannelClaim,
|
2019-06-27 01:59:27 +02:00
|
|
|
};
|
|
|
|
|
2019-11-27 22:08:43 +01:00
|
|
|
const LENGTH_TO_COLLAPSE = 300;
|
2020-01-30 02:02:21 +01:00
|
|
|
const ESCAPE_KEY = 27;
|
2019-11-27 22:08:43 +01:00
|
|
|
|
2019-06-27 01:59:27 +02:00
|
|
|
function Comment(props: Props) {
|
2019-10-23 09:04:40 +02:00
|
|
|
const {
|
2021-01-26 20:50:44 +01:00
|
|
|
closeInlinePlayer,
|
2020-02-05 04:55:00 +01:00
|
|
|
uri,
|
2019-10-23 09:04:40 +02:00
|
|
|
author,
|
|
|
|
authorUri,
|
|
|
|
timePosted,
|
|
|
|
message,
|
|
|
|
channelIsBlocked,
|
2020-01-30 02:02:21 +01:00
|
|
|
commentIsMine,
|
|
|
|
commentId,
|
|
|
|
updateComment,
|
2020-08-24 19:35:21 +02:00
|
|
|
linkedComment,
|
2020-09-11 19:51:31 +02:00
|
|
|
commentingEnabled,
|
|
|
|
myChannels,
|
|
|
|
doToast,
|
|
|
|
isTopLevel,
|
2020-10-07 21:14:52 +02:00
|
|
|
threadDepth,
|
2020-10-08 17:31:36 +02:00
|
|
|
isPinned,
|
2020-10-08 21:55:16 +02:00
|
|
|
othersReacts,
|
2019-10-23 09:04:40 +02:00
|
|
|
} = props;
|
2020-09-11 19:51:31 +02:00
|
|
|
const {
|
|
|
|
push,
|
2020-10-08 17:31:36 +02:00
|
|
|
replace,
|
|
|
|
location: { pathname, search },
|
2020-09-11 19:51:31 +02:00
|
|
|
} = useHistory();
|
|
|
|
const [isReplying, setReplying] = React.useState(false);
|
2020-01-30 02:02:21 +01:00
|
|
|
const [isEditing, setEditing] = useState(false);
|
|
|
|
const [editedMessage, setCommentValue] = useState(message);
|
|
|
|
const [charCount, setCharCount] = useState(editedMessage.length);
|
|
|
|
// used for controlling the visibility of the menu icon
|
|
|
|
const [mouseIsHovering, setMouseHover] = useState(false);
|
2020-08-24 19:35:21 +02:00
|
|
|
const [advancedEditor] = usePersistedState('comment-editor-mode', false);
|
2020-10-08 21:55:16 +02:00
|
|
|
const [displayDeadComment, setDisplayDeadComment] = React.useState(false);
|
2020-09-11 19:51:31 +02:00
|
|
|
const hasChannels = myChannels && myChannels.length > 0;
|
2020-10-08 21:55:16 +02:00
|
|
|
const likesCount = (othersReacts && othersReacts.like) || 0;
|
|
|
|
const dislikesCount = (othersReacts && othersReacts.dislike) || 0;
|
|
|
|
const totalLikesAndDislikes = likesCount + dislikesCount;
|
2020-10-26 19:06:33 +01:00
|
|
|
const slimedToDeath = totalLikesAndDislikes >= 5 && dislikesCount / totalLikesAndDislikes > 0.8;
|
2021-02-09 17:05:56 +01:00
|
|
|
|
2020-10-08 17:31:36 +02:00
|
|
|
let channelOwnerOfContent;
|
|
|
|
try {
|
|
|
|
const { channelName } = parseURI(uri);
|
|
|
|
if (channelName) {
|
|
|
|
channelOwnerOfContent = channelName;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
2019-12-04 19:07:40 +01:00
|
|
|
|
2019-10-23 09:04:40 +02:00
|
|
|
useEffect(() => {
|
2020-01-30 02:02:21 +01:00
|
|
|
if (isEditing) {
|
|
|
|
setCharCount(editedMessage.length);
|
|
|
|
|
|
|
|
// a user will try and press the escape key to cancel editing their comment
|
|
|
|
const handleEscape = event => {
|
|
|
|
if (event.keyCode === ESCAPE_KEY) {
|
|
|
|
setEditing(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleEscape);
|
|
|
|
|
|
|
|
// removes the listener so it doesn't cause problems elsewhere in the app
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('keydown', handleEscape);
|
|
|
|
};
|
|
|
|
}
|
2021-02-09 17:05:56 +01:00
|
|
|
}, [author, authorUri, editedMessage, isEditing, setEditing]);
|
2020-01-30 02:02:21 +01:00
|
|
|
|
|
|
|
function handleEditMessageChanged(event) {
|
2020-07-24 16:13:42 +02:00
|
|
|
setCommentValue(!SIMPLE_SITE && advancedEditor ? event : event.target.value);
|
2020-01-30 02:02:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 20:50:44 +01:00
|
|
|
function handleEditComment() {
|
|
|
|
closeInlinePlayer();
|
|
|
|
setEditing(true);
|
|
|
|
}
|
|
|
|
|
2020-01-30 02:02:21 +01:00
|
|
|
function handleSubmit() {
|
|
|
|
updateComment(commentId, editedMessage);
|
|
|
|
setEditing(false);
|
|
|
|
}
|
|
|
|
|
2020-09-11 19:51:31 +02:00
|
|
|
function handleCommentReply() {
|
|
|
|
if (!hasChannels) {
|
|
|
|
push(`/$/${PAGES.CHANNEL_NEW}?redirect=${pathname}`);
|
|
|
|
doToast({ message: __('A channel is required to comment on %SITE_NAME%', { SITE_NAME }) });
|
|
|
|
} else {
|
2020-10-07 21:14:52 +02:00
|
|
|
setReplying(!isReplying);
|
2020-09-11 19:51:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 17:31:36 +02:00
|
|
|
function handleTimeClick() {
|
|
|
|
const urlParams = new URLSearchParams(search);
|
|
|
|
urlParams.delete('lc');
|
|
|
|
urlParams.append('lc', commentId);
|
|
|
|
replace(`${pathname}?${urlParams.toString()}`);
|
|
|
|
}
|
|
|
|
|
2019-06-27 01:59:27 +02:00
|
|
|
return (
|
2020-02-05 04:55:00 +01:00
|
|
|
<li
|
2020-08-24 19:35:21 +02:00
|
|
|
className={classnames('comment', {
|
2020-10-07 21:14:52 +02:00
|
|
|
'comment--top-level': isTopLevel,
|
2020-09-11 19:51:31 +02:00
|
|
|
'comment--reply': !isTopLevel,
|
2020-08-24 19:35:21 +02:00
|
|
|
})}
|
|
|
|
id={commentId}
|
2020-07-15 18:36:40 +02:00
|
|
|
onMouseOver={() => setMouseHover(true)}
|
|
|
|
onMouseOut={() => setMouseHover(false)}
|
2020-02-05 04:55:00 +01:00
|
|
|
>
|
2020-10-08 17:31:36 +02:00
|
|
|
<div
|
|
|
|
className={classnames('comment__content', {
|
|
|
|
'comment--highlighted': linkedComment && linkedComment.comment_id === commentId,
|
2020-10-08 21:55:16 +02:00
|
|
|
'comment--slimed': slimedToDeath && !displayDeadComment,
|
2020-10-08 17:31:36 +02:00
|
|
|
})}
|
|
|
|
>
|
2020-10-20 19:10:02 +02:00
|
|
|
<div className="comment__thumbnail-wrapper">
|
2020-09-11 19:51:31 +02:00
|
|
|
{authorUri ? (
|
2020-10-20 19:10:02 +02:00
|
|
|
<ChannelThumbnail uri={authorUri} obscure={channelIsBlocked} small className="comment__author-thumbnail" />
|
2020-09-11 19:51:31 +02:00
|
|
|
) : (
|
2020-10-20 19:10:02 +02:00
|
|
|
<ChannelThumbnail small className="comment__author-thumbnail" />
|
2020-09-11 19:51:31 +02:00
|
|
|
)}
|
|
|
|
</div>
|
2019-10-24 19:24:53 +02:00
|
|
|
|
2020-09-11 19:51:31 +02:00
|
|
|
<div className="comment__body_container">
|
|
|
|
<div className="comment__meta">
|
|
|
|
<div className="comment__meta-information">
|
|
|
|
{!author ? (
|
|
|
|
<span className="comment__author">{__('Anonymous')}</span>
|
|
|
|
) : (
|
|
|
|
<Button
|
|
|
|
className="button--uri-indicator truncated-text comment__author"
|
|
|
|
navigate={authorUri}
|
|
|
|
label={author}
|
|
|
|
/>
|
|
|
|
)}
|
2020-01-30 02:02:21 +01:00
|
|
|
<Button
|
2020-10-06 21:35:13 +02:00
|
|
|
className="comment__time"
|
2020-10-08 17:31:36 +02:00
|
|
|
onClick={handleTimeClick}
|
2020-10-06 21:35:13 +02:00
|
|
|
label={<DateTime date={timePosted} timeAgo />}
|
2020-01-30 02:02:21 +01:00
|
|
|
/>
|
2020-10-08 17:31:36 +02:00
|
|
|
|
|
|
|
{isPinned && (
|
|
|
|
<span className="comment__pin">
|
2020-10-20 05:20:38 +02:00
|
|
|
<Icon icon={ICONS.PIN} size={14} />
|
2020-10-08 17:31:36 +02:00
|
|
|
{channelOwnerOfContent
|
|
|
|
? __('Pinned by @%channel%', { channel: channelOwnerOfContent })
|
|
|
|
: __('Pinned by creator')}
|
|
|
|
</span>
|
|
|
|
)}
|
2020-09-11 19:51:31 +02:00
|
|
|
</div>
|
|
|
|
<div className="comment__menu">
|
|
|
|
<Menu>
|
|
|
|
<MenuButton>
|
|
|
|
<Icon
|
|
|
|
size={18}
|
|
|
|
className={mouseIsHovering ? 'comment__menu-icon--hovering' : 'comment__menu-icon'}
|
|
|
|
icon={ICONS.MORE_VERTICAL}
|
|
|
|
/>
|
|
|
|
</MenuButton>
|
2021-02-11 06:12:41 +01:00
|
|
|
<CommentMenuList
|
|
|
|
uri={uri}
|
|
|
|
isTopLevel={isTopLevel}
|
|
|
|
isPinned={isPinned}
|
|
|
|
commentId={commentId}
|
|
|
|
authorUri={authorUri}
|
|
|
|
commentIsMine={commentIsMine}
|
|
|
|
handleEditComment={handleEditComment}
|
|
|
|
/>
|
2020-09-11 19:51:31 +02:00
|
|
|
</Menu>
|
|
|
|
</div>
|
2020-01-30 02:02:21 +01:00
|
|
|
</div>
|
2020-09-11 19:51:31 +02:00
|
|
|
<div>
|
|
|
|
{isEditing ? (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<FormField
|
|
|
|
type={!SIMPLE_SITE && advancedEditor ? 'markdown' : 'textarea'}
|
|
|
|
name="editing_comment"
|
|
|
|
value={editedMessage}
|
|
|
|
charCount={charCount}
|
|
|
|
onChange={handleEditMessageChanged}
|
|
|
|
textAreaMaxLength={FF_MAX_CHARS_IN_COMMENT}
|
2020-01-30 02:02:21 +01:00
|
|
|
/>
|
2020-09-11 19:51:31 +02:00
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
button="primary"
|
|
|
|
type="submit"
|
|
|
|
label={__('Done')}
|
|
|
|
requiresAuth={IS_WEB}
|
|
|
|
disabled={message === editedMessage}
|
|
|
|
/>
|
|
|
|
<Button button="link" label={__('Cancel')} onClick={() => setEditing(false)} />
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="comment__message">
|
2020-10-08 21:55:16 +02:00
|
|
|
{slimedToDeath && !displayDeadComment ? (
|
|
|
|
<div onClick={() => setDisplayDeadComment(true)} className="comment__dead">
|
2020-10-09 17:38:50 +02:00
|
|
|
{__('This comment was slimed to death.')} <Icon icon={ICONS.SLIME_ACTIVE} />
|
2020-10-08 21:55:16 +02:00
|
|
|
</div>
|
|
|
|
) : editedMessage.length >= LENGTH_TO_COLLAPSE ? (
|
2020-09-11 19:51:31 +02:00
|
|
|
<Expandable>
|
2020-10-20 19:10:02 +02:00
|
|
|
<MarkdownPreview content={message} promptLinks parentCommentId={commentId} />
|
2020-09-11 19:51:31 +02:00
|
|
|
</Expandable>
|
|
|
|
) : (
|
2020-10-20 19:10:02 +02:00
|
|
|
<MarkdownPreview content={message} promptLinks parentCommentId={commentId} />
|
2020-09-11 19:51:31 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="comment__actions">
|
2020-10-07 21:14:52 +02:00
|
|
|
{threadDepth !== 0 && (
|
2020-09-11 19:51:31 +02:00
|
|
|
<Button
|
|
|
|
requiresAuth={IS_WEB}
|
|
|
|
label={commentingEnabled ? __('Reply') : __('Log in to reply')}
|
|
|
|
className="comment__action"
|
|
|
|
onClick={handleCommentReply}
|
|
|
|
icon={ICONS.REPLY}
|
|
|
|
/>
|
|
|
|
)}
|
2020-10-27 17:24:31 +01:00
|
|
|
{ENABLE_COMMENT_REACTIONS && <CommentReactions uri={uri} commentId={commentId} />}
|
2020-09-11 19:51:31 +02:00
|
|
|
</div>
|
2020-10-07 21:14:52 +02:00
|
|
|
|
|
|
|
{isReplying && (
|
|
|
|
<CommentCreate
|
|
|
|
isReply
|
|
|
|
uri={uri}
|
|
|
|
parentId={commentId}
|
|
|
|
onDoneReplying={() => setReplying(false)}
|
|
|
|
onCancelReplying={() => setReplying(false)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-09-11 19:51:31 +02:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
2019-10-24 19:24:53 +02:00
|
|
|
</div>
|
2019-07-24 23:02:35 +02:00
|
|
|
</div>
|
2020-09-11 19:51:31 +02:00
|
|
|
|
2020-10-07 21:14:52 +02:00
|
|
|
<CommentsReplies threadDepth={threadDepth - 1} uri={uri} parentId={commentId} linkedComment={linkedComment} />
|
2019-06-27 01:59:27 +02:00
|
|
|
</li>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Comment;
|