2022-06-24 17:14:20 +00:00
|
|
|
import { PortfolioMetrics } from 'common/user'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { last } from 'lodash'
|
2022-08-12 03:44:51 +00:00
|
|
|
import { memo, useEffect, useState } from 'react'
|
|
|
|
import { Period, getPortfolioHistory } from 'web/lib/firebase/users'
|
2022-06-24 17:14:20 +00:00
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { Row } from '../layout/row'
|
|
|
|
import { PortfolioValueGraph } from './portfolio-value-graph'
|
|
|
|
|
|
|
|
export const PortfolioValueSection = memo(
|
|
|
|
function PortfolioValueSection(props: {
|
2022-08-12 03:44:51 +00:00
|
|
|
userId: string
|
2022-08-04 21:27:02 +00:00
|
|
|
disableSelector?: boolean
|
2022-06-24 17:14:20 +00:00
|
|
|
}) {
|
2022-08-12 03:44:51 +00:00
|
|
|
const { disableSelector, userId } = props
|
|
|
|
|
2022-07-03 19:18:12 +00:00
|
|
|
const [portfolioPeriod, setPortfolioPeriod] = useState<Period>('allTime')
|
2022-08-12 03:44:51 +00:00
|
|
|
const [portfolioHistory, setUsersPortfolioHistory] = useState<
|
|
|
|
PortfolioMetrics[]
|
|
|
|
>([])
|
|
|
|
useEffect(() => {
|
|
|
|
getPortfolioHistory(userId).then(setUsersPortfolioHistory)
|
|
|
|
}, [userId])
|
|
|
|
const lastPortfolioMetrics = last(portfolioHistory)
|
2022-06-24 17:14:20 +00:00
|
|
|
|
2022-07-19 09:54:05 +00:00
|
|
|
if (portfolioHistory.length === 0 || !lastPortfolioMetrics) {
|
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
2022-07-18 22:55:17 +00:00
|
|
|
// PATCH: If portfolio history started on June 1st, then we label it as "Since June"
|
|
|
|
// instead of "All time"
|
|
|
|
const allTimeLabel =
|
2022-07-19 09:54:05 +00:00
|
|
|
lastPortfolioMetrics.timestamp < Date.parse('2022-06-20T00:00:00.000Z')
|
2022-07-18 22:55:17 +00:00
|
|
|
? 'Since June'
|
|
|
|
: 'All time'
|
|
|
|
|
2022-06-24 17:14:20 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Row className="gap-8">
|
|
|
|
<div className="mb-4 w-full">
|
2022-08-04 21:27:02 +00:00
|
|
|
<Col
|
|
|
|
className={disableSelector ? 'items-center justify-center' : ''}
|
|
|
|
>
|
2022-06-24 17:14:20 +00:00
|
|
|
<div className="text-sm text-gray-500">Portfolio value</div>
|
|
|
|
<div className="text-lg">
|
|
|
|
{formatMoney(
|
|
|
|
lastPortfolioMetrics.balance +
|
|
|
|
lastPortfolioMetrics.investmentValue
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</div>
|
2022-08-04 21:27:02 +00:00
|
|
|
{!disableSelector && (
|
|
|
|
<select
|
|
|
|
className="select select-bordered self-start"
|
|
|
|
onChange={(e) => {
|
|
|
|
setPortfolioPeriod(e.target.value as Period)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<option value="allTime">{allTimeLabel}</option>
|
|
|
|
<option value="weekly">7 days</option>
|
|
|
|
<option value="daily">24 hours</option>
|
|
|
|
</select>
|
|
|
|
)}
|
2022-06-24 17:14:20 +00:00
|
|
|
</Row>
|
|
|
|
<PortfolioValueGraph
|
|
|
|
portfolioHistory={portfolioHistory}
|
|
|
|
period={portfolioPeriod}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|