import React, { FC, useMemo, useState, useCallback, useEffect } from 'react'; import { Icon } from '~/components/input/Icon'; import styles from './styles.module.scss'; import { connect } from 'react-redux'; import { selectAuthUpdates, selectAuthUser } from '~/redux/auth/selectors'; import { pick } from 'ramda'; import classNames from 'classnames'; import * as AUTH_ACTIONS from '~/redux/auth/actions'; import { NotificationBubble } from '../../notifications/NotificationBubble'; import { INotification, IMessageNotification } from '~/redux/types'; const mapStateToProps = state => ({ user: pick(['last_seen_messages'], selectAuthUser(state)), updates: selectAuthUpdates(state), }); const mapDispatchToProps = { authSetLastSeenMessages: AUTH_ACTIONS.authSetLastSeenMessages, authOpenProfile: AUTH_ACTIONS.authOpenProfile, }; type IProps = ReturnType & typeof mapDispatchToProps & {}; const NotificationsUnconnected: FC = ({ updates: { last, notifications }, user: { last_seen_messages }, authSetLastSeenMessages, authOpenProfile, }) => { const [visible, setVisible] = useState(false); const has_new = useMemo( () => notifications.length && last && Date.parse(last) && (!last_seen_messages || (Date.parse(last_seen_messages) && Date.parse(last) > Date.parse(last_seen_messages))), [last, last_seen_messages, notifications] ); const onNotificationClick = useCallback( (notification: INotification) => { switch (notification.type) { case 'message': return authOpenProfile( (notification as IMessageNotification).content.from.username, 'messages' ); default: return; } }, [authOpenProfile] ); const showList = useCallback(() => setVisible(true), [setVisible]); const hideList = useCallback(() => setVisible(false), [setVisible]); useEffect(() => { if (!visible || !has_new || !last) return; authSetLastSeenMessages(last); }, [visible, last]); return (
0, })} >
{has_new ? : }
{visible && ( )}
); }; const Notifications = connect( mapStateToProps, mapDispatchToProps )(NotificationsUnconnected); export { Notifications };