1- import React , { useEffect , useState } from "react" ;
1+ import React , { useMemo } from "react" ;
22import styled , { css } from "styled-components" ;
33import { landscapeStyle } from "styles/landscapeStyle" ;
44import { responsiveSize } from "styles/responsiveSize" ;
55
66import { useParams } from "react-router-dom" ;
7+ import Skeleton from "react-loading-skeleton" ;
78
89import { CoinIds } from "consts/coingecko" ;
910
@@ -101,34 +102,26 @@ function beautifyStatNumber(value: number): string {
101102}
102103
103104const calculateJurorOdds = ( stakingAmount : number , totalStake : number ) : string => {
104- const odds = ( stakingAmount * 100 ) / totalStake ;
105+ const odds = totalStake !== 0 ? ( stakingAmount * 100 ) / totalStake : 0 ;
105106 return `${ odds . toFixed ( 2 ) } %` ;
106107} ;
107108
108109const SimulatorPopup : React . FC < { stakingAmount : number } > = ( { stakingAmount } ) => {
109- const [ courtData , setCourtData ] = useState < any > ( null ) ;
110110 const { id } = useParams ( ) ;
111111
112112 const timeframedCourtData = useHomePageExtraStats ( 30 ) ;
113113 const { prices : pricesData } = useCoinPrice ( [ CoinIds . ETH ] ) ;
114114 const ethPriceUSD = pricesData ? pricesData [ CoinIds . ETH ] ?. price : undefined ;
115115
116- useEffect ( ( ) => {
117- if ( timeframedCourtData ?. data ?. courts ) {
118- const foundCourt = timeframedCourtData ?. data ?. courts . find ( ( c ) => c . id === id ) ;
119- if ( foundCourt ) {
120- setCourtData ( foundCourt ) ;
121- }
122- }
116+ const foundCourt = useMemo ( ( ) => {
117+ return timeframedCourtData ?. data ?. courts ?. find ( ( c ) => c . id === id ) ;
123118 } , [ timeframedCourtData , id ] ) ;
124119
125- if ( ! courtData ) return null ;
126-
127- const effectiveStakeAsNumber = Number ( courtData . effectiveStake ) / 1e18 ;
128- const expectedCases = beautifyStatNumber ( stakingAmount * courtData . treeDisputesPerPnk ) ;
129- const expectedVotes = beautifyStatNumber ( stakingAmount * courtData . treeVotesPerPnk ) ;
130- const expectedRewardsUSD = formatUSD ( courtData . treeExpectedRewardPerPnk * stakingAmount * ethPriceUSD ) ;
131- const jurorOdds = calculateJurorOdds ( stakingAmount , effectiveStakeAsNumber + stakingAmount ) ;
120+ const effectiveStakeAsNumber = foundCourt && Number ( foundCourt . effectiveStake ) / 1e18 ;
121+ const expectedCases = foundCourt && beautifyStatNumber ( stakingAmount * foundCourt . treeDisputesPerPnk ) ;
122+ const expectedVotes = foundCourt && beautifyStatNumber ( stakingAmount * foundCourt . treeVotesPerPnk ) ;
123+ const expectedRewardsUSD = foundCourt && formatUSD ( foundCourt . treeExpectedRewardPerPnk * stakingAmount * ethPriceUSD ) ;
124+ const jurorOdds = effectiveStakeAsNumber && calculateJurorOdds ( stakingAmount , effectiveStakeAsNumber + stakingAmount ) ;
132125
133126 const simulatorItems = [
134127 { icon : < LawBalanceIcon /> , description : "You would have been selected in" , value : `${ expectedCases } cases` } ,
@@ -146,7 +139,7 @@ const SimulatorPopup: React.FC<{ stakingAmount: number }> = ({ stakingAmount })
146139 < SimulatorItem key = { index } >
147140 < IconWrapper > { item . icon } </ IconWrapper >
148141 < StyledDescription > { item . description } </ StyledDescription >
149- < StyledValue > { item . value } </ StyledValue >
142+ < StyledValue > { ! item . value . includes ( "undefined" ) ? item . value : < Skeleton width = { 48 } /> } </ StyledValue >
150143 </ SimulatorItem >
151144 ) ) }
152145 </ ItemsContainer >
0 commit comments