lbry-desktop/ui/page/notifications/view.jsx

114 lines
3.5 KiB
React
Raw Normal View History

2020-07-23 16:22:57 +02:00
// @flow
2020-08-11 22:32:03 +02:00
import * as ICONS from 'constants/icons';
import { NOTIFICATION_COMMENT } from 'constants/notifications';
2020-07-23 16:22:57 +02:00
import React from 'react';
import Page from 'component/page';
import Card from 'component/common/card';
import Spinner from 'component/spinner';
import Notification from 'component/notification';
2020-08-11 22:32:03 +02:00
import Yrbl from 'component/yrbl';
import Button from 'component/button';
2020-07-23 16:22:57 +02:00
type Props = {
notifications: ?Array<Notification>,
fetching: boolean,
2020-08-10 22:47:39 +02:00
unreadCount: number,
doReadNotifications: () => void,
2020-07-23 16:22:57 +02:00
};
export default function NotificationsPage(props: Props) {
2020-08-10 22:47:39 +02:00
const { notifications, fetching, unreadCount, doReadNotifications } = props;
2020-07-23 16:22:57 +02:00
// Group sequential comment notifications if they are by the same author
let groupedCount = 1;
const groupedNotifications =
notifications &&
notifications.reduce((list, notification, index) => {
if (index === 0) {
return [notification];
}
const previousNotification = notifications[index - 1];
const isCommentNotification = notification.notification_rule === NOTIFICATION_COMMENT;
const previousIsCommentNotification = previousNotification.notification_rule === NOTIFICATION_COMMENT;
if (isCommentNotification && previousIsCommentNotification) {
const notificationTarget = notification.notification_parameters.device.target;
const previousTarget = previousNotification && previousNotification.notification_parameters.device.target;
const author = notification.notification_parameters.dynamic.comment_author;
const previousAuthor = previousNotification.notification_parameters.dynamic.comment_author;
if (author === previousAuthor && notificationTarget === previousTarget) {
const newList = [...list];
newList.pop();
groupedCount += 1;
const newNotification = {
...previousNotification,
group_count: groupedCount,
};
newList[index - groupedCount] = newNotification;
return newList;
} else {
if (groupedCount > 1) {
groupedCount = 1;
}
return [...list, notification];
}
} else {
if (groupedCount > 1) {
groupedCount = 1;
}
return [...list, notification];
}
}, []);
2020-08-10 22:47:39 +02:00
React.useEffect(() => {
if (unreadCount > 0) {
doReadNotifications();
}
}, [unreadCount, doReadNotifications]);
2020-07-23 16:22:57 +02:00
return (
<Page>
{fetching && (
<div className="main--empty">
<Spinner delayed />
</div>
)}
{groupedNotifications && groupedNotifications.length > 0 ? (
2020-07-23 16:22:57 +02:00
<Card
isBodyList
title={__('Notifications')}
body={
<div className="notification_list">
{groupedNotifications.map((notification, index) => {
if (!notification) {
return null;
}
2020-07-23 16:22:57 +02:00
return <Notification key={notification.id} notification={notification} />;
})}
</div>
}
/>
) : (
2020-08-11 22:32:03 +02:00
<div className="main--empty">
<Yrbl
title={__('No Notifications')}
subtitle={
<div>
<p>{__("You don't have any notifications yet, but they will be here when you do!")}</p>
<div className="section__actions">
<Button button="primary" icon={ICONS.HOME} label={__('Go Home')} navigate="/" />
</div>
</div>
}
/>
</div>
2020-07-23 16:22:57 +02:00
)}
</Page>
);
}