2020-09-11 19:51:31 +02:00
|
|
|
// @flow
|
2020-10-27 17:24:31 +01:00
|
|
|
import { ENABLE_CREATOR_REACTIONS } from 'config';
|
2020-10-01 20:43:44 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-12-14 22:42:00 +01:00
|
|
|
import * as PAGES from 'constants/pages';
|
2020-10-01 20:43:44 +02:00
|
|
|
import * as REACTION_TYPES from 'constants/reactions';
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Button from 'component/button';
|
2020-10-27 17:24:31 +01:00
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
2020-12-14 22:42:00 +01:00
|
|
|
import { useHistory } from 'react-router';
|
2020-09-11 19:51:31 +02:00
|
|
|
|
|
|
|
type Props = {
|
2020-09-29 16:10:23 +02:00
|
|
|
myReacts: Array<string>,
|
|
|
|
othersReacts: any,
|
|
|
|
react: (string, string) => void,
|
|
|
|
commentId: string,
|
2020-09-30 17:59:05 +02:00
|
|
|
pendingCommentReacts: Array<string>,
|
2020-10-27 17:24:31 +01:00
|
|
|
claimIsMine: boolean,
|
2021-02-09 17:05:56 +01:00
|
|
|
activeChannelId: ?string,
|
2020-10-27 17:24:31 +01:00
|
|
|
claim: ?ChannelClaim,
|
2020-12-14 22:42:00 +01:00
|
|
|
doToast: ({ message: string }) => void,
|
2020-09-11 19:51:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function CommentReactions(props: Props) {
|
2021-02-09 17:05:56 +01:00
|
|
|
const { myReacts, othersReacts, commentId, react, claimIsMine, claim, activeChannelId, doToast } = props;
|
2020-12-14 22:42:00 +01:00
|
|
|
const {
|
|
|
|
push,
|
|
|
|
location: { pathname },
|
|
|
|
} = useHistory();
|
2020-10-28 16:55:10 +01:00
|
|
|
const canCreatorReact =
|
|
|
|
claim &&
|
|
|
|
claimIsMine &&
|
|
|
|
(claim.value_type === 'channel'
|
2021-02-09 17:05:56 +01:00
|
|
|
? claim.claim_id === activeChannelId
|
|
|
|
: claim.signing_channel && claim.signing_channel.claim_id === activeChannelId);
|
2020-10-28 16:55:10 +01:00
|
|
|
const authorUri =
|
|
|
|
claim && claim.value_type === 'channel'
|
|
|
|
? claim.canonical_url
|
|
|
|
: claim && claim.signing_channel && claim.signing_channel.canonical_url;
|
2020-09-29 16:10:23 +02:00
|
|
|
|
2021-02-19 04:29:11 +01:00
|
|
|
const getCountForReact = (type) => {
|
2020-10-01 20:43:44 +02:00
|
|
|
let count = 0;
|
|
|
|
if (othersReacts && othersReacts[type]) {
|
|
|
|
count += othersReacts[type];
|
|
|
|
}
|
|
|
|
if (myReacts && myReacts.includes(type)) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
};
|
2020-09-11 19:51:31 +02:00
|
|
|
|
2020-10-27 17:24:31 +01:00
|
|
|
const creatorLiked = getCountForReact(REACTION_TYPES.CREATOR_LIKE) > 0;
|
|
|
|
|
2020-12-14 22:42:00 +01:00
|
|
|
function handleCommentLike() {
|
2021-02-09 17:05:56 +01:00
|
|
|
if (activeChannelId) {
|
2020-12-14 22:42:00 +01:00
|
|
|
react(commentId, REACTION_TYPES.LIKE);
|
|
|
|
} else {
|
|
|
|
promptForChannel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCommentDislike() {
|
2021-02-09 17:05:56 +01:00
|
|
|
if (activeChannelId) {
|
2020-12-14 22:42:00 +01:00
|
|
|
react(commentId, REACTION_TYPES.DISLIKE);
|
|
|
|
} else {
|
|
|
|
promptForChannel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function promptForChannel() {
|
|
|
|
push(`/$/${PAGES.CHANNEL_NEW}?redirect=${pathname}&lc=${commentId}`);
|
|
|
|
doToast({ message: __('A channel is required to throw fire and slime') });
|
|
|
|
}
|
|
|
|
|
2020-10-01 20:43:44 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
2020-09-30 17:59:05 +02:00
|
|
|
requiresAuth={IS_WEB}
|
2020-10-01 20:43:44 +02:00
|
|
|
title={__('Upvote')}
|
|
|
|
icon={ICONS.UPVOTE}
|
|
|
|
className={classnames('comment__action', {
|
|
|
|
'comment__action--active': myReacts && myReacts.includes(REACTION_TYPES.LIKE),
|
|
|
|
})}
|
2020-12-14 22:42:00 +01:00
|
|
|
onClick={handleCommentLike}
|
2020-10-27 17:24:31 +01:00
|
|
|
label={<span className="comment__reaction-count">{getCountForReact(REACTION_TYPES.LIKE)}</span>}
|
2020-10-01 20:43:44 +02:00
|
|
|
/>
|
|
|
|
<Button
|
2020-09-30 17:59:05 +02:00
|
|
|
requiresAuth={IS_WEB}
|
2020-10-01 20:43:44 +02:00
|
|
|
title={__('Downvote')}
|
|
|
|
icon={ICONS.DOWNVOTE}
|
|
|
|
className={classnames('comment__action', {
|
|
|
|
'comment__action--active': myReacts && myReacts.includes(REACTION_TYPES.DISLIKE),
|
|
|
|
})}
|
2020-12-14 22:42:00 +01:00
|
|
|
onClick={handleCommentDislike}
|
2020-10-27 17:24:31 +01:00
|
|
|
label={<span className="comment__reaction-count">{getCountForReact(REACTION_TYPES.DISLIKE)}</span>}
|
2020-10-01 20:43:44 +02:00
|
|
|
/>
|
2020-10-27 17:24:31 +01:00
|
|
|
|
2020-10-28 03:16:46 +01:00
|
|
|
{ENABLE_CREATOR_REACTIONS && (canCreatorReact || creatorLiked) && (
|
|
|
|
<Button
|
|
|
|
iconOnly
|
|
|
|
disabled={!canCreatorReact || !claimIsMine}
|
|
|
|
requiresAuth={IS_WEB}
|
|
|
|
title={claimIsMine ? __('You loved this') : __('Creator loved this')}
|
|
|
|
icon={creatorLiked ? ICONS.CREATOR_LIKE : ICONS.SUBSCRIBE}
|
|
|
|
className={classnames('comment__action comment__action--creator-like')}
|
|
|
|
onClick={() => react(commentId, REACTION_TYPES.CREATOR_LIKE)}
|
|
|
|
>
|
2021-02-19 04:29:11 +01:00
|
|
|
{creatorLiked && <ChannelThumbnail uri={authorUri} hideStakedIndicator className="comment__creator-like" />}
|
2020-10-28 03:16:46 +01:00
|
|
|
</Button>
|
2020-10-27 17:24:31 +01:00
|
|
|
)}
|
2020-10-01 20:43:44 +02:00
|
|
|
</>
|
|
|
|
);
|
2020-09-11 19:51:31 +02:00
|
|
|
}
|