2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2018-10-29 18:23:53 +01:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
|
|
|
import * as ICONS from 'constants/icons';
|
2019-07-02 06:39:17 +02:00
|
|
|
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-04-02 16:26:32 +02:00
|
|
|
import useIsMobile from 'effects/use-is-mobile';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-07-08 22:54:58 +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,
|
|
|
|
subscriptions: Array<string>,
|
2018-03-26 23:32:43 +02:00
|
|
|
doChannelSubscribe: ({ channelName: string, uri: string }) => void,
|
2019-07-08 22:54:58 +02:00
|
|
|
doChannelUnsubscribe: SubscriptionArgs => void,
|
2019-01-08 00:29:40 +01:00
|
|
|
doOpenModal: (id: string) => void,
|
2018-11-21 22:20:55 +01:00
|
|
|
showSnackBarOnSubscribe: boolean,
|
|
|
|
doToast: ({ message: string }) => void,
|
2020-04-02 16:26:32 +02:00
|
|
|
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-29 18:23:53 +01:00
|
|
|
doOpenModal,
|
2018-10-19 22:38:07 +02:00
|
|
|
subscriptions,
|
|
|
|
isSubscribed,
|
2018-11-21 22:20:55 +01:00
|
|
|
showSnackBarOnSubscribe,
|
|
|
|
doToast,
|
2020-04-02 16:26:32 +02:00
|
|
|
shrinkOnMobile = false,
|
2018-03-26 23:32:43 +02:00
|
|
|
} = props;
|
2019-06-28 09:33:07 +02:00
|
|
|
const buttonRef = useRef();
|
2019-07-02 06:39:17 +02:00
|
|
|
const isHovering = useHover(buttonRef);
|
2020-04-02 16:26:32 +02:00
|
|
|
const isMobile = useIsMobile();
|
2019-09-04 06:22:31 +02:00
|
|
|
const { channelName } = parseURI(permanentUrl);
|
2019-08-30 01:18:06 +02:00
|
|
|
const claimName = '@' + channelName;
|
2017-12-21 22:08:54 +01:00
|
|
|
const subscriptionHandler = isSubscribed ? doChannelUnsubscribe : doChannelSubscribe;
|
2019-06-28 09:27:55 +02:00
|
|
|
const subscriptionLabel = isSubscribed ? __('Following') : __('Follow');
|
2019-06-28 09:33:07 +02:00
|
|
|
const unfollowOverride = isSubscribed && isHovering && __('Unfollow');
|
2020-04-02 16:26:32 +02:00
|
|
|
const label = isMobile && shrinkOnMobile ? '' : unfollowOverride || subscriptionLabel;
|
2017-12-08 21:38:20 +01:00
|
|
|
|
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}
|
2018-05-23 05:48:22 +02:00
|
|
|
iconColor="red"
|
2019-06-28 09:33:07 +02:00
|
|
|
icon={unfollowOverride ? ICONS.UNSUBSCRIBE : ICONS.SUBSCRIBE}
|
2019-06-17 22:32:38 +02:00
|
|
|
button={'alt'}
|
2019-10-15 20:53:55 +02:00
|
|
|
requiresAuth={IS_WEB}
|
2020-04-02 16:26:32 +02:00
|
|
|
label={label}
|
2018-08-27 20:45:50 +02:00
|
|
|
onClick={e => {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2019-01-14 22:21:37 +01:00
|
|
|
if (!subscriptions.length) {
|
2018-10-29 18:23:53 +01:00
|
|
|
doOpenModal(MODALS.FIRST_SUBSCRIPTION);
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
2018-11-21 22:20:55 +01:00
|
|
|
|
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) {
|
2019-06-28 09:27:55 +02:00
|
|
|
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
|
|
|
}
|