Skip to content

Commit

Permalink
refactor VaultManageViewComponent to use HistoricalYieldChart; update…
Browse files Browse the repository at this point in the history
… useAmount hook for better amount display handling
  • Loading branch information
marcinciarka committed Nov 5, 2024
1 parent 46a5242 commit 7148a96
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { capitalize } from 'lodash-es'

import { vaultExposureRawData } from '@/components/layout/VaultOpenView/mocks'
import { TransactionHashPill } from '@/components/molecules/TransactionHashPill/TransactionHashPill'
import { MockedLineChart } from '@/components/organisms/Charts/MockedLineChart'
import { HistoricalYieldChart } from '@/components/organisms/Charts/HistoricalYieldChart'
import { RebalancingActivity } from '@/components/organisms/RebalancingActivity/RebalancingActivity'
import { UserActivity } from '@/components/organisms/UserActivity/UserActivity'
import { VaultExposure } from '@/components/organisms/VaultExposure/VaultExposure'
Expand Down Expand Up @@ -56,15 +56,8 @@ export const VaultManageViewComponent = ({
const { publicClient, transactionClient, tokenBalance, tokenBalanceLoading } = useClient({
vault,
})
const {
amountParsed,
amountDisplay,
amountRaw,
manualSetAmount,
handleAmountChange,
onFocus,
onBlur,
} = useAmount({ vault })
const { amountParsed, amountDisplay, manualSetAmount, handleAmountChange, onFocus, onBlur } =
useAmount({ vault })
const {
sidebar,
txHashes,
Expand Down Expand Up @@ -178,7 +171,7 @@ export const VaultManageViewComponent = ({
}
defaultExpanded
>
<MockedLineChart />
<HistoricalYieldChart aprHourlyList={vault.aprValues} />
</Expander>
<Expander
title={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ export const HistoricalYieldChart = ({
const parsedData = useMemo(() => {
const now = dayjs().startOf('hour')

return [...aprHourlyList].reverse().map((item, itemIndex) => ({
name: now.subtract(itemIndex, 'hour').format('MMM DD, HH:mm'),
'Summer Strategy': new BigNumber(item).toFixed(2),
}))
return [...aprHourlyList]
.reverse()
.map((item, itemIndex) => ({
name: now.subtract(itemIndex, 'hour').format('MMM DD, HH:mm'),
'Summer Strategy': new BigNumber(item).toFixed(2),
}))
.reverse()
}, [aprHourlyList])

return (
Expand Down
22 changes: 16 additions & 6 deletions apps/earn-protocol/hooks/use-amount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ChangeEvent, useMemo, useState } from 'react'
import { type ChangeEvent, useEffect, useMemo, useState } from 'react'
import { type SDKVaultishType } from '@summerfi/app-types'
import BigNumber from 'bignumber.js'

Expand All @@ -13,18 +13,28 @@ export const useAmount = ({ vault }: UseAmountProps) => {
const [amountRaw, setAmountRaw] = useState<string>()

const amountDisplay = useMemo(() => {
if (!amountRaw) {
return ''
if (!amountRaw && amountRaw !== '0') {
return '0.00'
}

if (new BigNumber(amountRaw).isNaN() || editMode) {
return amountRaw
}

return new BigNumber(amountRaw).toFixed(vaultTokenDecimals).replace(/(\.\d*?[1-9])0+$/gu, '$1')
// remove trailing dot
const amountWithNoFollowingZeroes = new BigNumber(
// double parsing removes zeroes at the end
new BigNumber(amountRaw).toFixed(vaultTokenDecimals), // and this gives it a "max" decimals
).toString()

return amountWithNoFollowingZeroes
}, [amountRaw, editMode, vaultTokenDecimals])

useEffect(() => {
if (!editMode) {
setAmountRaw(amountDisplay)
}
}, [editMode, amountDisplay])

const amountParsed = useMemo(() => {
if (!amountRaw || new BigNumber(amountRaw).isNaN()) {
return new BigNumber(0)
Expand Down Expand Up @@ -56,7 +66,7 @@ export const useAmount = ({ vault }: UseAmountProps) => {
// only numbers and dots
/[^0-9.]/gu.test(value) ||
// leading numbers max is vaultTokenDecimals
(decimal || '').length >= vaultTokenDecimals
(decimal || '').length > vaultTokenDecimals
) {
ev.stopPropagation()
ev.preventDefault()
Expand Down

0 comments on commit 7148a96

Please sign in to comment.