import clsx from 'clsx' 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 { 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) const [graphMode, setGraphMode] = useState<'profit' | 'value'>('profit') const [graphDisplayNumber, setGraphDisplayNumber] = useState< number | string | null >(null) const handleGraphDisplayChange = (num: string | number | null) => { setGraphDisplayNumber(num) } // 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 ( <> setGraphMode('profit')} >
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')} >
Portfolio value
{graphMode === 'value' ? graphDisplayNumber ? graphDisplayNumber : formatMoney(totalValue) : formatMoney(totalValue)}
) } ) export function PortfolioPeriodSelection(props: { setPortfolioPeriod: (string: any) => void portfolioPeriod: string className?: string selectClassName?: string }) { const { setPortfolioPeriod, portfolioPeriod, className, selectClassName } = props return ( ) }