lbry-desktop/ui/page/notifications/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

185 lines
5.8 KiB
JavaScript

// @flow
import * as ICONS from 'constants/icons';
import React from 'react';
import Page from 'component/page';
import Spinner from 'component/spinner';
import { FormField } from 'component/common/form';
import Notification from 'component/notification';
import Button from 'component/button';
import usePersistedState from 'effects/use-persisted-state';
import Yrbl from 'component/yrbl';
import * as NOTIFICATIONS from 'constants/notifications';
import useFetched from 'effects/use-fetched';
import { RULE } from 'constants/notifications';
import BrowserNotificationBanner from '$web/component/browserNotificationBanner';
type Props = {
notifications: Array<Notification>,
notificationsFiltered: Array<Notification>,
notificationCategories: Array<NotificationCategory>,
fetching: boolean,
unreadCount: number,
unseenCount: number,
doSeeAllNotifications: () => void,
doReadNotifications: () => void,
doNotificationList: (?Array<string>) => void,
activeChannel: ?ChannelClaim,
doCommentReactList: (Array<string>) => Promise<any>,
};
export default function NotificationsPage(props: Props) {
const {
notifications,
notificationsFiltered,
fetching,
unreadCount,
unseenCount,
doSeeAllNotifications,
doReadNotifications,
doNotificationList,
notificationCategories,
activeChannel,
doCommentReactList,
} = props;
const initialFetchDone = useFetched(fetching);
const [name, setName] = usePersistedState('notifications--rule', NOTIFICATIONS.NOTIFICATION_NAME_ALL);
const isFiltered = name !== NOTIFICATIONS.NOTIFICATION_NAME_ALL;
const list = isFiltered ? notificationsFiltered : notifications;
// Fetch reacts
React.useEffect(() => {
if ((!fetching || initialFetchDone) && activeChannel) {
let idsForReactionFetch = [];
list.map((notification) => {
const { notification_rule, notification_parameters } = notification;
const isComment =
notification_rule === RULE.COMMENT ||
notification_rule === RULE.COMMENT_REPLY ||
notification_rule === RULE.CREATOR_COMMENT;
const commentId =
isComment &&
notification_parameters &&
notification_parameters.dynamic &&
notification_parameters.dynamic.hash;
if (commentId) {
idsForReactionFetch.push(commentId);
}
});
if (idsForReactionFetch.length !== 0) {
doCommentReactList(idsForReactionFetch);
}
}
}, [doCommentReactList, list, activeChannel, fetching, initialFetchDone]);
React.useEffect(() => {
if (unseenCount > 0 || unreadCount > 0) {
// If there are unread notifications when entering the page, reset to All.
setName(NOTIFICATIONS.NOTIFICATION_NAME_ALL);
}
}, []);
React.useEffect(() => {
if (unseenCount > 0) {
doSeeAllNotifications();
}
}, [unseenCount, doSeeAllNotifications]);
const stringifiedNotificationCategories = JSON.stringify(notificationCategories);
React.useEffect(() => {
if (stringifiedNotificationCategories) {
const arrayNotificationCategories = JSON.parse(stringifiedNotificationCategories);
if (name !== NOTIFICATIONS.NOTIFICATION_NAME_ALL) {
// Fetch filtered list when:
// (1) 'name' changed
// (2) new "all" notifications received (e.g. from websocket).
try {
const matchingCategory = arrayNotificationCategories.find((category) => category.name === name);
if (matchingCategory) {
doNotificationList(matchingCategory.types);
}
} catch (e) {
console.error(e);
}
}
}
}, [name, notifications, stringifiedNotificationCategories]);
const notificationListElement = (
<>
<BrowserNotificationBanner />
<div className="claim-list__header">
<h1 className="card__title">{__('Notifications')}</h1>
<div className="claim-list__alt-controls--wrap">
{fetching && <Spinner type="small" />}
{unreadCount > 0 && (
<Button icon={ICONS.EYE} onClick={doReadNotifications} button="secondary" label={__('Mark all as read')} />
)}
{notificationCategories && (
<FormField
className="notification__filter"
type="select"
name="filter"
value={name}
onChange={(e) => setName(e.target.value)}
>
{notificationCategories.map((category) => {
return (
<option key={category.name} value={category.name}>
{__(category.name)}
</option>
);
})}
</FormField>
)}
</div>
</div>
{list && list.length > 0 && !(isFiltered && fetching) ? (
<div className="card">
<div className="notification_list">
{list.map((notification) => {
return <Notification key={notification.id} notification={notification} />;
})}
</div>
</div>
) : (
<div className="main--empty">
{!fetching && (
<Yrbl
title={__('No notifications')}
subtitle={
isFiltered
? __('Try selecting another filter.')
: __("You don't have any notifications yet, but they will be here when you do!")
}
actions={
<div className="section__actions">
<Button button="primary" icon={ICONS.HOME} label={__('Go Home')} navigate="/" />
</div>
}
/>
)}
</div>
)}
</>
);
return (
<Page>
{initialFetchDone ? (
notificationListElement
) : fetching ? (
<div className="main--empty">
<Spinner delayed />
</div>
) : (
notificationListElement
)}
</Page>
);
}