// @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, notificationsFiltered: Array, notificationCategories: Array, fetching: boolean, unreadCount: number, unseenCount: number, doSeeAllNotifications: () => void, doReadNotifications: () => void, doNotificationList: (?Array) => void, doNotificationCategories: () => void, activeChannel: ?ChannelClaim, doCommentReactList: (Array) => Promise, }; export default function NotificationsPage(props: Props) { const { notifications, notificationsFiltered, fetching, unreadCount, unseenCount, doSeeAllNotifications, doReadNotifications, doNotificationList, doNotificationCategories, notificationCategories, activeChannel, doCommentReactList, } = props; const [name, setName] = usePersistedState('notifications--rule', NOTIFICATIONS.NOTIFICATION_NAME_ALL); const isFiltered = name !== NOTIFICATIONS.NOTIFICATION_NAME_ALL; const list = isFiltered ? notificationsFiltered : notifications; const fetchedOnce = useFetched(fetching); const categoriesReady = notificationCategories; const notificationsReady = !isFiltered || fetchedOnce; const ready = categoriesReady && notificationsReady; // Fetch reacts React.useEffect(() => { if (ready && !fetching && 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); } } }, [ready, doCommentReactList, list, activeChannel, fetching]); // Mark all as seen React.useEffect(() => { if (unseenCount > 0) { doSeeAllNotifications(); } }, [unseenCount, doSeeAllNotifications]); const stringifiedNotificationCategories = notificationCategories ? JSON.stringify(notificationCategories) : ''; // Fetch filtered notifications React.useEffect(() => { if (stringifiedNotificationCategories) { const arrayNotificationCategories = JSON.parse(stringifiedNotificationCategories); if (name !== NOTIFICATIONS.NOTIFICATION_NAME_ALL) { try { const matchingCategory = arrayNotificationCategories.find((category) => category.name === name); if (matchingCategory) { doNotificationList(matchingCategory.types); } } catch (e) { console.error(e); // eslint-disable-line no-console } } } }, [name, notifications, stringifiedNotificationCategories, doNotificationList]); React.useEffect(() => { if (!notificationCategories) { doNotificationCategories(); } }, []); // eslint-disable-line react-hooks/exhaustive-deps return ( {ready && (

{__('Notifications')}

{fetching && } {unreadCount > 0 && (
)} {!ready ? (
) : list && list.length > 0 && !(isFiltered && fetching) ? (
{list.map((notification) => { return ; })}
) : (
{!fetching && (
} /> )} )}
); }