mango-v4-ui/components/modals/PnlHistoryModal.tsx

143 lines
4.5 KiB
TypeScript
Raw Normal View History

2023-01-15 21:13:34 -08:00
import { ModalProps } from '../../types/modal'
import Modal from '../shared/Modal'
2023-02-23 16:28:49 -08:00
import mangoStore from '@store/mangoStore'
2023-01-16 20:59:51 -08:00
import { useTranslation } from 'next-i18next'
2023-01-15 21:13:34 -08:00
import { useEffect, useMemo } from 'react'
import useMangoAccount from 'hooks/useMangoAccount'
import dayjs from 'dayjs'
import Change from '@components/shared/Change'
2023-01-16 02:07:46 -08:00
import SheenLoader from '@components/shared/SheenLoader'
import { NoSymbolIcon } from '@heroicons/react/20/solid'
2023-02-23 16:28:49 -08:00
import { PerformanceDataItem } from 'types'
2023-01-15 21:13:34 -08:00
2023-01-16 17:26:31 -08:00
interface PnlChange {
time: string
pnlChange: number
}
2023-01-15 21:13:34 -08:00
interface PnlHistoryModalProps {
pnlChangeToday: number
}
type ModalCombinedProps = PnlHistoryModalProps & ModalProps
const PnlHistoryModal = ({
isOpen,
onClose,
pnlChangeToday,
}: ModalCombinedProps) => {
2023-01-16 20:59:51 -08:00
const { t } = useTranslation('account')
2023-01-15 21:13:34 -08:00
const { mangoAccountAddress } = useMangoAccount()
const actions = mangoStore.getState().actions
const loading = mangoStore((s) => s.mangoAccount.performance.loading)
const performanceData = mangoStore((s) => s.mangoAccount.performance.data)
useEffect(() => {
if (mangoAccountAddress) {
2023-02-12 18:03:48 -08:00
actions.fetchAccountPerformance(mangoAccountAddress, 31)
2023-01-15 21:13:34 -08:00
}
}, [actions, mangoAccountAddress])
2023-01-16 17:26:31 -08:00
const dailyValues: PnlChange[] = useMemo(() => {
2023-01-15 21:13:34 -08:00
if (!performanceData.length) return []
const dailyPnl = performanceData.filter((d: PerformanceDataItem) => {
2023-02-12 18:03:48 -08:00
const startTime = new Date().getTime() - 30 * 86400000
const dataDate = new Date(d.time)
const dataTime = dataDate.getTime()
return dataTime >= startTime && dataDate.getHours() === 0
2023-01-15 21:13:34 -08:00
})
return dailyPnl.length
? dailyPnl
.map((d: PerformanceDataItem, index: number) => {
if (index < dailyPnl.length - 1) {
2023-01-18 19:38:06 -08:00
const change = dailyPnl[index + 1].pnl - d.pnl
2023-01-15 21:13:34 -08:00
return {
time: d.time,
2023-01-18 19:38:06 -08:00
pnlChange: change,
2023-01-15 21:13:34 -08:00
}
2023-01-16 17:26:31 -08:00
} else {
return {
time: performanceData[performanceData.length - 1].time,
pnlChange: pnlChangeToday,
}
2023-01-15 21:13:34 -08:00
}
})
.reverse()
: []
}, [performanceData])
2023-01-16 17:26:31 -08:00
const pnlThisWeek = useMemo(() => {
if (dailyValues.length) {
const saturdayIndex = dailyValues.findIndex((d) => {
const day = new Date(d.time).getDay()
return day === 6
})
if (saturdayIndex !== -1) {
return dailyValues
.slice(0, saturdayIndex)
.reduce((a, c) => a + c.pnlChange, 0)
} else {
return dailyValues.reduce((a, c) => a + c.pnlChange, 0)
}
}
return 0
}, [dailyValues])
const getLastSunday = (d: Date) => {
return d.setDate(d.getDate() - d.getDay())
}
2023-01-15 21:13:34 -08:00
return (
<Modal isOpen={isOpen} onClose={onClose}>
<div className="h-96">
2023-01-16 02:07:46 -08:00
<div className="flex h-full flex-col">
2023-03-02 09:02:26 -08:00
<h2 className="mb-4 w-full text-center">{t('pnl-history')}</h2>
2023-01-16 02:07:46 -08:00
{loading ? (
<div className="space-y-1.5">
{[...Array(4)].map((x, i) => (
<SheenLoader className="flex flex-1" key={i}>
<div className="h-12 w-full bg-th-bkg-2" />
</SheenLoader>
))}
</div>
) : dailyValues?.length ? (
2023-01-16 17:26:31 -08:00
<>
<div className="thin-scroll overflow-auto pr-1">
<div className="border-b border-th-bkg-3">
{dailyValues.map((v) => (
2023-01-16 17:26:31 -08:00
<div
className="flex items-center justify-between border-t border-th-bkg-3 p-3"
key={v.time + v.pnlChange}
>
<p>{dayjs(v.time).format('YYYY-MM-DD')}</p>
2023-01-24 17:12:13 -08:00
<Change change={v.pnlChange} prefix="$" />
2023-01-16 17:26:31 -08:00
</div>
))}
</div>
2023-01-15 21:13:34 -08:00
</div>
2023-01-16 17:26:31 -08:00
<div className="mt-4 flex justify-between rounded-md bg-th-bkg-2 p-3">
2023-01-16 20:59:51 -08:00
<p>
{t('week-starting', {
week: dayjs(getLastSunday(new Date())).format('MM-DD'),
})}
</p>
2023-01-16 17:26:31 -08:00
<Change change={pnlThisWeek} prefix="$" />
</div>
</>
2023-01-16 02:07:46 -08:00
) : (
<div className="flex h-full flex-col items-center justify-center pb-12">
<NoSymbolIcon className="mb-2 h-6 w-6 text-th-fgd-3" />
2023-01-16 20:59:51 -08:00
<p>{t('no-pnl-history')}</p>
2023-01-16 02:07:46 -08:00
</div>
)}
</div>
2023-01-15 21:13:34 -08:00
</div>
</Modal>
)
}
export default PnlHistoryModal