diff --git a/components/shared/DetailedAreaChart.tsx b/components/shared/DetailedAreaChart.tsx new file mode 100644 index 00000000..7536ae45 --- /dev/null +++ b/components/shared/DetailedAreaChart.tsx @@ -0,0 +1,281 @@ +import { FunctionComponent, useState } from 'react' +import dayjs from 'dayjs' +import relativeTime from 'dayjs/plugin/relativeTime' +import { + AreaChart, + Area, + XAxis, + YAxis, + Tooltip, + ResponsiveContainer, +} from 'recharts' +import FlipNumbers from 'react-flip-numbers' + +import LineChartIcon from '../icons/LineChartIcon' +import ContentBox from '../shared/ContentBox' +import { DownTriangle, UpTriangle } from '../shared/DirectionTriangles' +import { formatFixedDecimals } from '../../utils/numbers' +import SheenLoader from '../shared/SheenLoader' +import { COLORS } from '../../styles/colors' +import { useTheme } from 'next-themes' +import { IconButton } from './Button' +import { ArrowLeftIcon } from '@heroicons/react/solid' +import { FadeInFadeOut } from './Transitions' + +dayjs.extend(relativeTime) + +interface DetailedAreaChartProps { + data: any[] + hideChart?: () => void + loading?: boolean + title?: string + xKey: string + yKey: string +} + +const DetailedAreaChart: FunctionComponent = ({ + data, + hideChart, + loading, + title, + xKey, + yKey, +}) => { + const [mouseData, setMouseData] = useState(null) + const [daysToShow, setDaysToShow] = useState(1) + const { theme } = useTheme() + + const handleMouseMove = (coords: any) => { + if (coords.activePayload) { + setMouseData(coords.activePayload[0].payload) + } + } + + const handleMouseLeave = () => { + setMouseData(null) + } + + const calculateChartChange = () => { + if (data.length) { + if (mouseData) { + const index = data.findIndex((d: any) => d[xKey] === mouseData[xKey]) + return ((data[index][yKey] - data[0][yKey]) / data[0][yKey]) * 100 + } else + return ( + ((data[data.length - 1][yKey] - data[0][yKey]) / data[0][yKey]) * 100 + ) + } + return 0 + } + + return ( + + + {loading ? ( + +
+ + ) : data.length ? ( +
+
+
+ + + +
+

{title}

+ {mouseData ? ( +
+
+ + = 0 + ? 'text-th-green' + : 'text-th-red' + }`} + > + {calculateChartChange() >= 0 ? ( + + ) : ( + + )} + + {calculateChartChange().toFixed(2)}% + + +
+

+ {dayjs(mouseData[xKey]).format('DD MMM YY, h:mma')} +

+
+ ) : ( +
+
+ + = 0 + ? 'text-th-green' + : 'text-th-red' + }`} + > + {calculateChartChange() >= 0 ? ( + + ) : ( + + )} + + {calculateChartChange().toFixed(2)}% + + +
+

+ {dayjs(data[data.length - 1][xKey]).format( + 'DD MMM YY, h:mma' + )} +

+
+ )} +
+
+
+
+
+ + + +
+
+ + + } + /> + + + = 0 + ? COLORS.GREEN[theme] + : COLORS.RED[theme] + } + stopOpacity={0.15} + /> + = 0 + ? COLORS.GREEN[theme] + : COLORS.RED[theme] + } + stopOpacity={0} + /> + + + = 0 + ? COLORS.GREEN[theme] + : COLORS.RED[theme] + } + strokeWidth={1.5} + fill="url(#gradientArea)" + /> + + + + +
+
+
+ ) : ( +
+
+ +

Chart not available

+
+
+ )} + + + ) +} + +export default DetailedAreaChart diff --git a/pages/index.tsx b/pages/index.tsx index 6197e444..353ad31c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -10,6 +10,14 @@ import TokenList from '../components/TokenList' import mangoStore from '../store/state' import { formatDecimal } from '../utils/numbers' import FlipNumbers from 'react-flip-numbers' +import { UpTriangle } from '../components/shared/DirectionTriangles' +import SimpleAreaChart from '../components/shared/SimpleAreaChart' +import { COLORS } from '../styles/colors' +import { useTheme } from 'next-themes' +import { IconButton } from '../components/shared/Button' +import { ArrowsExpandIcon } from '@heroicons/react/solid' +import DetailedAreaChart from '../components/shared/DetailedAreaChart' +import { Transition } from '@headlessui/react' export async function getStaticProps({ locale }: { locale: string }) { return { @@ -19,44 +27,103 @@ export async function getStaticProps({ locale }: { locale: string }) { } } +const chartData = [ + [1, 300], + [2, 310], + [3, 320], + [4, 330], + [5, 340], + [6, 350], + [7, 360], + [8, 370], + [9, 380], + [10, 390], +] + const Index: NextPage = () => { const { t } = useTranslation('common') const mangoAccount = mangoStore((s) => s.mangoAccount.current) const [showDepositModal, setShowDepositModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) + const [showDetailedValueChart, setShowDetailedValueChart] = useState(false) + const [showExpandChart, setShowExpandChart] = useState(false) + const { theme } = useTheme() - return ( + const onHoverMenu = (open: boolean, action: string) => { + if ( + (!open && action === 'onMouseEnter') || + (open && action === 'onMouseLeave') + ) { + setShowExpandChart(!open) + } + } + + const handleShowDetailedValueChart = () => { + setShowDetailedValueChart(true) + setShowExpandChart(false) + } + + return !showDetailedValueChart ? ( <> -
-
-

{t('account-value')}

-
- $ - {mangoAccount ? ( - - ) : ( - (0).toFixed(2) - )} +
+
+
+

{t('account-value')}

+
+ $ + {mangoAccount ? ( + + ) : ( + (0).toFixed(2) + )} +
+
+ +

2.13%

+
+
+
onHoverMenu(showExpandChart, 'onMouseEnter')} + onMouseLeave={() => onHoverMenu(showExpandChart, 'onMouseLeave')} + > + + + handleShowDetailedValueChart()} + > + + +
- {/*
- $ - {mangoAccount - ? formatDecimal( - toUiDecimals(mangoAccount.getEquity().toNumber()), - 2 - ) - : (0).toFixed(2)} -
*/}
@@ -74,6 +141,14 @@ const Index: NextPage = () => { /> ) : null} + ) : ( + setShowDetailedValueChart(false)} + title={t('account-value')} + xKey="0" + yKey="1" + /> ) }