import { useEffect, useState } from 'react' import { CheckCircleIcon, ExternalLinkIcon, InformationCircleIcon, XCircleIcon, } from '@heroicons/react/outline' import useMangoStore from '../stores/useMangoStore' import { notify } from '../utils/notifications' const NotificationList = () => { const setMangoStore = useMangoStore((s) => s.set) const notifications = useMangoStore((s) => s.notifications) const walletTokens = useMangoStore((s) => s.wallet.tokens) // if a notification is shown with {"InstructionError":[0,{"Custom":1}]} then // add a notification letting the user know they may not have enough SOL useEffect(() => { if (notifications.length) { const notEnoughSoLMessage = 'You may not have enough SOL for this transaction' const customErrorNotification = notifications.find( (n) => n.description && n.description.includes('"Custom":1') ) const notEnoughSolNotification = notifications.find( (n) => n.title && n.title.includes(notEnoughSoLMessage) ) const solBalance = walletTokens.find( (t) => t.config.symbol === 'SOL' )?.uiBalance if ( customErrorNotification && solBalance < 0.04 && !notEnoughSolNotification ) { notify({ title: notEnoughSoLMessage, type: 'info', }) } console.log('notifications', notifications) console.log('walletTokens', walletTokens) } }, [notifications, walletTokens]) useEffect(() => { if (notifications.length > 0) { const id = setInterval(() => { setMangoStore((state) => { state.notifications = notifications.slice(1, notifications.length) }) }, 5000) return () => { clearInterval(id) } } }, [notifications, setMangoStore]) const reversedNotifications = [...notifications].reverse() return (
{reversedNotifications.map((n, idx) => ( ))}
) } const Notification = ({ type, title, description, txid }) => { const [showNotification, setShowNotification] = useState(true) if (!showNotification) return null return (
{type === 'success' ? ( ) : null} {type === 'info' && ( )} {type === 'error' && ( )}
{title}
{description ? (

{description}

) : null} {txid ? ( {txid.slice(0, 8)}... {txid.slice(txid.length - 8)} ) : null}
) } export default NotificationList