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 { PillButton } from '../buttons/pill-button' 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'>('value') 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('value')} >
Portfolio value
{graphMode === 'value' ? graphDisplayNumber ? graphDisplayNumber : formatMoney(totalValue) : formatMoney(totalValue)}
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)}
) } ) export function PortfolioPeriodSelection(props: { setPortfolioPeriod: (string: any) => void portfolioPeriod: string className?: string selectClassName?: string }) { const { setPortfolioPeriod, portfolioPeriod, className, selectClassName } = props return ( ) } export function GraphToggle(props: { setGraphMode: (mode: 'profit' | 'value') => void graphMode: string }) { const { setGraphMode, graphMode } = props return ( { setGraphMode('value') }} xs={true} className="z-50" > Value { setGraphMode('profit') }} xs={true} className="z-50" > Profit ) }