import { useCallback, useEffect, useMemo, useState } from 'react' import { Button, Col, Drawer, Row, Space, Switch, Typography } from 'antd' import IonIcon from '@sentre/antd-ionicon' import { MenuSystemItem } from 'view/sidebar/constants' import NotificationDrawer from './notificationDrawer' import { RootDispatch, useRootDispatch } from 'store' import { getNotifications, getUnreadNotifications, setNotifications, upsetUserNotification, } from 'store/notifications.reducer' import { useNotificationsSelector, useUserNotification, } from 'hooks/useNotifications' import { useIsLogin } from 'hooks/useWallet' type NotificationProps = { open?: boolean; onClose?: () => void } const Notification = ({ open = false, onClose = () => {}, }: NotificationProps) => { const dispatch = useRootDispatch() const [unreadOnly, setUnreadOnly] = useState(false) const isLogin = useIsLogin() const { notificationMark } = useUserNotification() const { _id: latestNotiId } = useNotificationsSelector(([noti]) => noti) || {} const onMarkAllAsRead = useCallback(async () => { if (!latestNotiId) return const userNotification = { notificationMark: latestNotiId, readIds: [], } await dispatch(upsetUserNotification({ userNotification })) if (unreadOnly) await dispatch(setNotifications({ notifications: [] })) }, [dispatch, latestNotiId, unreadOnly]) const isUnreadExist = useMemo( () => latestNotiId && latestNotiId !== notificationMark && isLogin, [isLogin, latestNotiId, notificationMark], ) useEffect(() => { const fetchNoti = unreadOnly ? getUnreadNotifications : getNotifications dispatch(fetchNoti({ offset: 0, isNew: true })) }, [unreadOnly, dispatch]) return ( )} } placement="left" closable={false} onClose={onClose} open={open} headerStyle={{ border: 'none' }} bodyStyle={{ padding: 0, paddingBottom: 12 }} > ) } export default Notification