Skip to content

Commit 5245f2d

Browse files
committed
fix: few comments by coderabbitai
1 parent 638c5ba commit 5245f2d

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useEffect, useState } from "react";
1+
import React, { useMemo } from "react";
22
import styled, { css } from "styled-components";
33
import { landscapeStyle } from "styles/landscapeStyle";
44
import { responsiveSize } from "styles/responsiveSize";
55

66
import { useParams } from "react-router-dom";
7+
import Skeleton from "react-loading-skeleton";
78

89
import { CoinIds } from "consts/coingecko";
910

@@ -101,34 +102,26 @@ function beautifyStatNumber(value: number): string {
101102
}
102103

103104
const 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

108109
const 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>

web/src/pages/Courts/CourtDetails/StakePanel/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const StakePanel: React.FC<{ courtName: string; id: string }> = ({ courtName = "
122122
{isStaking && Number(uncommify(amount)) > 0 ? (
123123
<SimulatorPopup
124124
stakingAmount={
125-
!isUndefined(jurorBalance)
125+
!isUndefined(jurorBalance?.[2])
126126
? Number(formatEther(jurorBalance?.[2])) + Number(uncommify(amount))
127127
: Number(uncommify(amount))
128128
}

web/src/pages/Courts/CourtDetails/Stats.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,16 @@ const stats: IStat[] = [
193193
},
194194
];
195195

196+
interface ITimeframedStatData {
197+
treeExpectedRewardPerPnk: number;
198+
treeVotesPerPnk: number;
199+
treeDisputesPerPnk: number;
200+
}
201+
196202
interface ITimeframedStat {
197203
title: string | React.ReactNode;
198204
coinId?: number;
199-
getText: (data: { treeExpectedRewardPerPnk: number; treeVotesPerPnk: number; treeDisputesPerPnk: number }) => string;
205+
getText: (data: ITimeframedStatData) => string;
200206
color: IStatDisplay["color"];
201207
icon: React.FC<React.SVGAttributes<SVGElement>>;
202208
}
@@ -221,14 +227,7 @@ const Stats = () => {
221227
const { prices: pricesData } = useCoinPrice(coinIds);
222228

223229
const foundCourt = useMemo(() => {
224-
if (timeframedCourtData?.data?.courts) {
225-
const foundCourt = timeframedCourtData?.data?.courts.find((c) => c.id === id);
226-
console.log({ foundCourt });
227-
return foundCourt;
228-
} else {
229-
console.log("Court not found or diffCourts not available");
230-
return undefined;
231-
}
230+
return timeframedCourtData?.data?.courts?.find((c) => c.id === id);
232231
}, [timeframedCourtData, id]);
233232

234233
const handleTimeRangeChange = (value: string | number) => {
@@ -311,7 +310,7 @@ const Stats = () => {
311310
<StyledAllTimeText>All time</StyledAllTimeText>
312311
</AllTimeContainer>
313312
<StyledCard>
314-
{stats.map(({ title, coinId, getText, getSubtext, color, icon }, i) => {
313+
{stats.map(({ title, coinId, getText, getSubtext, color, icon }) => {
315314
const coinPrice = !isUndefined(pricesData) ? pricesData[coinIds[coinId!]]?.price : undefined;
316315
return (
317316
<StatDisplay
@@ -338,7 +337,7 @@ const Stats = () => {
338337
<Info />
339338
</TimeSelectorContainer>
340339
<StyledCard>
341-
{timeframedStats.map(({ title, getText, color, icon }, i) => {
340+
{timeframedStats.map(({ title, getText, color, icon }) => {
342341
return (
343342
<StatDisplay
344343
key={title}

0 commit comments

Comments
 (0)