-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathMoveMap.tsx
More file actions
389 lines (363 loc) · 11.3 KB
/
MoveMap.tsx
File metadata and controls
389 lines (363 loc) · 11.3 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import {
Cell,
Label,
XAxis,
YAxis,
Scatter,
ScatterChart,
CartesianGrid,
ResponsiveContainer,
ZAxis,
} from 'recharts'
import { useContext, useState, useEffect } from 'react'
import { ColorSanMapping } from 'src/types'
import type { Key } from 'chessground/types'
import type { DrawShape } from 'chessground/draw'
import { WindowSizeContext } from 'src/contexts'
import { MoveTooltip } from './MoveTooltip'
interface MoveMapEntry {
move: string
x: number
y: number
opacity?: number
importance?: number
size?: number
rawCp?: number
winrate?: number
rawMaiaProb?: number
relativeCp?: number
}
interface Props {
moveMap?: MoveMapEntry[]
colorSanMapping: ColorSanMapping
setHoverArrow: React.Dispatch<React.SetStateAction<DrawShape | null>>
makeMove: (move: string) => void
isHomePage?: boolean
}
// Helper function to convert hex color to rgba with alpha
const hexToRgba = (hex: string, alpha: number): string => {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if (!result) return hex
const r = parseInt(result[1], 16)
const g = parseInt(result[2], 16)
const b = parseInt(result[3], 16)
return `rgba(${r}, ${g}, ${b}, ${alpha})`
}
export const MoveMap: React.FC<Props> = ({
moveMap,
colorSanMapping,
setHoverArrow,
makeMove,
isHomePage = false,
}: Props) => {
const { isMobile, width } = useContext(WindowSizeContext)
const [hoveredMove, setHoveredMove] = useState<string | null>(null)
const [hoveredMoveData, setHoveredMoveData] = useState<MoveMapEntry | null>(
null,
)
const [mousePosition, setMousePosition] = useState<{
x: number
y: number
} | null>(null)
const [mobileTooltipMove, setMobileTooltipMove] = useState<string | null>(
null,
)
// Clear tooltip when position changes (indicated by colorSanMapping change)
useEffect(() => {
setHoveredMove(null)
setHoveredMoveData(null)
setMousePosition(null)
setMobileTooltipMove(null)
}, [colorSanMapping])
// Responsive font sizing based on screen width
const getAxisLabelFontSize = () => {
if (width < 640) return 10 // Very small screens
if (width < 768) return 11 // Small screens
if (width < 1024) return 12 // Medium screens
if (width < 1280) return 13 // Large screens
return 14 // Extra large screens
}
const getTickFontSize = () => {
if (width < 640) return 8 // Very small screens
if (width < 768) return 9 // Small screens
if (width < 1024) return 9 // Medium screens
if (width < 1280) return 10 // Large screens
return 11 // Extra large screens
}
const getDirectionalLabelFontSize = () => {
if (width < 640) return 9 // Very small screens
if (width < 768) return 10 // Small screens
if (width < 1024) return 10 // Medium screens
if (width < 1280) return 11 // Large screens
return 12 // Extra large screens
}
const getYAxisLabelFontSize = () => {
if (width < 640) return 10 // Very small screens
if (width < 768) return 11 // Small screens
if (width < 1024) return 12 // Medium screens
if (width < 1280) return 13 // Large screens
return 14 // Extra large screens
}
// Responsive dy values for Y-axis labels
const getMainLabelDy = () => {
if (width < 640) return 20 // Mobile - keep original value
if (width < 1280) return 20 // Small desktop screens - reduce offset
return 32 // Large screens - original value
}
const getUnlikelyLabelDy = () => {
if (isHomePage) {
// Reduced offset for home page
if (width < 640) return 80 // Mobile
if (width < 1280) return 90 // Small desktop
return 100 // Large screens
}
// Original offsets for analysis page
if (width < 640) return 100 // Mobile - keep original value
if (width < 1280) return 110 // Small desktop screens - reduce offset
return 130 // Large screens - original value
}
const getLikelyLabelDy = () => {
if (isHomePage) {
// Reduced offset for home page
if (width < 640) return -20 // Mobile
if (width < 1280) return -20 // Small desktop
return -30 // Large screens
}
// Original offsets for analysis page
if (width < 640) return -30 // Mobile - keep original value
if (width < 1280) return -30 // Small desktop screens - reduce offset
return -48 // Large screens - original value
}
const onMouseEnter = (
move: string,
moveData: MoveMapEntry,
event?: React.MouseEvent,
) => {
if (!isMobile) {
setHoverArrow({
orig: move.slice(0, 2) as Key,
dest: move.slice(2, 4) as Key,
brush: 'green',
modifiers: {
lineWidth: 10,
},
})
setHoveredMove(move)
setHoveredMoveData(moveData)
// Capture mouse position for tooltip positioning
if (event) {
// Get position relative to the viewport for better positioning
setMousePosition({
x: event.clientX,
y: event.clientY,
})
}
}
}
const onMouseLeave = () => {
if (!isMobile) {
setHoverArrow(null)
setHoveredMove(null)
setHoveredMoveData(null)
setMousePosition(null)
}
}
const onCellClick = (
move: string,
moveData: MoveMapEntry,
event?: React.MouseEvent,
) => {
if (isMobile) {
if (mobileTooltipMove === move) {
// Second click on same move - make the move
makeMove(move)
setMobileTooltipMove(null)
setHoveredMove(null)
setHoveredMoveData(null)
setMousePosition(null)
setHoverArrow(null)
} else {
// First click - show tooltip
setMobileTooltipMove(move)
setHoverArrow({
orig: move.slice(0, 2) as Key,
dest: move.slice(2, 4) as Key,
brush: 'green',
modifiers: {
lineWidth: 10,
},
})
setHoveredMove(move)
setHoveredMoveData(moveData)
// Capture mouse position for tooltip positioning
if (event) {
setMousePosition({
x: event.clientX,
y: event.clientY,
})
}
}
} else {
// Desktop - make move immediately
makeMove(move)
}
}
const onTooltipClick = (move: string) => {
if (isMobile) {
makeMove(move)
setMobileTooltipMove(null)
setHoveredMove(null)
setHoveredMoveData(null)
setMousePosition(null)
setHoverArrow(null)
}
}
const onContainerMouseLeave = () => {
if (!isMobile) {
onMouseLeave()
}
}
const renderTooltip = () => {
if (!hoveredMove || !hoveredMoveData || !mousePosition) return null
return (
<MoveTooltip
move={hoveredMove}
colorSanMapping={colorSanMapping}
maiaProb={hoveredMoveData.rawMaiaProb}
stockfishCp={hoveredMoveData.rawCp}
stockfishWinrate={hoveredMoveData.winrate}
stockfishCpRelative={hoveredMoveData.relativeCp}
position={mousePosition}
onClickMove={isMobile ? onTooltipClick : undefined}
/>
)
}
return (
<div
id="analysis-move-map"
className="flex h-64 max-h-full w-full flex-col overflow-hidden bg-background-1/60 md:h-full md:rounded"
onMouseLeave={onContainerMouseLeave}
>
<h2 className="p-3 text-base text-primary md:text-sm xl:text-base">
Move Map
</h2>
<div className="relative flex h-full w-full flex-col">
<ResponsiveContainer width="100%" height="100%">
<ScatterChart margin={{ left: 0, top: 5, right: 30, bottom: 20 }}>
<CartesianGrid strokeDasharray="3 3" stroke="#3C3C3C" />
<XAxis
dataKey="x"
type="number"
axisLine={false}
label={{
value: 'SF Loss',
fill: '#76ADDD',
position: 'insideBottom',
fontSize: getAxisLabelFontSize(),
fontWeight: 600,
offset: -8,
dx: isMobile ? -6 : -12,
}}
tickCount={5}
tickLine={false}
tickMargin={0}
tick={{ fill: 'white', fontSize: getTickFontSize() }}
domain={[-3, 0]}
ticks={[-3, -2, -1, 0]}
>
<Label
value="← Blunders"
position="insideBottomLeft"
fill="#5A9DD7"
fontSize={getDirectionalLabelFontSize()}
fontWeight={500}
dy={10}
dx={isMobile ? -40 : -18}
/>
<Label
value="Best Moves →"
position="insideBottomRight"
fontSize={getDirectionalLabelFontSize()}
fill="#5A9DD7"
fontWeight={500}
dy={10}
dx={18}
/>
</XAxis>
<YAxis
dataKey="y"
type="number"
axisLine={false}
label={{
value: 'Maia',
fill: '#FE7F6D',
position: 'insideLeft',
angle: -90,
fontSize: getYAxisLabelFontSize(),
fontWeight: 600,
dx: isMobile ? 5 : 10,
dy: getMainLabelDy(),
}}
tickCount={4}
tick={{
fill: 'white',
fontSize: getTickFontSize(),
}}
tickMargin={0}
tickLine={false}
tickFormatter={(value) => `${value}%`}
domain={([dataMin, dataMax]) => [0, dataMax > 40 ? 100 : 40]}
>
<Label
value="← Unlikely"
dx={10}
angle={-90}
fontSize={getDirectionalLabelFontSize()}
fontWeight={500}
dy={getUnlikelyLabelDy()}
position="insideLeft"
fill="#BF5F52"
/>
<Label
value="Likely →"
fill="#BF5F52"
position="insideLeft"
fontSize={getDirectionalLabelFontSize()}
dx={10}
angle={-90}
fontWeight={500}
dy={getLikelyLabelDy()}
/>
</YAxis>
<ZAxis dataKey="z" type="number" range={[100, 101]} />
<Scatter data={moveMap || []} fill="#fff">
{moveMap?.map((entry, index) => {
const opacity = Math.max(entry.opacity ?? 1, 0.5)
const baseColor = colorSanMapping[entry.move]?.color ?? '#fff'
const fillColor = baseColor.startsWith('#')
? hexToRgba(baseColor, opacity)
: baseColor
return (
<Cell
key={`cell-${entry.move}${index}`}
fill={fillColor}
onMouseEnter={(event) =>
onMouseEnter(entry.move, entry, event)
}
onMouseLeave={onMouseLeave}
onMouseDown={(event) =>
onCellClick(entry.move, entry, event)
}
style={{ cursor: 'pointer' }}
/>
)
})}
</Scatter>
</ScatterChart>
</ResponsiveContainer>
{/* Comprehensive tooltip */}
{renderTooltip()}
</div>
</div>
)
}