Refactor fileReactions component

- makeSelectReactionsForUri and selectThemePath were not being used
- isLivestreamClaim from redux
- Reduce repetition, make single reaction component
This commit is contained in:
Rafael 2022-02-01 17:07:08 -03:00 committed by Thomas Zarebczan
parent dbeb102dc4
commit 21a5e27cd2
2 changed files with 118 additions and 78 deletions

View file

@ -1,26 +1,37 @@
import { connect } from 'react-redux';
import {
makeSelectReactionsForUri,
makeSelectMyReactionForUri,
makeSelectLikeCountForUri,
makeSelectDislikeCountForUri,
} from 'redux/selectors/reactions';
import { doFetchReactions, doReactionLike, doReactionDislike } from 'redux/actions/reactions';
import { selectThemePath } from 'redux/selectors/settings';
import FileViewCount from './view';
import { makeSelectClaimForUri } from 'redux/selectors/claims';
import { selectClaimForUri, selectIsStreamPlaceholderForUri } from 'redux/selectors/claims';
const select = (state, props) => ({
claim: makeSelectClaimForUri(props.uri)(state),
reactions: makeSelectReactionsForUri(props.uri)(state),
myReaction: makeSelectMyReactionForUri(props.uri)(state),
likeCount: makeSelectLikeCountForUri(props.uri)(state),
dislikeCount: makeSelectDislikeCountForUri(props.uri)(state),
theme: selectThemePath(state),
});
const select = (state, props) => {
const { uri } = props;
export default connect(select, {
const claim = selectClaimForUri(state, uri);
const { claim_id: claimId, signing_channel, value_type } = claim || {};
const channelName = signing_channel && signing_channel.name;
const isCollection = value_type && value_type === 'collection'; // hack because nudge gets cut off by card on cols.
return {
myReaction: makeSelectMyReactionForUri(uri)(state),
likeCount: makeSelectLikeCountForUri(uri)(state),
dislikeCount: makeSelectDislikeCountForUri(uri)(state),
isLivestreamClaim: selectIsStreamPlaceholderForUri(state, uri),
claimId,
channelName,
isCollection,
};
};
const perform = {
doFetchReactions,
doReactionLike,
doReactionDislike,
})(FileViewCount);
};
export default connect(select, perform)(FileViewCount);

View file

@ -6,80 +6,46 @@ import classnames from 'classnames';
import Button from 'component/button';
import { formatNumberWithCommas } from 'util/number';
import NudgeFloating from 'component/nudgeFloating';
import { SIMPLE_SITE } from 'config';
import Tooltip from 'component/common/tooltip';
const LIVE_REACTION_FETCH_MS = 1000 * 45;
type Props = {
claim: StreamClaim,
doFetchReactions: (string) => void,
doReactionLike: (string) => void,
doReactionDislike: (string) => void,
uri: string,
// redux
claimId?: string,
channelName?: string,
isCollection?: boolean,
likeCount: number,
dislikeCount: number,
myReaction: ?string,
livestream?: boolean,
isLivestreamClaim?: boolean,
doFetchReactions: (claimId: ?string) => void,
doReactionLike: (uri: string) => void,
doReactionDislike: (uri: string) => void,
};
function FileReactions(props: Props) {
export default function FileReactions(props: Props) {
const {
claim,
uri,
doFetchReactions,
doReactionLike,
doReactionDislike,
claimId,
channelName,
isCollection,
myReaction,
likeCount,
dislikeCount,
livestream,
isLivestreamClaim,
doFetchReactions,
doReactionLike,
doReactionDislike,
} = props;
const claimId = claim && claim.claim_id;
const channel = claim && claim.signing_channel && claim.signing_channel.name;
const isCollection = claim && claim.value_type === 'collection'; // hack because nudge gets cut off by card on cols.
const likeIcon = SIMPLE_SITE ? (myReaction === REACTION_TYPES.LIKE ? ICONS.FIRE_ACTIVE : ICONS.FIRE) : ICONS.UPVOTE;
const dislikeIcon = SIMPLE_SITE
? myReaction === REACTION_TYPES.DISLIKE
? ICONS.SLIME_ACTIVE
: ICONS.SLIME
: ICONS.DOWNVOTE;
React.useEffect(() => {
function fetchReactions() {
doFetchReactions(claimId);
}
const likeIcon = myReaction === REACTION_TYPES.LIKE ? ICONS.FIRE_ACTIVE : ICONS.FIRE;
const dislikeIcon = myReaction === REACTION_TYPES.DISLIKE ? ICONS.SLIME_ACTIVE : ICONS.SLIME;
let fetchInterval;
if (claimId) {
fetchReactions();
if (livestream) {
fetchInterval = setInterval(fetchReactions, 45000);
}
}
return () => {
if (fetchInterval) {
clearInterval(fetchInterval);
}
};
}, [claimId, doFetchReactions, livestream]);
return (
const likeLabel = (
<>
{channel && !isCollection && (
<NudgeFloating
name="nudge:support-acknowledge"
text={__('Let %channel% know you enjoyed this!', { channel })}
/>
)}
<Button
title={__('I like this')}
requiresAuth={IS_WEB}
authSrc="filereaction_like"
className={classnames('button--file-action', { 'button--fire': myReaction === REACTION_TYPES.LIKE })}
label={
<>
{myReaction === REACTION_TYPES.LIKE && SIMPLE_SITE && (
{myReaction === REACTION_TYPES.LIKE && (
<>
<div className="button__fire-glow" />
<div className="button__fire-particle1" />
@ -92,19 +58,11 @@ function FileReactions(props: Props) {
)}
{formatNumberWithCommas(likeCount, 0)}
</>
}
iconSize={18}
icon={likeIcon}
onClick={() => doReactionLike(uri)}
/>
<Button
requiresAuth={IS_WEB}
authSrc={'filereaction_dislike'}
title={__('I dislike this')}
className={classnames('button--file-action', { 'button--slime': myReaction === REACTION_TYPES.DISLIKE })}
label={
);
const dislikeLabel = (
<>
{myReaction === REACTION_TYPES.DISLIKE && SIMPLE_SITE && (
{myReaction === REACTION_TYPES.DISLIKE && (
<>
<div className="button__slime-stain" />
<div className="button__slime-drop1" />
@ -113,13 +71,84 @@ function FileReactions(props: Props) {
)}
{formatNumberWithCommas(dislikeCount, 0)}
</>
);
React.useEffect(() => {
function fetchReactions() {
doFetchReactions(claimId);
}
iconSize={18}
let fetchInterval;
if (claimId) {
fetchReactions();
if (isLivestreamClaim) {
fetchInterval = setInterval(fetchReactions, LIVE_REACTION_FETCH_MS);
}
}
return () => {
if (fetchInterval) {
clearInterval(fetchInterval);
}
};
}, [claimId, doFetchReactions, isLivestreamClaim]);
return (
<>
{channelName && !isCollection && (
<NudgeFloating
name="nudge:support-acknowledge"
text={__('Let %channel% know you enjoyed this!', { channel: channelName })}
/>
)}
<FileReaction
title={__('I like this')}
label={likeLabel}
icon={likeIcon}
isActive={myReaction === REACTION_TYPES.LIKE}
activeClassName="button--fire"
onClick={() => doReactionLike(uri)}
/>
<FileReaction
title={__('I dislike this')}
label={dislikeLabel}
icon={dislikeIcon}
isActive={myReaction === REACTION_TYPES.DISLIKE}
activeClassName="button--slime"
onClick={() => doReactionDislike(uri)}
/>
</>
);
}
export default FileReactions;
type ReactionProps = {
title: string,
label: any,
icon: string,
isActive: boolean,
activeClassName: string,
onClick: () => void,
};
const FileReaction = (reactionProps: ReactionProps) => {
const { title, label, icon, isActive, activeClassName, onClick } = reactionProps;
return (
<Tooltip title={title} arrow={false}>
<div style={{ margin: '0' }}>
<Button
requiresAuth
authSrc="filereaction_like"
className={classnames('button--file-action', { [activeClassName]: isActive })}
label={label}
iconSize={18}
icon={icon}
onClick={onClick}
/>
</div>
</Tooltip>
);
};