2020-07-23 16:22:57 +02:00
|
|
|
// @flow
|
2020-07-30 22:14:12 +02:00
|
|
|
|
2020-09-16 18:35:59 +02:00
|
|
|
import * as NOTIFICATIONS from 'constants/notifications';
|
2020-09-15 16:00:35 +02:00
|
|
|
import * as PAGES from 'constants/pages';
|
2020-07-23 16:22:57 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
import React from 'react';
|
2020-08-21 21:44:54 +02:00
|
|
|
import classnames from 'classnames';
|
2020-07-23 16:22:57 +02:00
|
|
|
import Icon from 'component/common/icon';
|
|
|
|
import DateTime from 'component/dateTime';
|
2020-08-31 19:12:34 +02:00
|
|
|
import Button from 'component/button';
|
2020-07-23 16:22:57 +02:00
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
|
|
|
import { MenuItem } from '@reach/menu-button';
|
|
|
|
import { formatLbryUrlForWeb } from 'util/url';
|
|
|
|
import { useHistory } from 'react-router';
|
2020-08-31 19:12:34 +02:00
|
|
|
import { parseURI } from 'lbry-redux';
|
|
|
|
import { PAGE_VIEW_QUERY, DISCUSSION_PAGE } from 'page/channel/view';
|
2020-07-23 16:22:57 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
notification: WebNotification,
|
|
|
|
menuButton: boolean,
|
|
|
|
children: any,
|
2020-08-21 21:44:54 +02:00
|
|
|
doSeeNotifications: ([number]) => void,
|
2020-07-23 16:22:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function Notification(props: Props) {
|
2020-08-21 21:44:54 +02:00
|
|
|
const { notification, menuButton = false, doSeeNotifications } = props;
|
2020-07-23 16:22:57 +02:00
|
|
|
const { push } = useHistory();
|
2020-08-21 21:44:54 +02:00
|
|
|
const { notification_rule, notification_parameters, is_seen, id } = notification;
|
2020-09-16 18:35:59 +02:00
|
|
|
const isCommentNotification =
|
|
|
|
notification_rule === NOTIFICATIONS.NOTIFICATION_COMMENT || notification_rule === NOTIFICATIONS.NOTIFICATION_REPLY;
|
2020-09-08 22:56:48 +02:00
|
|
|
const commentText = isCommentNotification && notification_parameters.dynamic.comment;
|
2020-09-16 18:35:59 +02:00
|
|
|
|
|
|
|
let notificationTarget;
|
|
|
|
switch (notification_rule) {
|
|
|
|
case NOTIFICATIONS.DAILY_WATCH_AVAILABLE:
|
|
|
|
case NOTIFICATIONS.DAILY_WATCH_REMIND:
|
|
|
|
notificationTarget = `/$/${PAGES.CHANNELS_FOLLOWING}`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
notificationTarget = notification_parameters.device.target;
|
|
|
|
}
|
|
|
|
|
2020-07-30 22:14:12 +02:00
|
|
|
let notificationLink = formatLbryUrlForWeb(notificationTarget);
|
2020-08-31 19:12:34 +02:00
|
|
|
let urlParams = new URLSearchParams();
|
2020-09-08 22:56:48 +02:00
|
|
|
if (isCommentNotification && notification_parameters.dynamic.hash) {
|
2020-08-31 19:12:34 +02:00
|
|
|
urlParams.append('lc', notification_parameters.dynamic.hash);
|
2020-07-30 22:14:12 +02:00
|
|
|
}
|
2020-07-23 16:22:57 +02:00
|
|
|
|
2020-08-31 19:12:34 +02:00
|
|
|
try {
|
|
|
|
const { isChannel } = parseURI(notificationTarget);
|
|
|
|
if (isChannel) {
|
|
|
|
urlParams.append(PAGE_VIEW_QUERY, DISCUSSION_PAGE);
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
notificationLink += `?${urlParams.toString()}`;
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
let icon;
|
2020-08-21 21:44:54 +02:00
|
|
|
switch (notification_rule) {
|
2020-09-16 18:35:59 +02:00
|
|
|
case NOTIFICATIONS.NOTIFICATION_CREATOR_SUBSCRIBER:
|
2020-07-23 16:22:57 +02:00
|
|
|
icon = <Icon icon={ICONS.SUBSCRIBE} sectionIcon className="notification__icon" />;
|
|
|
|
break;
|
2020-09-16 18:35:59 +02:00
|
|
|
case NOTIFICATIONS.NOTIFICATION_COMMENT:
|
2020-08-21 21:44:54 +02:00
|
|
|
icon = <ChannelThumbnail small uri={notification_parameters.dynamic.comment_author} />;
|
2020-07-23 16:22:57 +02:00
|
|
|
break;
|
2020-09-16 18:35:59 +02:00
|
|
|
case NOTIFICATIONS.NOTIFICATION_REPLY:
|
2020-09-08 22:56:48 +02:00
|
|
|
icon = <ChannelThumbnail small uri={notification_parameters.dynamic.reply_author} />;
|
|
|
|
break;
|
2020-07-23 16:22:57 +02:00
|
|
|
default:
|
|
|
|
icon = <Icon icon={ICONS.NOTIFICATION} sectionIcon className="notification__icon" />;
|
|
|
|
}
|
|
|
|
|
2020-08-21 21:44:54 +02:00
|
|
|
function handleNotificationClick() {
|
|
|
|
if (!is_seen) {
|
|
|
|
doSeeNotifications([id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notificationLink) {
|
|
|
|
push(notificationLink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:12:34 +02:00
|
|
|
function handleSeeNotification(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
doSeeNotifications([id]);
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
const Wrapper = menuButton
|
|
|
|
? (props: { children: any }) => (
|
2020-08-21 21:44:54 +02:00
|
|
|
<MenuItem className="menu__link--notification" onSelect={handleNotificationClick}>
|
2020-07-23 16:22:57 +02:00
|
|
|
{props.children}
|
|
|
|
</MenuItem>
|
|
|
|
)
|
2020-08-21 21:44:54 +02:00
|
|
|
: notificationLink
|
|
|
|
? (props: { children: any }) => (
|
|
|
|
<a className="menu__link--notification" onClick={handleNotificationClick}>
|
2020-07-23 16:22:57 +02:00
|
|
|
{props.children}
|
|
|
|
</a>
|
2020-08-21 21:44:54 +02:00
|
|
|
)
|
|
|
|
: (props: { children: any }) => (
|
|
|
|
<span
|
|
|
|
className={is_seen ? 'menu__link--notification-nolink' : 'menu__link--notification'}
|
|
|
|
onClick={handleNotificationClick}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</span>
|
2020-07-23 16:22:57 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Wrapper>
|
2020-08-21 21:44:54 +02:00
|
|
|
<div
|
|
|
|
className={classnames('notification__wrapper', {
|
|
|
|
'notification__wrapper--unseen': !is_seen,
|
|
|
|
})}
|
|
|
|
>
|
2020-07-23 16:22:57 +02:00
|
|
|
<div className="notification__icon">{icon}</div>
|
|
|
|
<div className="notification__content">
|
2020-07-30 22:14:12 +02:00
|
|
|
<div>
|
2020-09-08 22:56:48 +02:00
|
|
|
{!isCommentNotification && (
|
2020-08-21 21:44:54 +02:00
|
|
|
<div className="notification__title">{notification_parameters.device.title}</div>
|
2020-07-30 22:14:12 +02:00
|
|
|
)}
|
|
|
|
|
2020-09-08 22:56:48 +02:00
|
|
|
{isCommentNotification && commentText ? (
|
2020-08-21 21:44:54 +02:00
|
|
|
<>
|
|
|
|
<div className="notification__title">{notification_parameters.device.title}</div>
|
|
|
|
<div className="notification__text mobile-hidden">{commentText}</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="notification__text">{notification_parameters.device.text}</div>
|
|
|
|
</>
|
|
|
|
)}
|
2020-07-30 22:14:12 +02:00
|
|
|
</div>
|
|
|
|
|
2020-08-31 19:12:34 +02:00
|
|
|
<div className="notification__extra">
|
|
|
|
<div className="notification__time">
|
2020-09-15 15:54:05 +02:00
|
|
|
<DateTime timeAgo date={notification.active_at} />
|
2020-08-31 19:12:34 +02:00
|
|
|
</div>
|
|
|
|
{!is_seen && <Button className="notification__mark-seen" onClick={handleSeeNotification} />}
|
2020-07-23 16:22:57 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Wrapper>
|
|
|
|
);
|
|
|
|
}
|