Skip to content

Commit 7e92e10

Browse files
committed
refactor(web): abstraction user level coherency, level calculation, pixelart width and heights
1 parent 0d3479f commit 7e92e10

File tree

5 files changed

+69
-28
lines changed

5 files changed

+69
-28
lines changed

web/src/pages/Dashboard/JurorInfo/PixelArt.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,48 @@ import socratesImage from "assets/pngs/dashboard/socrates.png";
77
import platoImage from "assets/pngs/dashboard/plato.png";
88
import aristotelesImage from "assets/pngs/dashboard/aristoteles.png";
99

10-
const StyledImage = styled.img<{ show: boolean }>`
11-
width: 189px;
12-
height: 189px;
10+
interface IStyledImage {
11+
show: boolean;
12+
width: number | string;
13+
height: number | string;
14+
}
15+
16+
const StyledImage = styled.img<IStyledImage>`
17+
width: ${({ width }) => width};
18+
height: ${({ height }) => height};
1319
display: ${({ show }) => (show ? "block" : "none")};
1420
`;
1521

16-
const StyledSkeleton = styled(Skeleton)`
17-
width: 189px;
18-
height: 189px;
22+
interface IStyledSkeleton {
23+
width: number | string;
24+
height: number | string;
25+
}
26+
27+
const StyledSkeleton = styled(Skeleton)<IStyledSkeleton>`
28+
width: ${({ width }) => width};
29+
height: ${({ height }) => height};
1930
`;
2031

2132
const images = [diogenesImage, pythagorasImage, socratesImage, platoImage, aristotelesImage];
2233

2334
interface IPixelArt {
2435
level: number;
36+
width: number | string;
37+
height: number | string;
2538
}
2639

27-
const PixelArt: React.FC<IPixelArt> = ({ level }) => {
40+
const PixelArt: React.FC<IPixelArt> = ({ level, width, height }) => {
2841
const [imageLoaded, setImageLoaded] = useState(false);
2942
return (
3043
<div>
31-
{!imageLoaded && <StyledSkeleton />}
44+
{!imageLoaded && <StyledSkeleton width={width} height={height} />}
3245
<StyledImage
3346
src={images[level]}
3447
alt="Pixel Art per Level"
3548
onLoad={() => setImageLoaded(true)}
3649
show={imageLoaded}
50+
width={width}
51+
height={height}
3752
/>
3853
</div>
3954
);

web/src/pages/Dashboard/JurorInfo/index.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import JurorRewards from "./JurorRewards";
77
import PixelArt from "./PixelArt";
88
import { useAccount } from "wagmi";
99
import { useUserQuery } from "queries/useUser";
10+
import { getCoherencyScore, getUserLevelData } from "utils/userLevelCalculation";
1011
// import StakingRewards from "./StakingRewards";
1112

1213
const Container = styled.div``;
@@ -35,35 +36,20 @@ const Card = styled(_Card)`
3536
)}
3637
`;
3738

38-
const levelTitles = [
39-
{ scoreRange: [0, 20], level: 0, title: "Diogenes" },
40-
{ scoreRange: [20, 40], level: 1, title: "Pythagoras" },
41-
{ scoreRange: [40, 60], level: 2, title: "Socrates" },
42-
{ scoreRange: [60, 80], level: 3, title: "Plato" },
43-
{ scoreRange: [80, 100], level: 4, title: "Aristotle" },
44-
];
45-
46-
const calculateCoherencyScore = (totalCoherent: number, totalDisputes: number): number =>
47-
totalCoherent / (Math.max(totalDisputes, 1) + 10);
48-
4939
const JurorInfo: React.FC = () => {
5040
const { address } = useAccount();
5141
const { data } = useUserQuery(address?.toLowerCase());
5242
const totalCoherent = data?.user ? parseInt(data?.user?.totalCoherent) : 0;
5343
const totalResolvedDisputes = data?.user ? parseInt(data?.user?.totalResolvedDisputes) : 1;
5444

55-
const coherencyScore = calculateCoherencyScore(totalCoherent, totalResolvedDisputes);
56-
const roundedCoherencyScore = Math.round(coherencyScore * 100);
57-
const userLevelData =
58-
levelTitles.find(({ scoreRange }) => {
59-
return roundedCoherencyScore >= scoreRange[0] && roundedCoherencyScore < scoreRange[1];
60-
}) ?? levelTitles[0];
45+
const coherencyScore = getCoherencyScore(totalCoherent, totalResolvedDisputes);
46+
const userLevelData = getUserLevelData(totalCoherent, totalResolvedDisputes);
6147

6248
return (
6349
<Container>
6450
<Header>Juror Dashboard</Header>
6551
<Card>
66-
<PixelArt level={userLevelData.level} />
52+
<PixelArt level={userLevelData.level} width="189" height="189" />
6753
<Coherency
6854
userLevelData={userLevelData}
6955
score={coherencyScore}

web/src/pages/Home/TopJurors/JurorCard.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import styled from "styled-components";
33
import { IdenticonOrAvatar, AddressOrName } from "components/ConnectWallet/AccountDisplay";
44
import EthIcon from "assets/svgs/icons/eth.svg";
55
import PnkIcon from "assets/svgs/icons/kleros.svg";
6+
import PixelArt from "pages/Dashboard/JurorInfo/PixelArt";
7+
import { getUserLevelData } from "utils/userLevelCalculation";
8+
import { useUserQuery } from "hooks/queries/useUser";
69

710
const Container = styled.div`
811
display: flex;
@@ -78,6 +81,12 @@ const StyledIcon = styled.div`
7881
}
7982
`;
8083

84+
const HowItWorks = styled.div`
85+
display: flex;
86+
align-items: center;
87+
gap: 16px;
88+
`;
89+
8190
const StyledIdenticonOrAvatar = styled(IdenticonOrAvatar)``;
8291

8392
interface IJurorCard {
@@ -90,6 +99,14 @@ const JurorCard: React.FC<IJurorCard> = ({ id, address }) => {
9099
const pnkReward = "30K";
91100
const coherentVotes = "10/12";
92101

102+
const { data } = useUserQuery(address?.toLowerCase());
103+
const totalCoherent = data?.user ? parseInt(data?.user?.totalCoherent) : 0;
104+
const totalResolvedDisputes = data?.user ? parseInt(data?.user?.totalResolvedDisputes) : 1;
105+
// const userLevelData = getUserLevelData(totalCoherent, totalResolvedDisputes);
106+
const userLevelData = {
107+
level: 4,
108+
};
109+
93110
return (
94111
<Container>
95112
<JurorTitle>
@@ -112,7 +129,10 @@ const JurorCard: React.FC<IJurorCard> = ({ id, address }) => {
112129
<label>{coherentVotes}</label>
113130
</Coherency>
114131
</RewardsAndCoherency>
115-
{/*"How it works" section here*/}
132+
<HowItWorks>
133+
<label> Level {userLevelData.level}</label>
134+
<PixelArt width="32" height="32" level={userLevelData.level} />
135+
</HowItWorks>
116136
</JurorData>
117137
</Container>
118138
);

web/src/pages/Home/TopJurors/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const ListContainer = styled.div`
1919
justify-content: center;
2020
`;
2121

22-
const TopJurors: React.FC<ITopJurors> = () => {
22+
const TopJurors: React.FC = () => {
2323
const jurors = [
2424
{ id: 1, address: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045" },
2525
{ id: 2, address: "0x74199ddaC9607A3a694011793f674FA1E0d0Fe2D" },
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const levelTitles = [
2+
{ scoreRange: [0, 20], level: 0, title: "Diogenes" },
3+
{ scoreRange: [20, 40], level: 1, title: "Pythagoras" },
4+
{ scoreRange: [40, 60], level: 2, title: "Socrates" },
5+
{ scoreRange: [60, 80], level: 3, title: "Plato" },
6+
{ scoreRange: [80, 100], level: 4, title: "Aristotle" },
7+
];
8+
9+
export const getCoherencyScore = (totalCoherent: number, totalDisputes: number): number =>
10+
totalCoherent / (Math.max(totalDisputes, 1) + 10);
11+
12+
export const getUserLevelData = (totalCoherent: number, totalDisputes: number) => {
13+
const coherencyScore = getCoherencyScore(totalCoherent, totalDisputes);
14+
const roundedCoherencyScore = Math.round(coherencyScore * 100);
15+
return (
16+
levelTitles.find(({ scoreRange }) => {
17+
return roundedCoherencyScore >= scoreRange[0] && roundedCoherencyScore < scoreRange[1];
18+
}) ?? levelTitles[0]
19+
);
20+
};

0 commit comments

Comments
 (0)