import { useEffect, useState } from 'react' import useMangoStore from '../stores/useMangoStore' import dayjs from 'dayjs' import { BadgeCheckIcon, BellIcon, InformationCircleIcon, LinkIcon, PlusCircleIcon, TrashIcon, } from '@heroicons/react/outline' import { RadioGroup } from '@headlessui/react' import useAlertsStore from '../stores/useAlertsStore' import useLocalStorageState from '../hooks/useLocalStorageState' import TopBar from '../components/TopBar' import Button, { LinkButton } from '../components/Button' import AlertsModal from '../components/AlertsModal' import AlertItem from '../components/AlertItem' import PageBodyContainer from '../components/PageBodyContainer' import EmptyState from '../components/EmptyState' import Select from '../components/Select' import { abbreviateAddress } from '../utils' const relativeTime = require('dayjs/plugin/relativeTime') dayjs.extend(relativeTime) const TABS = ['Active', 'Triggered'] export default function Alerts() { const connected = useMangoStore((s) => s.wallet.connected) const marginAccounts = useMangoStore((s) => s.marginAccounts) const [activeTab, setActiveTab] = useState(TABS[0]) const [openAlertModal, setOpenAlertModal] = useState(false) const [reactivateAlertData, setReactivateAlertData] = useState(null) const [acc, setAcc] = useState('All') const [filteredActiveAlerts, setFilteredActiveAlerts] = useState([]) const [filteredTriggeredAlerts, setFilteredTriggeredAlerts] = useState([]) const activeAlerts = useAlertsStore((s) => s.activeAlerts) const triggeredAlerts = useAlertsStore((s) => s.triggeredAlerts) const loading = useAlertsStore((s) => s.loading) const [clearedAlerts, setClearedAlerts] = useState([]) const [clearAlertsTimestamp, setClearAlertsTimestamp] = useLocalStorageState( 'clearAlertsTimestamp' ) useEffect(() => { if (!connected || loading) { setAcc('All') } }, [connected, loading]) useEffect(() => { if (clearAlertsTimestamp && !loading) { const filterByTimestamp = triggeredAlerts.filter( (alert) => alert.triggeredTimestamp > clearAlertsTimestamp && Object.keys(alert).includes('triggeredTimestamp') ) setClearedAlerts(filterByTimestamp) } }, [clearAlertsTimestamp, loading]) const handleTabChange = (tabName) => { setActiveTab(tabName) } const handleAccountChange = (val) => { if (val !== 'All') { setAcc(val) const showActive = activeAlerts.filter( (alert) => alert.acc.toString() === val ) const showTriggered = clearAlertsTimestamp ? clearedAlerts.filter((alert) => alert.acc.toString() === val) : triggeredAlerts.filter((alert) => alert.acc.toString() === val) setFilteredActiveAlerts(showActive) setFilteredTriggeredAlerts(showTriggered) } else { setAcc(val) } } return (

Alerts

{marginAccounts.length === 2 ? ( handleAccountChange(val)} className="flex border border-th-fgd-4 rounded-md w-full mt-3 sm:mt-0 sm:w-80 h-full text-xs h-8" > {({ checked }) => ( )} {marginAccounts.map((acc, i) => ( {({ checked }) => ( )} ))} ) : null} {marginAccounts.length > 2 ? ( ) : null}
{loading ? ( <>
) } const TabContent = ({ acc, activeTab, activeAlerts, clearedAlerts, clearAlertsTimestamp, filteredActiveAlerts, filteredTriggeredAlerts, setClearAlertsTimestamp, setOpenAlertModal, setReactivateAlertData, triggeredAlerts, }) => { switch (activeTab) { case 'Active': return activeAlerts.length === 0 ? (
No Alerts Found

Get notified when your account is in danger of liquidation.

) : ( <>

Active alerts will only trigger once.

{acc === 'All' ? activeAlerts.map((alert) => ( )) : filteredActiveAlerts.map((alert) => ( ))} ) case 'Triggered': return (
{triggeredAlerts.length === 0 || (clearAlertsTimestamp && clearedAlerts.length === 0) || (acc !== 'All' && filteredTriggeredAlerts.length === 0) ? (
Smooth Sailing

None of your active liquidation alerts have been triggered.

) : ( <>

Re-activate alerts you want to receive again.

setClearAlertsTimestamp( triggeredAlerts[0].triggeredTimestamp ) } >
Clear
{acc === 'All' ? clearAlertsTimestamp ? clearedAlerts.map((alert) => ( )) : triggeredAlerts.map((alert) => ( )) : filteredTriggeredAlerts.map((alert) => ( ))} )}
) default: return activeAlerts.map((alert) => ( )) } }