2020-09-11 19:51:31 +02:00
|
|
|
// @flow
|
2021-10-19 01:37:58 +02: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,
|
2021-09-01 18:03:48 +02:00
|
|
|
uri: string,
|
2020-10-27 17:24:31 +01:00
|
|
|
claim: ?ChannelClaim,
|
2020-12-14 22:42:00 +01:00
|
|
|
doToast: ({ message: string }) => void,
|
2021-08-27 12:29:58 +02:00
|
|
|
hideCreatorLike: boolean,
|
2021-09-01 18:03:48 +02:00
|
|
|
resolve: (string) => void,
|
2020-09-11 19:51:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function CommentReactions(props: Props) {
|
2021-08-27 12:29:58 +02:00
|
|
|
const {
|
|
|
|
myReacts,
|
|
|
|
othersReacts,
|
|
|
|
commentId,
|
|
|
|
react,
|
|
|
|
claimIsMine,
|
2021-09-01 18:03:48 +02:00
|
|
|
uri,
|
2021-08-27 12:29:58 +02:00
|
|
|
claim,
|
|
|
|
activeChannelId,
|
|
|
|
doToast,
|
|
|
|
hideCreatorLike,
|
2021-09-01 18:03:48 +02:00
|
|
|
resolve,
|
2021-08-27 12:29:58 +02:00
|
|
|
} = props;
|
2020-12-14 22:42:00 +01:00
|
|
|
const {
|
|
|
|
push,
|
|
|
|
location: { pathname },
|
|
|
|
} = useHistory();
|
2021-09-01 18:03:48 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!claim) {
|
|
|
|
resolve(uri);
|
|
|
|
}
|
|
|
|
}, [claim, resolve, uri]);
|
|
|
|
|
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;
|
|
|
|
};
|
2021-08-27 12:29:58 +02:00
|
|
|
const shouldHide = !canCreatorReact && hideCreatorLike;
|
2020-10-27 17:24:31 +01:00
|
|
|
const creatorLiked = getCountForReact(REACTION_TYPES.CREATOR_LIKE) > 0;
|
2021-10-19 01:37:58 +02:00
|
|
|
const likeIcon = ICONS.UPVOTE;
|
|
|
|
const dislikeIcon = ICONS.DOWNVOTE;
|
2020-10-27 17:24:31 +01:00
|
|
|
|
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
|
|
|
|
title={__('Upvote')}
|
2021-07-20 17:08:08 +02:00
|
|
|
icon={likeIcon}
|
2020-10-01 20:43:44 +02:00
|
|
|
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
|
|
|
|
title={__('Downvote')}
|
2021-07-20 17:08:08 +02:00
|
|
|
icon={dislikeIcon}
|
2020-10-01 20:43:44 +02:00
|
|
|
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
|
|
|
|
2021-08-27 12:29:58 +02:00
|
|
|
{!shouldHide && ENABLE_CREATOR_REACTIONS && (canCreatorReact || creatorLiked) && (
|
2020-10-28 03:16:46 +01:00
|
|
|
<Button
|
|
|
|
disabled={!canCreatorReact || !claimIsMine}
|
|
|
|
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)}
|
|
|
|
>
|
ChannelThumbnail improvements
- [x] (6332) The IntersectionObserver method of lazy-loading loads cached images visibly late on slower devices. Previously, it was also showing the "broken image" icon briefly, which we mended by placing a dummy transparent image as the initial src.
- Reverted that ugly transparent image fix.
- Use the browser's built-in `loading="lazy"` instead. Sorry, Safari.
- [x] Size-optimization did not take "device pixel ratio" into account.
- When resizing an image through the CDN, we can't just take the dimensions of the tag in pixels directly -- we need to take zooming into account, otherwise the image ends up blurry.
- Previously, we quickly disabled optimization for the channel avatar in the Channel Page because of this. Now that we know the root-cause, the change was reverted and we now go through the CDN with appropriate sizes. This also improves our Web Vital scores.
- [x] Size-optimization wasn't really implemented for all ChannelThumbnail instances.
- The CDN-optimized size was hardcoded to the largest instance, so small images like sidebar thumbnails are still loading images that are unnecessarily larger.
- There's a little-bit of hardcoding of values from CSS here, but I think it's a ok compromise (not something we change often). It also doesn't need to be exact -- the "device pixel ratio" calculate will ensure it's slightly larger than what we need.
- [x] Set `width` and `height` of `<img>` to improve CLS.
- Addresses Ligthhouse complaints, although technically the shifting was addressed at the `ClaimPreviewTile` level (sub-container dimensions are well defined).
- Notes: the values don't need to be the final CSS-adjusted sizes. It just needs to be in the right aspect ratio to help the browser pre-allocate space to avoid shifts.
- [x] Add option to disable lazy-load Channel Thumbnails
- The guidelines mentioned that items that are already in the viewport should not enable `loading="lazy"`.
- We have a few areas where it doesn't make sense to lazy-load (e.g. thumbnail in Header, channel selector dropdown, publish preview, etc.).
2021-07-05 07:20:40 +02:00
|
|
|
{creatorLiked && (
|
2021-08-26 16:44:00 +02:00
|
|
|
<ChannelThumbnail xsmall uri={authorUri} hideStakedIndicator className="comment__creator-like" allowGifs />
|
ChannelThumbnail improvements
- [x] (6332) The IntersectionObserver method of lazy-loading loads cached images visibly late on slower devices. Previously, it was also showing the "broken image" icon briefly, which we mended by placing a dummy transparent image as the initial src.
- Reverted that ugly transparent image fix.
- Use the browser's built-in `loading="lazy"` instead. Sorry, Safari.
- [x] Size-optimization did not take "device pixel ratio" into account.
- When resizing an image through the CDN, we can't just take the dimensions of the tag in pixels directly -- we need to take zooming into account, otherwise the image ends up blurry.
- Previously, we quickly disabled optimization for the channel avatar in the Channel Page because of this. Now that we know the root-cause, the change was reverted and we now go through the CDN with appropriate sizes. This also improves our Web Vital scores.
- [x] Size-optimization wasn't really implemented for all ChannelThumbnail instances.
- The CDN-optimized size was hardcoded to the largest instance, so small images like sidebar thumbnails are still loading images that are unnecessarily larger.
- There's a little-bit of hardcoding of values from CSS here, but I think it's a ok compromise (not something we change often). It also doesn't need to be exact -- the "device pixel ratio" calculate will ensure it's slightly larger than what we need.
- [x] Set `width` and `height` of `<img>` to improve CLS.
- Addresses Ligthhouse complaints, although technically the shifting was addressed at the `ClaimPreviewTile` level (sub-container dimensions are well defined).
- Notes: the values don't need to be the final CSS-adjusted sizes. It just needs to be in the right aspect ratio to help the browser pre-allocate space to avoid shifts.
- [x] Add option to disable lazy-load Channel Thumbnails
- The guidelines mentioned that items that are already in the viewport should not enable `loading="lazy"`.
- We have a few areas where it doesn't make sense to lazy-load (e.g. thumbnail in Header, channel selector dropdown, publish preview, etc.).
2021-07-05 07:20:40 +02:00
|
|
|
)}
|
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
|
|
|
}
|