2020-07-23 16:22:57 +02:00
|
|
|
// @flow
|
2020-08-11 22:32:03 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-07-23 16:22:57 +02:00
|
|
|
import React from 'react';
|
|
|
|
import Page from 'component/page';
|
|
|
|
import Spinner from 'component/spinner';
|
2021-03-05 05:02:20 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2020-07-23 16:22:57 +02:00
|
|
|
import Notification from 'component/notification';
|
2020-08-11 22:32:03 +02:00
|
|
|
import Button from 'component/button';
|
2021-03-05 05:02:20 +01:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-08-21 21:44:54 +02:00
|
|
|
import Yrbl from 'component/yrbl';
|
2021-03-05 05:02:20 +01:00
|
|
|
import * as NOTIFICATIONS from 'constants/notifications';
|
2021-03-25 05:37:53 +01:00
|
|
|
import useFetched from 'effects/use-fetched';
|
2021-08-27 12:29:58 +02:00
|
|
|
import { RULE } from 'constants/notifications';
|
2021-03-25 05:37:53 +01:00
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
type Props = {
|
2020-08-21 21:44:54 +02:00
|
|
|
notifications: Array<Notification>,
|
2021-03-25 05:37:53 +01:00
|
|
|
notificationsFiltered: Array<Notification>,
|
2021-04-29 21:10:20 +02:00
|
|
|
notificationCategories: Array<NotificationCategory>,
|
2020-07-23 16:22:57 +02:00
|
|
|
fetching: boolean,
|
2020-08-10 22:47:39 +02:00
|
|
|
unreadCount: number,
|
2020-08-21 21:44:54 +02:00
|
|
|
unseenCount: number,
|
|
|
|
doSeeAllNotifications: () => void,
|
2020-08-10 22:47:39 +02:00
|
|
|
doReadNotifications: () => void,
|
2021-04-29 21:10:20 +02:00
|
|
|
doNotificationList: (?Array<string>) => void,
|
2021-08-27 12:29:58 +02:00
|
|
|
activeChannel: ?ChannelClaim,
|
|
|
|
doCommentReactList: (Array<string>) => Promise<any>,
|
2020-07-23 16:22:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function NotificationsPage(props: Props) {
|
2021-03-25 05:37:53 +01:00
|
|
|
const {
|
|
|
|
notifications,
|
|
|
|
notificationsFiltered,
|
|
|
|
fetching,
|
|
|
|
unreadCount,
|
|
|
|
unseenCount,
|
|
|
|
doSeeAllNotifications,
|
|
|
|
doReadNotifications,
|
|
|
|
doNotificationList,
|
2021-04-29 21:10:20 +02:00
|
|
|
notificationCategories,
|
2021-08-27 12:29:58 +02:00
|
|
|
activeChannel,
|
|
|
|
doCommentReactList,
|
2021-03-25 05:37:53 +01:00
|
|
|
} = props;
|
|
|
|
const initialFetchDone = useFetched(fetching);
|
2021-04-29 21:10:20 +02:00
|
|
|
const [name, setName] = usePersistedState('notifications--rule', NOTIFICATIONS.NOTIFICATION_NAME_ALL);
|
|
|
|
const isFiltered = name !== NOTIFICATIONS.NOTIFICATION_NAME_ALL;
|
2021-03-25 05:37:53 +01:00
|
|
|
const list = isFiltered ? notificationsFiltered : notifications;
|
2021-03-05 05:02:20 +01:00
|
|
|
|
2021-08-27 12:29:58 +02:00
|
|
|
// Fetch reacts
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [initialFetchDone, doCommentReactList, list, activeChannel]);
|
|
|
|
|
2021-03-05 05:02:20 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (unseenCount > 0 || unreadCount > 0) {
|
|
|
|
// If there are unread notifications when entering the page, reset to All.
|
2021-04-29 21:10:20 +02:00
|
|
|
setName(NOTIFICATIONS.NOTIFICATION_NAME_ALL);
|
2021-03-05 05:02:20 +01:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2020-08-10 22:47:39 +02:00
|
|
|
React.useEffect(() => {
|
2020-12-14 19:52:17 +01:00
|
|
|
if (unseenCount > 0) {
|
|
|
|
doSeeAllNotifications();
|
2020-08-10 22:47:39 +02:00
|
|
|
}
|
2020-12-14 19:52:17 +01:00
|
|
|
}, [unseenCount, doSeeAllNotifications]);
|
2020-08-10 22:47:39 +02:00
|
|
|
|
2021-04-29 21:10:20 +02:00
|
|
|
const stringifiedNotificationCategories = JSON.stringify(notificationCategories);
|
2021-03-25 05:37:53 +01:00
|
|
|
React.useEffect(() => {
|
2021-04-29 21:10:20 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 05:37:53 +01:00
|
|
|
}
|
2021-04-29 21:10:20 +02:00
|
|
|
}, [name, notifications, stringifiedNotificationCategories]);
|
2021-03-25 05:37:53 +01:00
|
|
|
|
|
|
|
const notificationListElement = (
|
|
|
|
<>
|
|
|
|
<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')} />
|
|
|
|
)}
|
2021-04-29 21:10:20 +02:00
|
|
|
{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>
|
|
|
|
)}
|
2021-03-25 05:37:53 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-29 21:10:20 +02:00
|
|
|
{list && list.length > 0 && !(isFiltered && fetching) ? (
|
2021-03-25 05:37:53 +01:00
|
|
|
<div className="card">
|
|
|
|
<div className="notification_list">
|
2021-08-27 12:29:58 +02:00
|
|
|
{list.map((notification) => {
|
2021-03-25 05:37:53 +01:00
|
|
|
return <Notification key={notification.id} notification={notification} />;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="main--empty">
|
|
|
|
{!fetching && (
|
|
|
|
<Yrbl
|
|
|
|
title={__('No notifications')}
|
|
|
|
subtitle={
|
2021-08-27 12:29:58 +02:00
|
|
|
isFiltered
|
|
|
|
? __('Try selecting another filter.')
|
|
|
|
: __("You don't have any notifications yet, but they will be here when you do!")
|
2021-03-25 05:37:53 +01:00
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button button="primary" icon={ICONS.HOME} label={__('Go Home')} navigate="/" />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
return (
|
|
|
|
<Page>
|
2021-03-25 05:37:53 +01:00
|
|
|
{initialFetchDone ? (
|
|
|
|
notificationListElement
|
|
|
|
) : fetching ? (
|
2020-07-23 16:22:57 +02:00
|
|
|
<div className="main--empty">
|
|
|
|
<Spinner delayed />
|
|
|
|
</div>
|
2021-03-25 05:37:53 +01:00
|
|
|
) : (
|
|
|
|
notificationListElement
|
2020-07-23 16:22:57 +02:00
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|