2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2018-10-29 18:23:53 +01:00
|
|
|
import * as ICONS from 'constants/icons';
|
2019-07-02 06:39:17 +02:00
|
|
|
import React, { useRef } from 'react';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { parseURI } from 'util/lbryURI';
|
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';
|
2021-07-06 19:01:55 +02:00
|
|
|
import { ENABLE_UI_NOTIFICATIONS } from 'config';
|
2021-10-27 16:38:10 +02:00
|
|
|
import useBrowserNotifications from '$web/component/browserNotificationSettings/use-browser-notifications';
|
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,
|
2020-11-02 17:51:08 +01:00
|
|
|
notificationsDisabled?: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
2019-09-04 06:22:31 +02:00
|
|
|
permanentUrl: ?string,
|
2018-10-19 22:38:07 +02:00
|
|
|
isSubscribed: boolean,
|
2021-07-05 18:49:58 +02:00
|
|
|
doChannelSubscribe: (SubscriptionArgs, boolean) => void,
|
|
|
|
doChannelUnsubscribe: (SubscriptionArgs, boolean) => void,
|
2018-11-21 22:20:55 +01:00
|
|
|
doToast: ({ message: string }) => void,
|
2020-04-02 16:26:32 +02:00
|
|
|
shrinkOnMobile: boolean,
|
2020-11-02 17:51:08 +01:00
|
|
|
notificationsDisabled: boolean,
|
2020-11-02 21:43:52 +01:00
|
|
|
user: ?User,
|
2021-04-29 05:44:29 +02:00
|
|
|
uri: string,
|
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
|
|
|
doToast,
|
2020-04-02 16:26:32 +02:00
|
|
|
shrinkOnMobile = false,
|
2020-11-02 17:51:08 +01:00
|
|
|
notificationsDisabled,
|
2020-11-02 21:43:52 +01:00
|
|
|
user,
|
2021-04-29 05:44:29 +02:00
|
|
|
uri,
|
2018-03-26 23:32:43 +02:00
|
|
|
} = props;
|
2020-04-23 18:34:47 +02:00
|
|
|
|
2019-06-28 09:33:07 +02:00
|
|
|
const buttonRef = useRef();
|
2020-04-02 16:26:32 +02:00
|
|
|
const isMobile = useIsMobile();
|
2020-04-23 18:34:47 +02:00
|
|
|
let isHovering = useHover(buttonRef);
|
|
|
|
isHovering = isMobile ? true : isHovering;
|
2021-07-06 19:01:55 +02:00
|
|
|
const uiNotificationsEnabled = (user && user.experimental_ui) || ENABLE_UI_NOTIFICATIONS;
|
2020-04-23 18:34:47 +02:00
|
|
|
|
2021-04-29 05:44:29 +02:00
|
|
|
const { channelName: rawChannelName } = parseURI(uri);
|
2021-10-17 10:36:14 +02:00
|
|
|
|
|
|
|
let channelName;
|
|
|
|
|
|
|
|
if (permanentUrl) {
|
|
|
|
try {
|
|
|
|
const { channelName: name } = parseURI(permanentUrl);
|
|
|
|
if (name) {
|
|
|
|
channelName = name;
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
const claimName = channelName && '@' + channelName;
|
2020-04-23 18:34:47 +02:00
|
|
|
|
2021-10-27 16:38:10 +02:00
|
|
|
const { pushSupported, pushEnabled, pushRequest } = useBrowserNotifications();
|
|
|
|
|
2017-12-21 22:08:54 +01:00
|
|
|
const subscriptionHandler = isSubscribed ? doChannelUnsubscribe : doChannelSubscribe;
|
2021-10-27 16:38:10 +02:00
|
|
|
|
2020-10-09 09:13:28 +02:00
|
|
|
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');
|
2020-04-04 01:29:39 +02:00
|
|
|
|
2020-04-23 18:34:47 +02:00
|
|
|
const label = isMobile && shrinkOnMobile ? '' : unfollowOverride || subscriptionLabel;
|
2020-05-26 05:48:12 +02:00
|
|
|
const titlePrefix = isSubscribed ? __('Unfollow this channel') : __('Follow this channel');
|
2020-04-04 01:29:39 +02:00
|
|
|
|
2021-04-29 05:44:29 +02:00
|
|
|
if (isSubscribed && !permanentUrl && rawChannelName) {
|
|
|
|
return (
|
|
|
|
<div className="button-group">
|
|
|
|
<Button
|
|
|
|
ref={buttonRef}
|
|
|
|
iconColor="red"
|
|
|
|
largestLabel={isMobile && shrinkOnMobile ? '' : subscriptionLabel}
|
|
|
|
icon={ICONS.UNSUBSCRIBE}
|
|
|
|
button={'alt'}
|
|
|
|
requiresAuth={IS_WEB}
|
|
|
|
label={label}
|
|
|
|
title={titlePrefix}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
subscriptionHandler(
|
|
|
|
{
|
|
|
|
channelName: '@' + rawChannelName,
|
|
|
|
uri: uri,
|
|
|
|
notificationsDisabled: true,
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2021-04-29 05:44:29 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
return permanentUrl && claimName ? (
|
2020-11-02 17:51:08 +01:00
|
|
|
<div className="button-group">
|
|
|
|
<Button
|
|
|
|
ref={buttonRef}
|
|
|
|
iconColor="red"
|
|
|
|
largestLabel={isMobile && shrinkOnMobile ? '' : subscriptionLabel}
|
2021-08-24 16:57:01 +02:00
|
|
|
icon={unfollowOverride ? ICONS.UNSUBSCRIBE : isSubscribed ? ICONS.SUBSCRIBED : ICONS.SUBSCRIBE}
|
2020-11-02 17:51:08 +01:00
|
|
|
button={'alt'}
|
|
|
|
requiresAuth={IS_WEB}
|
|
|
|
label={label}
|
|
|
|
title={titlePrefix}
|
2021-04-29 05:44:29 +02:00
|
|
|
onClick={(e) => {
|
2020-11-02 17:51:08 +01:00
|
|
|
e.stopPropagation();
|
2018-08-27 20:45:50 +02:00
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
subscriptionHandler(
|
|
|
|
{
|
|
|
|
channelName: claimName,
|
|
|
|
uri: permanentUrl,
|
|
|
|
notificationsDisabled: true,
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
2020-11-02 17:51:08 +01:00
|
|
|
}}
|
|
|
|
/>
|
2020-11-02 21:43:52 +01:00
|
|
|
{isSubscribed && uiNotificationsEnabled && (
|
2020-11-02 17:51:08 +01:00
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
icon={notificationsDisabled ? ICONS.BELL : ICONS.BELL_ON}
|
2021-02-18 03:43:26 +01:00
|
|
|
aria-label={notificationsDisabled ? __('Turn on notifications') : __('Turn off notifications')}
|
2020-11-02 17:51:08 +01:00
|
|
|
onClick={() => {
|
|
|
|
const newNotificationsDisabled = !notificationsDisabled;
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
doChannelSubscribe(
|
|
|
|
{
|
|
|
|
channelName: claimName,
|
|
|
|
uri: permanentUrl,
|
|
|
|
notificationsDisabled: newNotificationsDisabled,
|
|
|
|
},
|
|
|
|
false
|
|
|
|
);
|
2020-11-02 17:51:08 +01:00
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
doToast({
|
|
|
|
message: __(
|
|
|
|
newNotificationsDisabled
|
|
|
|
? 'Notifications turned off for %channel%'
|
|
|
|
: 'Notifications turned on for %channel%!',
|
|
|
|
{ channel: claimName }
|
|
|
|
),
|
|
|
|
});
|
2021-10-27 16:38:10 +02:00
|
|
|
|
|
|
|
if (!newNotificationsDisabled && pushSupported && !pushEnabled) {
|
|
|
|
pushRequest();
|
|
|
|
}
|
2020-11-02 17:51:08 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2019-09-04 06:22:31 +02:00
|
|
|
) : null;
|
2019-06-11 20:10:58 +02:00
|
|
|
}
|