11import type { YieldBalanceDto , YieldDto } from "@stakekit/api-hooks" ;
22import BigNumber from "bignumber.js" ;
3+ import { isPast } from "date-fns" ;
34import { useMemo } from "react" ;
45import { useTranslation } from "react-i18next" ;
56import { Box } from "../../../components/atoms/box" ;
67import { TokenIcon } from "../../../components/atoms/token-icon" ;
78import { Text } from "../../../components/atoms/typography/text" ;
89import { defaultFormattedNumber } from "../../../utils" ;
9- import { daysUntilDate } from "../../../utils/date" ;
10+ import { formatDurationUntilDate } from "../../../utils/date" ;
1011
1112export const PositionBalances = ( {
1213 yieldBalance,
@@ -17,14 +18,30 @@ export const PositionBalances = ({
1718} ) => {
1819 const { t } = useTranslation ( ) ;
1920
20- const daysRemaining = useMemo ( ( ) => {
21- return ( yieldBalance . type === "unstaking" ||
22- yieldBalance . type === "unlocking" ||
23- yieldBalance . type === "preparing" ) &&
24- yieldBalance . date
25- ? daysUntilDate ( new Date ( yieldBalance . date ) )
26- : null ;
27- } , [ yieldBalance . date , yieldBalance . type ] ) ;
21+ const durationUntilDate = useMemo ( ( ) => {
22+ if (
23+ ! yieldBalance . date ||
24+ ( yieldBalance . type !== "unstaking" &&
25+ yieldBalance . type !== "unlocking" &&
26+ yieldBalance . type !== "preparing" )
27+ ) {
28+ return null ;
29+ }
30+
31+ const date = new Date ( yieldBalance . date ) ;
32+
33+ if ( isPast ( date ) ) {
34+ return t ( "position_details.unstaking_imminent" ) ;
35+ }
36+
37+ const duration = formatDurationUntilDate ( date ) ;
38+
39+ if ( ! duration ) {
40+ return null ;
41+ }
42+
43+ return t ( "position_details.unstaking_duration" , { duration } ) ;
44+ } , [ yieldBalance . date , yieldBalance . type , t ] ) ;
2845
2946 const yieldType = integrationData . metadata . type ;
3047
@@ -68,11 +85,9 @@ export const PositionBalances = ({
6885 </ Text >
6986 </ Box >
7087
71- { typeof daysRemaining === "number" && (
88+ { ! ! durationUntilDate && (
7289 < Text variant = { { type : "muted" , weight : "normal" } } >
73- { t ( "position_details.unstaking_days" , {
74- count : daysRemaining ,
75- } ) }
90+ { durationUntilDate }
7691 </ Text >
7792 ) }
7893 </ Box >
0 commit comments