import { formatMoney } from 'common/util/format' import { last } from 'lodash' import { memo, useRef, useState } from 'react' import { usePortfolioHistory } from 'web/hooks/use-portfolio-history' import { Period } from 'web/lib/firebase/users' import { Col } from '../layout/col' import { Row } from '../layout/row' import { Spacer } from '../layout/spacer' import { PortfolioValueGraph } from './portfolio-value-graph' export const PortfolioValueSection = memo( function PortfolioValueSection(props: { userId: string }) { const { userId } = props const [portfolioPeriod, setPortfolioPeriod] = useState('weekly') const portfolioHistory = usePortfolioHistory(userId, portfolioPeriod) // Remember the last defined portfolio history. const portfolioRef = useRef(portfolioHistory) if (portfolioHistory) portfolioRef.current = portfolioHistory const currPortfolioHistory = portfolioRef.current const lastPortfolioMetrics = last(currPortfolioHistory) if (!currPortfolioHistory || !lastPortfolioMetrics) { return <> } const { balance, investmentValue, totalDeposits } = lastPortfolioMetrics const totalValue = balance + investmentValue const totalProfit = totalValue - totalDeposits return ( <>
Profit
{formatMoney(totalProfit)}
Portfolio value
{formatMoney(totalValue)}
) } )