import { Fragment, useEffect } from 'react'
import {
CheckCircleIcon,
ExternalLinkIcon,
InformationCircleIcon,
XCircleIcon,
} from '@heroicons/react/outline'
import useMangoStore, { CLUSTER } from '../stores/useMangoStore'
import { Notification, notify } from '../utils/notifications'
import { useTranslation } from 'next-i18next'
import Loading from './Loading'
import { Transition } from '@headlessui/react'
const NotificationList = () => {
const { t } = useTranslation('common')
const notifications = useMangoStore((s) => s.notifications)
const walletTokens = useMangoStore((s) => s.wallet.tokens)
const notEnoughSoLMessage = t('not-enough-sol')
// 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 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',
})
}
}
}, [notifications, walletTokens])
const reversedNotifications = [...notifications].reverse()
return (
{reversedNotifications.map((n) => (
))}
)
}
const Notification = ({ notification }: { notification: Notification }) => {
const { t } = useTranslation('common')
const setMangoStore = useMangoStore((s) => s.set)
const { type, title, description, txid, show, id } = notification
// overwrite the title if of the error message if it is a time out error
let parsedTitle
if (description) {
if (
description?.includes('Timed out awaiting') ||
description?.includes('was not confirmed')
) {
parsedTitle = 'Transaction status unknown'
}
}
// if the notification is a success, then hide the confirming tx notification with the same txid
useEffect(() => {
if ((type === 'error' || type === 'success') && txid) {
setMangoStore((s) => {
const newNotifications = s.notifications.map((n) =>
n.txid === txid && n.type === 'confirm' ? { ...n, show: false } : n
)
s.notifications = newNotifications
})
}
}, [type, txid])
const hideNotification = () => {
setMangoStore((s) => {
const newNotifications = s.notifications.map((n) =>
n.id === id ? { ...n, show: false } : n
)
s.notifications = newNotifications
})
}
// auto hide a notification after 8 seconds unless it is a confirming or time out notification
useEffect(() => {
const id = setTimeout(
() => {
if (show) {
hideNotification()
}
},
parsedTitle || type === 'confirm' || type === 'error' ? 40000 : 8000
)
return () => {
clearInterval(id)
}
})
return (
{type === 'success' ? (
) : null}
{type === 'info' && (
)}
{type === 'error' && (
)}
{type === 'confirm' && (
)}
)
}
export default NotificationList