-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDamageDistribution.tsx
More file actions
159 lines (146 loc) · 5.53 KB
/
DamageDistribution.tsx
File metadata and controls
159 lines (146 loc) · 5.53 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
import { Attribute as AttributeRaw, Condition as ConditionRaw } from '@discretize/gw2-ui-new';
import { Box, FormControl, Input, InputAdornment, InputLabel, Slider } from '@mui/material';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { makeStyles } from 'tss-react/mui';
import type { DistributionNameUI } from '../../../state/optimizer/types/optimizerSetupTypes';
import {
changeDistributionNew,
changeTextBoxes,
getDistributionNew,
getTextBoxes,
} from '../../../state/slices/distribution';
import { parseDistribution } from '../../../utils/usefulFunctions';
import TemplateInfo from '../../TemplateInfo';
import useAlternativeDamage from '../../baseComponents/useAlternativeDamage';
const Attribute = React.memo(AttributeRaw);
const Condition = React.memo(ConditionRaw);
const useStyles = makeStyles()((theme) => ({
textbox: {
maxWidth: 195,
},
slider: {
margin: theme.spacing(2),
minWidth: 200,
[theme.breakpoints.up('sm')]: {
marginLeft: theme.spacing(5),
},
},
percentSliderRail: {
opacity: 1,
},
}));
const DISTRIBUTION_NAMES = [
{ name: 'Power', min: 0, max: 6000, step: 10, color: '#b1b1b5' },
{ name: 'Power2', min: 0, max: 6000, step: 10, color: '#b1b1b5' },
{ name: 'Burning', min: 0, max: 60, step: 0.1 },
{ name: 'Bleeding', min: 0, max: 60, step: 0.1 },
{ name: 'Poisoned', min: 0, max: 60, step: 0.1 },
{ name: 'Torment', min: 0, max: 60, step: 0.1 },
{ name: 'Confusion', min: 0, max: 60, step: 0.1 },
{ name: 'Reflect', min: 0, max: 6000, step: 10, color: '#b1b1b5' },
] as const;
const DamageDistribution = () => {
const { classes } = useStyles();
const dispatch = useDispatch();
const distributionNew = useSelector(getDistributionNew);
// locally displayed in the text boxes. Text boxes might contain a string that is not a real number (yet), so we need to store those separately
const textBoxes = useSelector(getTextBoxes);
const onUpdate = (key: DistributionNameUI) => (e: unknown, value: number) => {
dispatch(changeTextBoxes({ index: key, value: String(Math.round(value * 100) / 100) }));
dispatch(changeDistributionNew({ index: key, value: Math.round(value * 100) / 100 }));
};
const [alternativeDamageLabel, alternativeDamageEnabled] = useAlternativeDamage();
// shows the slider (faded) if the user switched classes and left a nonzero value
const showAlternativeDamage = alternativeDamageEnabled || distributionNew.Power2;
const controls = DISTRIBUTION_NAMES.map((dist, index) => {
let style;
if (dist.name === 'Power2') {
if (!showAlternativeDamage) return null;
if (!alternativeDamageEnabled) style = { opacity: 0.5 };
}
return (
<Box key={`distriNew_${dist.name}`} style={style} sx={{ display: 'flex', flexWrap: 'wrap' }}>
<Box>
<FormControl className={classes.textbox} variant="standard">
<InputLabel htmlFor={`input-with-icon-adornment-${index}`}>
{
// eslint-disable-next-line no-nested-ternary
dist.name === 'Power' || dist.name === 'Power2' ? (
<Attribute
name="Power"
disableLink
text={dist.name === 'Power2' ? alternativeDamageLabel : undefined}
/>
) : dist.name === 'Reflect' ? (
<Attribute
name="Power"
disableLink
disableTooltip
// todo: translate this
text="Opponent-Power Reflect"
/>
) : (
<Condition name={dist.name} disableLink />
)
}
</InputLabel>
<Input
id={`input-with-icon-adornment-${index}`}
value={textBoxes[dist.name]}
endAdornment={
<InputAdornment position="end">
{dist.name === 'Power' || dist.name === 'Power2' || dist.name === 'Reflect' ? (
<Attribute name="Power" disableLink disableText />
) : (
<Condition name={dist.name} disableLink disableText />
)}
</InputAdornment>
}
error={parseDistribution(textBoxes[dist.name]).error}
onChange={(e) => {
const { value } = e.target;
dispatch(changeTextBoxes({ index: dist.name, value }));
const parsedValue = parseDistribution(value).value;
dispatch(changeDistributionNew({ index: dist.name, value: parsedValue }));
}}
autoComplete="off"
/>
</FormControl>
</Box>
<Box
sx={{
flexGrow: 1,
alignSelf: 'center',
mx: 3,
mb: 4,
minWidth: 200,
md: { marginLeft: 2 },
}}
>
<Slider
value={distributionNew[dist.name]}
step={dist.step}
marks={[...Array(7).keys()]
.map((value) => value * ((dist.max - dist.min) / 6))
.map((value) => ({
value,
label: `${value}`,
}))}
min={dist.min}
max={dist.max}
onChange={onUpdate(dist.name) as React.ComponentProps<typeof Slider>['onChange']}
valueLabelDisplay="auto"
/>
</Box>
</Box>
);
});
return (
<>
{controls}
<TemplateInfo />
</>
);
};
export default DamageDistribution;