-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAnalysisSidebar.tsx
More file actions
305 lines (284 loc) · 10.2 KB
/
AnalysisSidebar.tsx
File metadata and controls
305 lines (284 loc) · 10.2 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
import {
Highlight,
BlunderMeter,
MovesByRating,
SimplifiedAnalysisOverview,
SimplifiedBlunderMeter,
} from 'src/components/Analysis'
import { motion } from 'framer-motion'
import type { DrawShape } from 'chessground/draw'
import { Dispatch, SetStateAction, useCallback, useMemo } from 'react'
import type { ComponentProps, CSSProperties, ReactNode } from 'react'
import { useAnalysisController } from 'src/hooks/useAnalysisController'
import type { MaiaEvaluation, StockfishEvaluation } from 'src/types'
type HighlightBoardDescription = ComponentProps<
typeof Highlight
>['boardDescription']
interface Props {
hover: (move?: string) => void
makeMove: (move: string) => void
setHoverArrow: Dispatch<SetStateAction<DrawShape | null>>
analysisEnabled: boolean
controller: ReturnType<typeof useAnalysisController>
handleToggleAnalysis: () => void
hideDetailedBlunderMeter?: boolean
containerStyle?: CSSProperties
footerContent?: ReactNode
desktopContentHeightCss?: string
itemVariants?: {
hidden: {
opacity: number
y: number
}
visible: {
opacity: number
y: number
transition: {
duration: number
ease: number[]
type: string
}
}
exit: {
opacity: number
y: number
transition: {
duration: number
ease: number[]
type: string
}
}
}
}
export const AnalysisSidebar: React.FC<Props> = ({
hover,
makeMove,
controller,
analysisEnabled,
handleToggleAnalysis,
hideDetailedBlunderMeter = false,
containerStyle,
footerContent,
desktopContentHeightCss = '85vh',
itemVariants,
}) => {
const emptyBlunderMeterData = useMemo(
() => ({
goodMoves: { moves: [], probability: 0 },
okMoves: { moves: [], probability: 0 },
blunderMoves: { moves: [], probability: 0 },
}),
[],
)
const emptyRecommendations = useMemo(
() => ({
maia: undefined,
stockfish: undefined,
}),
[],
)
const mockHover = useCallback(() => void 0, [])
const mockMakeMove = useCallback(() => void 0, [])
const highlightMoveEvaluation = analysisEnabled
? (controller.moveEvaluation as {
maia?: MaiaEvaluation
stockfish?: StockfishEvaluation
})
: {
maia: undefined,
stockfish: undefined,
}
const highlightBoardDescription: HighlightBoardDescription = analysisEnabled
? controller.boardDescription
: {
segments: [
{
type: 'text',
content:
'Analysis is disabled. Enable analysis to see detailed move evaluations and recommendations.',
},
],
}
const highlightProps: ComponentProps<typeof Highlight> = {
hover: analysisEnabled ? hover : mockHover,
makeMove: analysisEnabled ? makeMove : mockMakeMove,
currentMaiaModel: controller.currentMaiaModel,
setCurrentMaiaModel: controller.setCurrentMaiaModel,
recommendations: analysisEnabled
? controller.moveRecommendations
: emptyRecommendations,
moveEvaluation: highlightMoveEvaluation,
colorSanMapping: analysisEnabled ? controller.colorSanMapping : {},
boardDescription: highlightBoardDescription,
currentNode: controller.currentNode ?? undefined,
simplified: false,
hideStockfishEvalSummary: hideDetailedBlunderMeter,
hideWhiteWinRateSummary: hideDetailedBlunderMeter,
}
const simplifiedBlunderMeterProps: ComponentProps<
typeof SimplifiedBlunderMeter
> = {
hover: analysisEnabled ? hover : mockHover,
makeMove: analysisEnabled ? makeMove : mockMakeMove,
data: analysisEnabled ? controller.blunderMeter : emptyBlunderMeterData,
colorSanMapping: analysisEnabled ? controller.colorSanMapping : {},
moveEvaluation: analysisEnabled ? controller.moveEvaluation : undefined,
playerToMove: analysisEnabled ? (controller.currentNode?.turn ?? 'w') : 'w',
}
const blunderMeterProps: ComponentProps<typeof BlunderMeter> = {
...simplifiedBlunderMeterProps,
}
const movesByRatingProps = {
moves: analysisEnabled ? controller.movesByRating : undefined,
colorSanMapping: analysisEnabled ? controller.colorSanMapping : {},
positionKey: analysisEnabled ? controller.currentNode?.fen : undefined,
}
const renderHeader = (
variant: 'desktop' | 'mobile',
extraClasses?: string,
) => {
const containerClasses = [
'flex h-10 min-h-10 items-center justify-between border-b border-glass-border bg-transparent text-white/90',
variant === 'desktop' ? 'px-4' : 'px-3',
variant === 'mobile' ? 'backdrop-blur-md' : '',
extraClasses ?? '',
]
.filter(Boolean)
.join(' ')
const buttonBase =
variant === 'desktop'
? 'flex items-center gap-1 rounded-md border border-glass-border bg-glass px-2 py-1 text-xs transition-colors hover:bg-glass-stronger'
: 'flex items-center gap-1 rounded-md border border-glass-border bg-glass px-2 py-1 text-xs transition-colors'
const visibilityButtonClass = `${buttonBase} ${
analysisEnabled ? 'text-white' : 'text-white/80'
}`
return (
<div className={containerClasses}>
<div className="flex items-center gap-2">
<span className="material-symbols-outlined text-xl text-white/80">
analytics
</span>
<h3
className={
variant === 'desktop'
? 'font-semibold text-white'
: 'text-sm font-semibold text-white'
}
>
Analysis
</h3>
</div>
<div className="flex items-center gap-2">
<button
type="button"
onClick={handleToggleAnalysis}
className={visibilityButtonClass}
aria-pressed={analysisEnabled}
>
<span className="material-symbols-outlined !text-xs text-white/80">
{analysisEnabled ? 'visibility_off' : 'visibility'}
</span>
<span className="text-white/90">
{analysisEnabled ? 'Hide' : 'Show'}
</span>
</button>
</div>
</div>
)
}
const renderDisabledOverlay = (
message: string,
options: { offsetTop?: boolean } = {},
) => {
const offsetClasses = options.offsetTop
? 'bottom-0 left-0 right-0 top-10'
: 'inset-0'
return (
<div
className={`pointer-events-none absolute z-10 flex items-center justify-center overflow-hidden rounded-md border border-glass-border bg-backdrop/90 backdrop-blur-md ${offsetClasses}`}
>
<div className="rounded-md border border-glass-border bg-gradient-to-br from-white/10 to-white/5 p-4 text-center">
<span className="material-symbols-outlined mb-2 text-3xl text-white/80">
lock
</span>
<p className="font-medium tracking-wide text-white">
Analysis Disabled
</p>
<p className="text-xs text-secondary">{message}</p>
</div>
</div>
)
}
const detailedLayout = (
<>
<div className="hidden xl:flex xl:h-full xl:flex-col xl:gap-3">
<div
className="desktop-analysis-big-row-1-container relative flex gap-3"
style={{ height: `calc((${desktopContentHeightCss} - 0.75rem) / 2)` }}
>
<div className="flex h-full w-full flex-col overflow-hidden rounded-md border border-glass-border bg-glass-strong backdrop-blur-md">
{renderHeader('desktop')}
<div className="flex h-full w-full flex-1">
<SimplifiedAnalysisOverview
highlightProps={{ ...highlightProps, simplified: true }}
blunderMeterProps={simplifiedBlunderMeterProps}
analysisEnabled={analysisEnabled}
hideBlunderMeter={hideDetailedBlunderMeter}
/>
</div>
</div>
{!analysisEnabled &&
renderDisabledOverlay('Enable analysis to see move evaluations', {
offsetTop: true,
})}
</div>
<div
className="desktop-analysis-big-row-2-container relative flex flex-row gap-3"
style={{ height: `calc((${desktopContentHeightCss} - 0.75rem) / 2)` }}
>
<div className="relative flex h-full w-full flex-col overflow-hidden rounded-md border border-glass-border bg-glass backdrop-blur-md">
<MovesByRating {...movesByRatingProps} />
</div>
{!hideDetailedBlunderMeter && <BlunderMeter {...blunderMeterProps} />}
{!analysisEnabled &&
renderDisabledOverlay('Enable analysis to see position evaluation')}
</div>
</div>
<div className="flex h-full flex-col gap-3 xl:hidden">
<div className="desktop-analysis-small-row-1-container relative flex overflow-hidden rounded-md border border-glass-border bg-glass-strong pt-10 backdrop-blur-md">
{renderHeader('mobile', 'absolute left-0 top-0 z-10 w-full')}
<div className="flex h-full w-full">
<SimplifiedAnalysisOverview
highlightProps={{ ...highlightProps, simplified: true }}
blunderMeterProps={simplifiedBlunderMeterProps}
analysisEnabled={analysisEnabled}
hideBlunderMeter={hideDetailedBlunderMeter}
/>
</div>
{!analysisEnabled &&
renderDisabledOverlay('Enable analysis to see move evaluations', {
offsetTop: true,
})}
</div>
<div className="desktop-analysis-small-row-2-container relative flex w-full">
<div className="relative flex h-full w-full flex-col overflow-hidden rounded-md border border-glass-border bg-glass backdrop-blur-md">
<MovesByRating {...movesByRatingProps} />
{!analysisEnabled &&
renderDisabledOverlay('Enable analysis to see move evaluations')}
</div>
</div>
</div>
</>
)
return (
<motion.div
id="analysis"
variants={itemVariants ?? {}}
className="desktop-right-column-container flex flex-col gap-3"
style={{ willChange: 'transform, opacity', ...containerStyle }}
>
<div className="min-h-0 flex-1">{detailedLayout}</div>
{footerContent ? <div className="shrink-0">{footerContent}</div> : null}
</motion.div>
)
}