import { useState, useEffect } from 'react' import { useQuery } from '@apollo/client' import { AMOUNT_STATISTIC } from '@graphql/queries' import { Card } from '@components/tailwind/Card' import { formatUSD } from '@utils/formatUSD' interface HolisticStatsProps { platform: string setPlatform: (platform: string) => void loading: boolean chainID?: string baseVariables?: any noMessaging?: boolean } export const HolisticStats = ({ platform: parentPlatform, setPlatform: parentSetPlatform, baseVariables, noMessaging, }: HolisticStatsProps) => { const [volume, setVolume] = useState('--') const [fee, setFee] = useState('--') const [addresses, setAddresses] = useState('--') const [txs, setTxs] = useState('--') const [variables, setVariables] = useState({}) const [platform, setPlatform] = useState(true) useEffect(() => { setVariables(baseVariables) }, [baseVariables]) const unSelectStyle = 'transition ease-out border-l-0 border-gray-700 border-opacity-30 text-gray-500 bg-gray-700 bg-opacity-30 hover:bg-opacity-20 hover:text-white' const selectStyle = 'text-white border-[#BE78FF] bg-synapse-radial' const handleVariable = (type: string) => { const queryVariables = JSON.parse(JSON.stringify(variables)) queryVariables['type'] = type return queryVariables } const { loading: loadingVolume } = useQuery(AMOUNT_STATISTIC, { fetchPolicy: 'network-only', variables: baseVariables ? handleVariable('TOTAL_VOLUME_USD') : { platform, duration: 'ALL_TIME', type: 'TOTAL_VOLUME_USD', useCache: true, useMv: true, }, onCompleted: (data: any) => { setVolume(data.amountStatistic.value) }, }) const { loading: loadingFee } = useQuery(AMOUNT_STATISTIC, { fetchPolicy: 'network-only', variables: baseVariables ? handleVariable('TOTAL_FEE_USD') : { platform, duration: 'ALL_TIME', type: 'TOTAL_FEE_USD', useCache: true, }, onCompleted: (data: any) => { setFee(data.amountStatistic.value) }, }) const { loading: loadingAddresses } = useQuery(AMOUNT_STATISTIC, { fetchPolicy: 'network-only', variables: baseVariables ? handleVariable('COUNT_ADDRESSES') : { platform, duration: 'ALL_TIME', type: 'COUNT_ADDRESSES', useCache: true, }, onCompleted: (data: any) => { setAddresses(data.amountStatistic.value) }, }) const { loading: loadingTxs } = useQuery(AMOUNT_STATISTIC, { fetchPolicy: 'network-only', variables: baseVariables ? handleVariable('COUNT_TRANSACTIONS') : { platform, duration: 'ALL_TIME', type: 'COUNT_TRANSACTIONS', useCache: true, }, onCompleted: (data: any) => { setTxs(data.amountStatistic.value) }, }) useEffect(() => { setPlatform(parentPlatform) }, [parentPlatform]) useEffect(() => { setVolume('--') setFee('--') setAddresses('--') setTxs('--') }, [platform]) const stats = [ parentPlatform === 'MESSAGE_BUS' ? null : { title: 'Volume', usd: true, loading: loadingVolume, value: formatUSD(volume), }, { title: 'Transactions', loading: loadingTxs, usd: false, value: formatUSD(txs), }, parentPlatform === 'MESSAGE_BUS' ? null : { title: 'Addresses', loading: loadingAddresses, usd: false, value: formatUSD(addresses), }, parentPlatform === 'MESSAGE_BUS' ? null : { title: 'Fees', loading: loadingFee, usd: true, value: formatUSD(fee), }, ] return ( <>
{noMessaging ? null : ( )}
{stats.map((stat, i) => { return stat && stat.value !== '--' ? (
{stat.title}
{stat.loading ? (
) : stat.usd ? ( '$' + stat.value ) : ( stat.value )}
) : parentPlatform !== 'MESSAGE_BUS' ? (
) : null })}
All Time
) }