-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHeroDevelopmentPage.tsx
More file actions
208 lines (195 loc) · 8.08 KB
/
HeroDevelopmentPage.tsx
File metadata and controls
208 lines (195 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use client';
import React, { useMemo, useState, useRef, useEffect } from 'react';
import Image from 'next/image';
import cls from './HeroDevelopmentPage.module.scss';
import {
AttributesPie,
AttributesPricing3,
BarChart,
Hero,
Stat,
rarityClassNames,
color,
statsPricingData,
} from '@/entities/Hero';
import { useClientTranslation } from '@/shared/i18n';
import useSizes from '@/shared/lib/hooks/useSizes';
import { classNames, Mods } from '@/shared/lib/classNames/classNames';
export interface HeroDevelopmentPageProps {
hero: Hero | undefined;
}
const HeroDevelopmentPage: React.FC<HeroDevelopmentPageProps> = ({ hero }) => {
const [stat, setStat] = useState<Stat>({ name: 'resistance', defaultLevel: 1, rarityClass: 1 });
const [toLevel, setToLevel] = useState<number>(0);
const [fromLevel, setFromLevel] = useState<number>(0);
const [upgradePotential, setUpgradePotential] = useState<number>(10);
const { t } = useClientTranslation('hero-development');
const { isMobileSize, isTabletSize, isDesktopSize, isWidescreenSize } = useSizes();
const combinedModCss: Mods = {
[cls.isMobile]: isMobileSize,
[cls.isTablet]: isTabletSize,
[cls.isDesktop]: isDesktopSize,
[cls.isWidescreen]: isWidescreenSize,
};
const defaultSlice = useMemo(
() => ({
max: hero ? hero.stats.reduce((acc: number, stat) => stat.defaultLevel + acc, 0) : 0,
sections: hero
? hero.stats.map((stat) => ({
value: stat.defaultLevel,
color: color[stat.name],
}))
: [{ value: 0, color: 'black' }],
}),
[hero, hero?.stats],
);
const upgradeSlice = useMemo(
() => ({
max: 10,
sections: [
{
value: toLevel - stat.defaultLevel,
color: color[stat.name],
},
{ value: upgradePotential, color: 'transparent' },
],
}),
[toLevel, stat],
);
// Dynamically calculate the radius based on the container size
const pieContainerRef = useRef<HTMLDivElement>(null);
const [radius, setRadius] = useState<number>(70); // Initial value
useEffect(() => {
const observer = new ResizeObserver((entries) => {
for (const entry of entries) {
if (entry.contentRect) {
setRadius(Math.min(entry.contentRect.width, entry.contentRect.height) / 2); // Dynamically calculate radius
}
}
});
if (pieContainerRef.current) {
observer.observe(pieContainerRef.current);
}
return () => {
if (pieContainerRef.current) {
observer.unobserve(pieContainerRef.current);
}
};
}, []);
const getDevelopment = () => {
// fate-priest cannot be developed
if (hero && hero.slug !== 'believer')
return (
<>
<div className={classNames(cls.AttributesPricingBlock, combinedModCss)}>
<AttributesPricing3
selectedStat={stat}
setSelectedStat={setStat}
fromLevel={fromLevel}
setFromLevel={setFromLevel}
toLevel={toLevel}
setToLevel={setToLevel}
setUpgradePotential={setUpgradePotential}
stats={hero.stats}
/>
</div>
<div className={classNames(cls.InfoSection, combinedModCss)}>
<table className={cls.InfoTable}>
<tbody>
<tr>
<td>{t('upgradePotentialAfterUpdate')}</td>
<td className={`${cls.FatGreen} ${cls.InfoTableCell}`}>
{upgradePotential}
</td>
</tr>
<tr>
<td>{t('rarityClass')}</td>
<td className={cls.InfoTableCell}>
<span className={cls.FatGreen}>
{t(rarityClassNames[stat.rarityClass])}
</span>
</td>
</tr>
</tbody>
</table>
<div
ref={pieContainerRef}
className={cls.Pie}
>
<AttributesPie
characterDefault={defaultSlice}
characterUpgrade={upgradeSlice}
borderwidth={2}
bordercolor="#000000"
radius={radius} // Dynamically calculated radius
/>
</div>
</div>
<table className={cls.PriceTable}>
<tbody>
<tr>
<th>{t('level')}</th>
<th>{t('stepsPerLevel')}</th>
<th>{t('stepsPrice')}</th>
<th>{t('price')}</th>
</tr>
{statsPricingData.stepsPerLevel.map((value, index) => (
<tr key={index}>
<td>{index + 1}</td>
<td>{value}</td>
<td>
{statsPricingData[stat.rarityClass]
? statsPricingData[stat.rarityClass][index]
: ''}
</td>
<td>
{statsPricingData[stat.rarityClass]
? value * statsPricingData[stat.rarityClass][index]
: ''}
</td>
</tr>
))}
</tbody>
</table>
</>
);
else
return hero ? (
<div className={cls.FatePriest}>{t('fatePriestCannotBeDeveloped')}</div>
) : (
<div />
);
};
if (!statsPricingData) {
return <h2>{t('Stat pricing data is unavailable')}</h2>;
}
return (
<div className={cls.Root}>
<div className={classNames(cls.Header, combinedModCss)}>{t('title')}</div>
{hero === undefined ? (
<div className={cls.Purpose}>{t('text')}</div>
) : (
<div className={classNames(cls.HeroAndChart, combinedModCss)}>
<div className={classNames(cls.Hero, combinedModCss)}>
{hero && (
<Image
className={classNames(cls.Image, combinedModCss)}
src={hero?.srcImg}
priority={true}
alt="image"
/>
)}
<div>{hero && <h1 className={cls.Title}>{hero.title}</h1>}</div>
</div>
{hero && (
<div className={classNames(cls.BarChartBlock, combinedModCss)}>
<BarChart stats={hero?.stats} />
</div>
)}
</div>
)}
{getDevelopment()}
</div>
);
};
export default HeroDevelopmentPage;