import { useEffect, useState } from 'react'
import styled from '@emotion/styled'
import dayjs from 'dayjs'
import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table'
import { AreaChart, Area, ReferenceLine, XAxis, YAxis, Tooltip } from 'recharts'
import { ExternalLinkIcon } from '@heroicons/react/outline'
import { usdFormatter } from '../utils'
import { AwardIcon, TrophyIcon } from './icons'
import useMangoStore from '../stores/useMangoStore'
const utc = require('dayjs/plugin/utc')
dayjs.extend(utc)
const StyledTooltipWrapper = styled.div`
min-width: 180px;
`
const LeaderboardTable = () => {
const [pnlHistory, setPnlHistory] = useState([])
const [loading, setLoading] = useState(false)
const pnlLeaderboard = useMangoStore((s) => s.pnlLeaderboard)
/* API Returns:
* [ { cumulative_pnl: -3.687498
date: "2021-06-10"
margin_account: "J8XtwLVyZjeH1PG1Nnk9cWbLn3zEemS1rCbn4x6AjtXM"
name: ""
owner: "APLKzSqJQw79q4U4ipBWnLdqkVzijSPNpDCNKwL8mW3B"
}, ... ]
*/
useEffect(() => {
const getPnlHistory = async () => {
setLoading(true)
const start = dayjs().utc().subtract(31, 'day').format('YYYY-MM-DD')
console.log(start)
const results = await Promise.all(
pnlLeaderboard.slice(pnlHistory.length).map(async (acc) => {
const response = await fetch(
`https://mango-transaction-log.herokuapp.com/stats/pnl_history/${acc.margin_account}?start_date=${start}`
)
const parsedResponse = await response.json()
return parsedResponse ? parsedResponse.reverse() : []
})
)
setPnlHistory(pnlHistory.concat(results))
setLoading(false)
}
getPnlHistory()
}, [pnlLeaderboard])
const formatPnlHistoryData = (data) => {
const start = new Date(
dayjs().utc().hour(0).minute(0).subtract(31, 'day')
).getTime()
return data.filter((d) => new Date(d.date).getTime() > start)
}
const tooltipContent = (tooltipProps) => {
if (tooltipProps.payload.length > 0) {
return (
Rank | Account | PNL | PNL / Time |
View on Step
![]() |
---|---|---|---|---|
{acc.rank}
{acc.rank === 1 ? (
|
{acc.name ? acc.name : `${acc.margin_account.slice( 0, 5 )}...${acc.margin_account.slice(-5)}`} |
{usdFormatter.format(acc.pnl)}
|
{loading && !pnlHistory[index] ? (
) : (
|
View
|