import Button, { IconButton, LinkButton } from '@components/shared/Button' import { Dialog, Transition } from '@headlessui/react' import { CalendarIcon, FaceSmileIcon, InboxIcon, TrashIcon, XMarkIcon, } from '@heroicons/react/20/solid' import { useWallet } from '@solana/wallet-adapter-react' import { useHeaders } from 'hooks/notifications/useHeaders' import { useIsAuthorized } from 'hooks/notifications/useIsAuthorized' import { useNotifications } from 'hooks/notifications/useNotifications' import { Fragment, useCallback, useEffect, useMemo, useState } from 'react' import { NOTIFICATION_API } from 'utils/constants' import NotificationCookieStore from '@store/notificationCookieStore' import dayjs from 'dayjs' import { useTranslation } from 'next-i18next' import { createLedgerMessage, createSolanaMessage, notify, } from 'utils/notifications' import mangoStore from '@store/mangoStore' import { ttCommons, ttCommonsExpanded, ttCommonsMono } from 'utils/fonts' const NotificationsDrawer = ({ isOpen, onClose, }: { isOpen: boolean onClose: () => void }) => { const { t } = useTranslation('notifications') const { data, refetch } = useNotifications() const connection = mangoStore((s) => s.connection) const wallet = useWallet() const isAuth = useIsAuthorized() const headers = useHeaders() const setCookie = NotificationCookieStore((s) => s.setCookie) const [isRemoving, setIsRemoving] = useState(false) const unseenNotifications = useMemo(() => { if (!data || !data.length) return [] return data.filter((x) => !x.seen) }, [data]) const markAsSeen = useCallback( async (ids: number[]) => { try { const resp = await fetch(`${NOTIFICATION_API}notifications/seen`, { method: 'POST', headers: headers.headers, body: JSON.stringify({ ids: ids, seen: true, }), }) const body = await resp.json() const error = body.error if (error) { notify({ type: 'error', title: 'Error', description: error, }) return } refetch() } catch (e) { notify({ type: 'error', title: 'Error', description: JSON.stringify(e), }) } }, [NOTIFICATION_API, headers], ) const remove = useCallback( async (ids: number[]) => { setIsRemoving(true) try { const resp = await fetch( `${NOTIFICATION_API}notifications/removeForUser`, { method: 'POST', headers: headers.headers, body: JSON.stringify({ ids: ids, }), }, ) const body = await resp.json() const error = body.error if (error) { notify({ type: 'error', title: 'Error', description: error, }) return } refetch() } catch (e) { notify({ type: 'error', title: 'Error', description: JSON.stringify(e), }) } setIsRemoving(false) }, [NOTIFICATION_API, headers], ) // Mark all notifications as seen when the inbox is opened useEffect(() => { if (isOpen && unseenNotifications?.length) { markAsSeen([...unseenNotifications.map((x) => x.id)]) } }, [isOpen, unseenNotifications]) return (

{t('notifications')}

{data?.length ? ( remove(data.map((n) => n.id))} > {t('clear-all')} ) : null}
{isAuth ? ( <> {data?.length ? ( <>
{data?.map((notification) => (

{notification.title}

remove([notification.id])} className="mt-1 text-th-fgd-3" hideBg >

{dayjs(notification.createdAt).format( 'DD MMM YYYY, h:mma', )}

{notification.content}

))}
) : (

{t('empty-state-title')}

{t('empty-state-desc')}

)} ) : (

{t('unauth-title')}

{t('unauth-desc')}

createLedgerMessage(wallet, setCookie, connection) } > {t('sign-using-ledger')}
)}
) } export default NotificationsDrawer