2022-06-24 17:14:20 +00:00
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { last } from 'lodash'
|
2022-08-28 23:03:00 +00:00
|
|
|
import { memo, useRef, useState } from 'react'
|
|
|
|
import { usePortfolioHistory } from 'web/hooks/use-portfolio-history'
|
|
|
|
import { Period } from 'web/lib/firebase/users'
|
2022-06-24 17:14:20 +00:00
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { Row } from '../layout/row'
|
2022-09-07 04:24:56 +00:00
|
|
|
import { Spacer } from '../layout/spacer'
|
2022-06-24 17:14:20 +00:00
|
|
|
import { PortfolioValueGraph } from './portfolio-value-graph'
|
|
|
|
|
|
|
|
export const PortfolioValueSection = memo(
|
2022-08-27 08:09:01 +00:00
|
|
|
function PortfolioValueSection(props: { userId: string }) {
|
|
|
|
const { userId } = props
|
2022-08-12 03:44:51 +00:00
|
|
|
|
2022-08-22 04:02:56 +00:00
|
|
|
const [portfolioPeriod, setPortfolioPeriod] = useState<Period>('weekly')
|
2022-08-28 23:03:00 +00:00
|
|
|
const portfolioHistory = usePortfolioHistory(userId, portfolioPeriod)
|
2022-08-27 08:09:01 +00:00
|
|
|
|
2022-08-28 23:03:00 +00:00
|
|
|
// Remember the last defined portfolio history.
|
|
|
|
const portfolioRef = useRef(portfolioHistory)
|
|
|
|
if (portfolioHistory) portfolioRef.current = portfolioHistory
|
|
|
|
const currPortfolioHistory = portfolioRef.current
|
2022-06-24 17:14:20 +00:00
|
|
|
|
2022-08-28 23:03:00 +00:00
|
|
|
const lastPortfolioMetrics = last(currPortfolioHistory)
|
|
|
|
if (!currPortfolioHistory || !lastPortfolioMetrics) {
|
2022-07-19 09:54:05 +00:00
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
2022-09-07 04:24:56 +00:00
|
|
|
const { balance, investmentValue, totalDeposits } = lastPortfolioMetrics
|
2022-08-27 08:09:01 +00:00
|
|
|
const totalValue = balance + investmentValue
|
2022-09-07 04:24:56 +00:00
|
|
|
const totalProfit = totalValue - totalDeposits
|
2022-07-18 22:55:17 +00:00
|
|
|
|
2022-06-24 17:14:20 +00:00
|
|
|
return (
|
2022-08-27 08:09:01 +00:00
|
|
|
<>
|
2022-06-24 17:14:20 +00:00
|
|
|
<Row className="gap-8">
|
2022-08-27 08:09:01 +00:00
|
|
|
<Col className="flex-1 justify-center">
|
2022-09-07 04:24:56 +00:00
|
|
|
<div className="text-sm text-gray-500">Profit</div>
|
|
|
|
<div className="text-lg">{formatMoney(totalProfit)}</div>
|
2022-08-27 08:09:01 +00:00
|
|
|
</Col>
|
|
|
|
<select
|
|
|
|
className="select select-bordered self-start"
|
|
|
|
value={portfolioPeriod}
|
|
|
|
onChange={(e) => {
|
|
|
|
setPortfolioPeriod(e.target.value as Period)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<option value="allTime">All time</option>
|
|
|
|
<option value="weekly">Last 7d</option>
|
|
|
|
<option value="daily">Last 24h</option>
|
|
|
|
</select>
|
2022-06-24 17:14:20 +00:00
|
|
|
</Row>
|
|
|
|
<PortfolioValueGraph
|
2022-08-28 23:03:00 +00:00
|
|
|
portfolioHistory={currPortfolioHistory}
|
2022-08-27 08:09:01 +00:00
|
|
|
includeTime={portfolioPeriod == 'daily'}
|
2022-09-07 04:24:56 +00:00
|
|
|
mode="profit"
|
|
|
|
/>
|
|
|
|
<Spacer h={8} />
|
|
|
|
<Col className="flex-1 justify-center">
|
|
|
|
<div className="text-sm text-gray-500">Portfolio value</div>
|
|
|
|
<div className="text-lg">{formatMoney(totalValue)}</div>
|
|
|
|
</Col>
|
|
|
|
<PortfolioValueGraph
|
|
|
|
portfolioHistory={currPortfolioHistory}
|
|
|
|
includeTime={portfolioPeriod == 'daily'}
|
|
|
|
mode="value"
|
2022-06-24 17:14:20 +00:00
|
|
|
/>
|
2022-08-27 08:09:01 +00:00
|
|
|
</>
|
2022-06-24 17:14:20 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|