import React, { MouseEvent, useCallback, useMemo } from 'react' import moment from 'moment' import { Col, Row, Image, Radio, Tooltip, Typography } from 'antd' import { RootDispatch, useRootDispatch } from 'store' import { isGuestAddress } from 'shared/util' import { NotificationData, setNotifications, upsetUserNotification, } from 'store/notifications.reducer' import NormalLogo from 'static/images/notification/normal-notification.png' import QuestLogo from 'static/images/notification/quest.png' import { useNotifications, useUserNotification } from 'hooks/useNotifications' import { useIsLogin, useWalletAddress } from 'hooks/useWallet' export type NotificationItemProps = { notification: NotificationData isBeforeMark: boolean unreadOnly: boolean } const NotificationItem = ({ notification: { _id, content, broadcastedAt, title, type, action }, isBeforeMark, unreadOnly, }: NotificationItemProps) => { const dispatch = useRootDispatch() const walletAddress = useWalletAddress() const notifications = useNotifications() const isLogin = useIsLogin() const { notificationMark, readIds, userAddress } = useUserNotification() const guest = useMemo(() => isGuestAddress(walletAddress), [walletAddress]) const logo = useMemo( () => (type === 'sentre' ? NormalLogo : QuestLogo), [type], ) const seen = useMemo(() => { if (isBeforeMark) return true if (readIds.includes(_id)) return true return false }, [_id, isBeforeMark, readIds]) const updateUserNotification = useCallback(async () => { const newUserNotification = { notificationMark, readIds: [...readIds], userAddress, } if (!newUserNotification.readIds.includes(_id)) newUserNotification.readIds.push(_id) return await dispatch( upsetUserNotification({ userNotification: newUserNotification, readOne: true, }), ) }, [_id, dispatch, notificationMark, readIds, userAddress]) const onRead = useCallback( async (e: MouseEvent) => { e.stopPropagation() if (seen || guest) return await updateUserNotification() if (unreadOnly) { const filteredNotifications = notifications.filter( (noti) => noti._id !== _id, ) await dispatch( setNotifications({ notifications: filteredNotifications }), ) } }, [ _id, dispatch, guest, notifications, seen, unreadOnly, updateUserNotification, ], ) const onAction = useCallback(async () => { if (guest) return if (!seen) await updateUserNotification() return window.open(action, 'blank') }, [action, guest, seen, updateUserNotification]) return ( alt {title} {content} {moment(broadcastedAt).fromNow()} {!seen ? ( Mark as read } > ) : ( )} ) } export default React.memo(NotificationItem)