lbry-desktop/ui/component/subscribeButton/view.jsx
Dan Peterson 03f69eff86
Browser push notifications (#133)
* fix type error

fix is subscribed check

- Persist subscription data locally
- add / remove subscription during log in / out
- Use store directly in hook

Add toast error if subscription fails

Revert removal of v2

hotfix linting issue

Add custom notification handler

- fix isSupported flag
- make icon color compatible with light/dark theme
- fix icon on notifications blocked banner

wip: add push notification banner to notifications page.

- ignore failed deletions via internal API
- add ua parsing package
- add more robust meta data to token save

refactor naming + add push toggle to notification button

shift some code around

update css naming o proper BEM notation

update notifications UI

remove now unneeded util function

Update push notification system to sue firebase sdk

separate service worker webpack bundling

update service worker to use firebase sdk

Add firebase config

Add firebase and remove filemanager

Stub out the basics for browser push notifications.

* fix safari

* try smaller image for badge

* add token validation with server, refactor code

* remove param

* add special icon for web notification badge

* add translations

* add missing trans for toast error

* add pushRequest method that will not prompt users who have subscribed but since disabled notifications in the settings.
2021-10-27 10:38:10 -04:00

161 lines
4.6 KiB
JavaScript

// @flow
import * as ICONS from 'constants/icons';
import React, { useRef } from 'react';
import { parseURI } from 'util/lbryURI';
import Button from 'component/button';
import useHover from 'effects/use-hover';
import { useIsMobile } from 'effects/use-screensize';
import { ENABLE_UI_NOTIFICATIONS } from 'config';
import useBrowserNotifications from '$web/component/browserNotificationSettings/use-browser-notifications';
type SubscriptionArgs = {
channelName: string,
uri: string,
notificationsDisabled?: boolean,
};
type Props = {
permanentUrl: ?string,
isSubscribed: boolean,
doChannelSubscribe: (SubscriptionArgs, boolean) => void,
doChannelUnsubscribe: (SubscriptionArgs, boolean) => void,
doToast: ({ message: string }) => void,
shrinkOnMobile: boolean,
notificationsDisabled: boolean,
user: ?User,
uri: string,
};
export default function SubscribeButton(props: Props) {
const {
permanentUrl,
doChannelSubscribe,
doChannelUnsubscribe,
isSubscribed,
doToast,
shrinkOnMobile = false,
notificationsDisabled,
user,
uri,
} = props;
const buttonRef = useRef();
const isMobile = useIsMobile();
let isHovering = useHover(buttonRef);
isHovering = isMobile ? true : isHovering;
const uiNotificationsEnabled = (user && user.experimental_ui) || ENABLE_UI_NOTIFICATIONS;
const { channelName: rawChannelName } = parseURI(uri);
let channelName;
if (permanentUrl) {
try {
const { channelName: name } = parseURI(permanentUrl);
if (name) {
channelName = name;
}
} catch (e) {}
}
const claimName = channelName && '@' + channelName;
const { pushSupported, pushEnabled, pushRequest } = useBrowserNotifications();
const subscriptionHandler = isSubscribed ? doChannelUnsubscribe : doChannelSubscribe;
const subscriptionLabel = isSubscribed
? __('Following --[button label indicating a channel has been followed]--')
: __('Follow');
const unfollowOverride = isSubscribed && isHovering && __('Unfollow');
const label = isMobile && shrinkOnMobile ? '' : unfollowOverride || subscriptionLabel;
const titlePrefix = isSubscribed ? __('Unfollow this channel') : __('Follow this channel');
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();
subscriptionHandler(
{
channelName: '@' + rawChannelName,
uri: uri,
notificationsDisabled: true,
},
true
);
}}
/>
</div>
);
}
return permanentUrl && claimName ? (
<div className="button-group">
<Button
ref={buttonRef}
iconColor="red"
largestLabel={isMobile && shrinkOnMobile ? '' : subscriptionLabel}
icon={unfollowOverride ? ICONS.UNSUBSCRIBE : isSubscribed ? ICONS.SUBSCRIBED : ICONS.SUBSCRIBE}
button={'alt'}
requiresAuth={IS_WEB}
label={label}
title={titlePrefix}
onClick={(e) => {
e.stopPropagation();
subscriptionHandler(
{
channelName: claimName,
uri: permanentUrl,
notificationsDisabled: true,
},
true
);
}}
/>
{isSubscribed && uiNotificationsEnabled && (
<Button
button="alt"
icon={notificationsDisabled ? ICONS.BELL : ICONS.BELL_ON}
aria-label={notificationsDisabled ? __('Turn on notifications') : __('Turn off notifications')}
onClick={() => {
const newNotificationsDisabled = !notificationsDisabled;
doChannelSubscribe(
{
channelName: claimName,
uri: permanentUrl,
notificationsDisabled: newNotificationsDisabled,
},
false
);
doToast({
message: __(
newNotificationsDisabled
? 'Notifications turned off for %channel%'
: 'Notifications turned on for %channel%!',
{ channel: claimName }
),
});
if (!newNotificationsDisabled && pushSupported && !pushEnabled) {
pushRequest();
}
}}
/>
)}
</div>
) : null;
}