lbry-desktop/ui/component/subscribeButton/view.jsx

77 lines
2.2 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import * as ICONS from 'constants/icons';
import React, { useRef } from 'react';
2018-11-21 22:20:55 +01:00
import { parseURI } from 'lbry-redux';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2019-09-27 20:56:15 +02:00
import useHover from 'effects/use-hover';
2020-08-10 22:47:39 +02:00
import { useIsMobile } from 'effects/use-screensize';
2018-03-26 23:32:43 +02:00
type SubscriptionArgs = {
2018-03-26 23:32:43 +02:00
channelName: string,
uri: string,
};
type Props = {
2019-09-04 06:22:31 +02:00
permanentUrl: ?string,
2018-10-19 22:38:07 +02:00
isSubscribed: boolean,
2018-03-26 23:32:43 +02:00
doChannelSubscribe: ({ channelName: string, uri: string }) => void,
doChannelUnsubscribe: SubscriptionArgs => void,
2018-11-21 22:20:55 +01:00
showSnackBarOnSubscribe: boolean,
doToast: ({ message: string }) => void,
shrinkOnMobile: boolean,
2018-03-26 23:32:43 +02:00
};
2019-06-11 20:10:58 +02:00
export default function SubscribeButton(props: Props) {
2018-03-26 23:32:43 +02:00
const {
2019-09-04 06:22:31 +02:00
permanentUrl,
2018-03-26 23:32:43 +02:00
doChannelSubscribe,
doChannelUnsubscribe,
2018-10-19 22:38:07 +02:00
isSubscribed,
2018-11-21 22:20:55 +01:00
showSnackBarOnSubscribe,
doToast,
shrinkOnMobile = false,
2018-03-26 23:32:43 +02:00
} = props;
2019-06-28 09:33:07 +02:00
const buttonRef = useRef();
const isMobile = useIsMobile();
let isHovering = useHover(buttonRef);
isHovering = isMobile ? true : isHovering;
2019-09-04 06:22:31 +02:00
const { channelName } = parseURI(permanentUrl);
const claimName = '@' + channelName;
const subscriptionHandler = isSubscribed ? doChannelUnsubscribe : doChannelSubscribe;
const subscriptionLabel = isSubscribed
? __('Following --[button label indicating a channel has been followed]--')
: __('Follow');
2019-06-28 09:33:07 +02:00
const unfollowOverride = isSubscribed && isHovering && __('Unfollow');
const label = isMobile && shrinkOnMobile ? '' : unfollowOverride || subscriptionLabel;
const titlePrefix = isSubscribed ? __('Unfollow this channel') : __('Follow this channel');
2019-09-04 06:22:31 +02:00
return permanentUrl ? (
2018-03-26 23:32:43 +02:00
<Button
2019-06-28 09:33:07 +02:00
ref={buttonRef}
iconColor="red"
largestLabel={isMobile && shrinkOnMobile ? '' : subscriptionLabel}
2019-06-28 09:33:07 +02:00
icon={unfollowOverride ? ICONS.UNSUBSCRIBE : ICONS.SUBSCRIBE}
2019-06-17 22:32:38 +02:00
button={'alt'}
requiresAuth={IS_WEB}
label={label}
title={titlePrefix}
onClick={e => {
e.stopPropagation();
2018-03-26 23:32:43 +02:00
subscriptionHandler({
2018-11-21 22:20:55 +01:00
channelName: claimName,
2019-09-04 06:22:31 +02:00
uri: permanentUrl,
2018-03-26 23:32:43 +02:00
});
2018-11-21 22:20:55 +01:00
if (showSnackBarOnSubscribe) {
doToast({ message: `${__('Now following ')} ${claimName}!` });
2018-11-21 22:20:55 +01:00
}
2018-03-26 23:32:43 +02:00
}}
/>
2019-09-04 06:22:31 +02:00
) : null;
2019-06-11 20:10:58 +02:00
}