lbry-desktop/web/component/browserNotificationSettings/use-browser-notifications.js
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

67 lines
2 KiB
JavaScript

// @flow
import { useEffect, useState, useMemo } from 'react';
import { pushSubscribe, pushUnsubscribe, pushIsSubscribed } from '$web/src/push-notifications';
import { isSupported } from 'firebase/messaging';
// @todo: Once we are on Redux 7 we should have proper hooks we can use here for store access.
import { store } from '$ui/store';
import { selectUser } from 'redux/selectors/user';
import { doToast } from 'redux/actions/notifications';
export default () => {
const [pushPermission, setPushPermission] = useState(window.Notification?.permission);
const [subscribed, setSubscribed] = useState(false);
const [pushEnabled, setPushEnabled] = useState(false);
const [pushSupported, setPushSupported] = useState(false);
const [user] = useState(selectUser(store.getState()));
useEffect(() => {
pushIsSubscribed(user.id).then((isSubscribed: boolean) => setSubscribed(isSubscribed));
isSupported().then((supported: boolean) => setPushSupported(supported));
}, [user]);
useMemo(() => setPushEnabled(pushPermission === 'granted' && subscribed), [pushPermission, subscribed]);
const subscribe = async () => {
if (await pushSubscribe(user.id)) {
setSubscribed(true);
setPushPermission(window.Notification?.permission);
} else {
showError();
}
};
const unsubscribe = async () => {
if (await pushUnsubscribe(user.id)) {
setSubscribed(false);
}
};
const pushToggle = async () => {
return !pushEnabled ? subscribe() : unsubscribe();
};
const pushRequest = async () => {
return window.Notification?.permission !== 'granted' ? subscribe() : null;
};
const showError = () => {
store.dispatch(
doToast({
isError: true,
message: __(
'There was an error enabling browser notifications. Please make sure your browser settings allow you to subscribe to notifications.'
),
})
);
};
return {
pushSupported,
pushEnabled,
pushPermission,
pushToggle,
pushRequest,
};
};