import TabButtons from '@components/shared/TabButtons' import mangoStore from '@store/mangoStore' import useMangoGroup from 'hooks/useMangoGroup' import { useTranslation } from 'next-i18next' import dynamic from 'next/dynamic' import { useEffect, useMemo, useState } from 'react' import { TokenStatsItem } from 'types' import { formatYAxis } from 'utils/formatting' const DetailedAreaChart = dynamic( () => import('@components/shared/DetailedAreaChart'), { ssr: false } ) const ChartTabs = ({ token }: { token: string }) => { const { t } = useTranslation('token') const [activeDepositsTab, setActiveDepositsTab] = useState('token:deposits') const [activeBorrowsTab, setActiveBorrowsTab] = useState('token:borrows') const [depositDaysToShow, setDepositDaysToShow] = useState('30') const [borrowDaysToShow, setBorrowDaysToShow] = useState('30') const [depositRateDaysToShow, setDepositRateDaysToShow] = useState('30') const [borrowRateDaysToShow, setBorrowRateDaysToShow] = useState('30') const tokenStats = mangoStore((s) => s.tokenStats.data) const initialStatsLoad = mangoStore((s) => s.tokenStats.initialLoad) const loadingTokenStats = mangoStore((s) => s.tokenStats.loading) const actions = mangoStore.getState().actions const { group } = useMangoGroup() useEffect(() => { if (group && !initialStatsLoad) { actions.fetchTokenStats() } }, [group]) const statsHistory = useMemo(() => { if (!tokenStats?.length) return [] return tokenStats.reduce((a: TokenStatsItem[], c: TokenStatsItem) => { if ( c.symbol === token || // ETH needs to be renamed ETH (Portal) in tokenStats db (c.symbol === 'ETH' && token === 'ETH (Portal)') ) { const copy = { ...c } copy.deposit_apr = copy.deposit_apr * 100 copy.borrow_apr = copy.borrow_apr * 100 a.push(copy) } return a.sort( (a, b) => new Date(a.date_hour).getTime() - new Date(b.date_hour).getTime() ) }, []) }, [tokenStats]) // const filterStats = (daysToShow: string) => { // if (!statsHistory.length) return [] // if (daysToShow !== '30') { // const seconds = Number(daysToShow) * 86400 // const data = statsHistory.filter((d) => { // const dataTime = new Date(d.date_hour).getTime() / 1000 // const now = new Date().getTime() / 1000 // const limit = now - seconds // return dataTime >= limit // }) // return data // } // return statsHistory // } return (
setActiveDepositsTab(v)} showBorders values={[ ['token:deposits', 0], ['token:deposit-rates', 0], ]} />
{activeDepositsTab === 'token:deposits' ? ( formatYAxis(x)} title={`${token} ${t('token:deposits')}`} xKey="date_hour" yKey={'total_deposits'} /> ) : ( `${x.toFixed(2)}%`} title={`${token} ${t('token:deposit-rates')} APR`} xKey="date_hour" yKey={'deposit_apr'} /> )}
setActiveBorrowsTab(v)} showBorders values={[ ['token:borrows', 0], ['token:borrow-rates', 0], ]} />
{activeBorrowsTab === 'token:borrows' ? ( formatYAxis(x)} title={`${token} ${t('token:borrows')}`} xKey="date_hour" yKey={'total_borrows'} /> ) : ( `${x.toFixed(2)}%`} title={`${token} ${t('token:borrow-rates')} APR`} xKey="date_hour" yKey={'borrow_apr'} /> )}
) } export default ChartTabs