From 799b9d843c745ac5473a169117156a2371a846ac Mon Sep 17 00:00:00 2001 From: Tyler Shipe Date: Mon, 12 Apr 2021 00:17:23 -0400 Subject: [PATCH] add notifications --- components/BalancesTable.tsx | 27 +++++++------ components/Notification.tsx | 72 +++++++++++++++++++++------------- components/OpenOrdersTable.tsx | 2 +- components/TradeForm.tsx | 11 +++--- hooks/useHydrateStore.tsx | 57 ++++----------------------- hooks/useWallet.tsx | 15 ++++++- pages/index.tsx | 7 +--- stores/useMangoStore.tsx | 7 ++++ utils/notifications.tsx | 18 ++++++++- 9 files changed, 109 insertions(+), 107 deletions(-) diff --git a/components/BalancesTable.tsx b/components/BalancesTable.tsx index 11dcbbec..ff04b9db 100644 --- a/components/BalancesTable.tsx +++ b/components/BalancesTable.tsx @@ -4,6 +4,7 @@ import useMangoStore from '../stores/useMangoStore' import { settleAll } from '../utils/mango' import useConnection from '../hooks/useConnection' import Button from '../components/Button' +import { notify } from '../utils/notifications' const BalancesTable = () => { const balances = useBalances() @@ -26,23 +27,23 @@ const BalancesTable = () => { markets, wallet ) - // notify({ - // message: 'Successfully settled funds', - // type: 'info', - // }); + notify({ + message: 'Successfully settled funds', + type: 'info', + }) } catch (e) { console.warn('Error settling all:', e) if (e.message === 'No unsettled funds') { - // notify({ - // message: 'There are no unsettled funds', - // type: 'error', - // }); + notify({ + message: 'There are no unsettled funds', + type: 'error', + }) } else { - // notify({ - // message: 'Error settling funds', - // description: e.message, - // type: 'error', - // }); + notify({ + message: 'Error settling funds', + description: e.message, + type: 'error', + }) } } } diff --git a/components/Notification.tsx b/components/Notification.tsx index 6f27e8cc..f86de132 100644 --- a/components/Notification.tsx +++ b/components/Notification.tsx @@ -1,8 +1,31 @@ -import { useState } from 'react' +import { useEffect, useState } from 'react' import xw from 'xwind' -import useInterval from '../hooks/useInterval' +import { + CheckCircleIcon, + InformationCircleIcon, + XCircleIcon, +} from '@heroicons/react/outline' +import useMangoStore from '../stores/useMangoStore' + +const NotificationList = () => { + const setMangoStore = useMangoStore((s) => s.set) + const notifications = useMangoStore((s) => s.notifications) + console.log('notifications', notifications) + + useEffect(() => { + if (notifications.length > 0) { + const id = setInterval(() => { + setMangoStore((state) => { + state.notifications = notifications.slice(1, notifications.length) + }) + }, 6000) + + return () => { + clearInterval(id) + } + } + }, [notifications, setMangoStore]) -const NotificationList = ({ notifications }) => { return (
{
{notifications.map((n, idx) => ( ))}
@@ -20,13 +45,9 @@ const NotificationList = ({ notifications }) => { ) } -const Notification = ({ title, message }) => { +const Notification = ({ type, message, description, txid }) => { const [showNotification, setShowNotification] = useState(true) - useInterval(() => { - setShowNotification(false) - }, 6000) - if (!showNotification) return null return ( @@ -36,25 +57,20 @@ const Notification = ({ title, message }) => {
- + {type === 'success' ? ( + + ) : null} + {type === 'info' && ( + + )} + {type === 'error' && ( + + )}
-
-

{title}

-

{message}

+
+

{message}

+

{description}

+ {txid ?

{txid}

: null}