import clsx from 'clsx' import { formatMoney } from 'common/util/format' import { last } from 'lodash' import { memo, useState } from 'react' import { usePortfolioHistory } from 'web/hooks/use-portfolio-history' import { Col } from '../layout/col' import { Row } from '../layout/row' import { GraphMode, PortfolioGraph } from './portfolio-value-graph' import { SizedContainer } from 'web/components/sized-container' export const PortfolioValueSection = memo( function PortfolioValueSection(props: { userId: string }) { const { userId } = props const portfolioHistory = usePortfolioHistory(userId, 'allTime') const [graphMode, setGraphMode] = useState('profit') const [graphDisplayNumber, setGraphDisplayNumber] = useState< number | string | null >(null) const handleGraphDisplayChange = (p: { y: number } | undefined) => { setGraphDisplayNumber(p != null ? formatMoney(p.y) : null) } const lastPortfolioMetrics = last(portfolioHistory) if (!portfolioHistory || !lastPortfolioMetrics) { return <> } const { balance, investmentValue, totalDeposits } = lastPortfolioMetrics const totalValue = balance + investmentValue const totalProfit = totalValue - totalDeposits return ( <> { setGraphMode('profit') setGraphDisplayNumber(null) }} >
Profit
0 ? 'text-teal-500' : 'text-red-600' : totalProfit > 0 ? 'text-teal-500' : 'text-red-600', 'text-lg sm:text-xl' )} > {graphMode === 'profit' ? graphDisplayNumber ? graphDisplayNumber : formatMoney(totalProfit) : formatMoney(totalProfit)}
{ setGraphMode('value') setGraphDisplayNumber(null) }} >
Portfolio value
{graphMode === 'value' ? graphDisplayNumber ? graphDisplayNumber : formatMoney(totalValue) : formatMoney(totalValue)}
{(width, height) => ( )} ) } )