Skip to content

Commit 5f2e0f1

Browse files
feat: add initial working iteration of working opening drilling
1 parent 0159477 commit 5f2e0f1

7 files changed

Lines changed: 215 additions & 103 deletions

File tree

src/components/Board/MovesContainer.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,33 +106,27 @@ export const MovesContainer: React.FC<Props> = (props) => {
106106
const { game, highlightIndices, termination, type } = props
107107
const { isMobile } = useContext(WindowSizeContext)
108108

109-
const controller = useBaseTreeController(type)
110-
const { currentNode, goToNode, gameTree } = controller
109+
const baseController = useBaseTreeController(type)
111110

112111
const mainLineNodes = useMemo(() => {
113-
if (type === 'analysis') {
114-
if (!game.tree) return []
115-
return game.tree.getMainLine()
116-
} else {
117-
return gameTree.getMainLine()
118-
}
119-
}, [game, type, gameTree])
112+
return baseController.gameTree.getMainLine() ?? game.tree.getMainLine()
113+
}, [game, type, baseController.gameTree, baseController.currentNode])
120114

121115
useEffect(() => {
122116
const handleKeyDown = (event: KeyboardEvent) => {
123-
if (!currentNode) return
117+
if (!baseController.currentNode) return
124118

125119
switch (event.key) {
126120
case 'ArrowRight':
127121
event.preventDefault()
128-
if (currentNode.mainChild) {
129-
goToNode(currentNode.mainChild)
122+
if (baseController.currentNode.mainChild) {
123+
baseController.goToNode(baseController.currentNode.mainChild)
130124
}
131125
break
132126
case 'ArrowLeft':
133127
event.preventDefault()
134-
if (currentNode.parent) {
135-
goToNode(currentNode.parent)
128+
if (baseController.currentNode.parent) {
129+
baseController.goToNode(baseController.currentNode.parent)
136130
}
137131
break
138132
default:
@@ -142,7 +136,7 @@ export const MovesContainer: React.FC<Props> = (props) => {
142136

143137
window.addEventListener('keydown', handleKeyDown)
144138
return () => window.removeEventListener('keydown', handleKeyDown)
145-
}, [currentNode, goToNode])
139+
}, [baseController.currentNode, baseController.goToNode])
146140

147141
const moves = useMemo(() => {
148142
const nodes = mainLineNodes.slice(1)
@@ -228,9 +222,11 @@ export const MovesContainer: React.FC<Props> = (props) => {
228222
</div>
229223
{pair.whiteMove && (
230224
<div
231-
onClick={() => goToNode(pair.whiteMove as GameNode)}
225+
onClick={() =>
226+
baseController.goToNode(pair.whiteMove as GameNode)
227+
}
232228
className={`flex min-w-fit cursor-pointer flex-row items-center rounded px-2 py-1 text-sm ${
233-
currentNode === pair.whiteMove
229+
baseController.currentNode === pair.whiteMove
234230
? 'bg-human-4/20'
235231
: 'hover:bg-background-2'
236232
} ${highlightSet.has(pairIndex * 2 + 1) ? 'bg-human-3/80' : ''}`}
@@ -244,9 +240,11 @@ export const MovesContainer: React.FC<Props> = (props) => {
244240
)}
245241
{pair.blackMove && (
246242
<div
247-
onClick={() => goToNode(pair.blackMove as GameNode)}
243+
onClick={() =>
244+
baseController.goToNode(pair.blackMove as GameNode)
245+
}
248246
className={`flex min-w-fit cursor-pointer flex-row items-center rounded px-2 py-1 text-sm ${
249-
currentNode === pair.blackMove
247+
baseController.currentNode === pair.blackMove
250248
? 'bg-human-4/20'
251249
: 'hover:bg-background-2'
252250
} ${highlightSet.has(pairIndex * 2 + 2) ? 'bg-human-3/80' : ''}`}
@@ -263,7 +261,9 @@ export const MovesContainer: React.FC<Props> = (props) => {
263261
{termination && (
264262
<div
265263
className="min-w-fit cursor-pointer border border-primary/10 bg-background-1/90 px-4 py-1 text-sm opacity-90"
266-
onClick={() => goToNode(mainLineNodes[mainLineNodes.length - 1])}
264+
onClick={() =>
265+
baseController.goToNode(mainLineNodes[mainLineNodes.length - 1])
266+
}
267267
>
268268
{termination.result}
269269
{', '}
@@ -287,10 +287,10 @@ export const MovesContainer: React.FC<Props> = (props) => {
287287
</span>
288288
<div
289289
onClick={() => {
290-
if (whiteNode) goToNode(whiteNode)
290+
if (whiteNode) baseController.goToNode(whiteNode)
291291
}}
292292
data-index={index * 2 + 1}
293-
className={`col-span-2 flex h-7 flex-1 cursor-pointer flex-row items-center justify-between px-2 text-sm hover:bg-background-2 ${currentNode === whiteNode && 'bg-human-4/20'} ${highlightSet.has(index * 2 + 1) && 'bg-human-3/80'}`}
293+
className={`col-span-2 flex h-7 flex-1 cursor-pointer flex-row items-center justify-between px-2 text-sm hover:bg-background-2 ${baseController.currentNode === whiteNode && 'bg-human-4/20'} ${highlightSet.has(index * 2 + 1) && 'bg-human-3/80'}`}
294294
>
295295
{whiteNode?.san ?? whiteNode?.move}
296296
{showAnnotations && whiteNode?.blunder && <BlunderIcon />}
@@ -302,16 +302,16 @@ export const MovesContainer: React.FC<Props> = (props) => {
302302
<FirstVariation
303303
color="white"
304304
node={whiteNode}
305-
currentNode={currentNode}
306-
goToNode={goToNode}
305+
currentNode={baseController.currentNode}
306+
goToNode={baseController.goToNode}
307307
/>
308308
) : null}
309309
<div
310310
onClick={() => {
311-
if (blackNode) goToNode(blackNode)
311+
if (blackNode) baseController.goToNode(blackNode)
312312
}}
313313
data-index={index * 2 + 2}
314-
className={`col-span-2 flex h-7 flex-1 cursor-pointer flex-row items-center justify-between px-2 text-sm hover:bg-background-2 ${currentNode === blackNode && 'bg-human-4/20'} ${highlightSet.has(index * 2 + 2) && 'bg-human-3/80'}`}
314+
className={`col-span-2 flex h-7 flex-1 cursor-pointer flex-row items-center justify-between px-2 text-sm hover:bg-background-2 ${baseController.currentNode === blackNode && 'bg-human-4/20'} ${highlightSet.has(index * 2 + 2) && 'bg-human-3/80'}`}
315315
>
316316
{blackNode?.san ?? blackNode?.move}
317317
{showAnnotations && blackNode?.blunder && <BlunderIcon />}
@@ -323,8 +323,8 @@ export const MovesContainer: React.FC<Props> = (props) => {
323323
<FirstVariation
324324
color="black"
325325
node={blackNode}
326-
currentNode={currentNode}
327-
goToNode={goToNode}
326+
currentNode={baseController.currentNode}
327+
goToNode={baseController.goToNode}
328328
/>
329329
) : null}
330330
</>
@@ -333,7 +333,9 @@ export const MovesContainer: React.FC<Props> = (props) => {
333333
{termination && !isMobile && (
334334
<div
335335
className="col-span-5 cursor-pointer rounded-sm border border-primary/10 bg-background-1/90 p-5 text-center opacity-90"
336-
onClick={() => goToNode(mainLineNodes[mainLineNodes.length - 1])}
336+
onClick={() =>
337+
baseController.goToNode(mainLineNodes[mainLineNodes.length - 1])
338+
}
337339
>
338340
{termination.result}
339341
{', '}

src/components/Openings/OpeningDrillSidebar.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ export const OpeningDrillSidebar: React.FC<Props> = ({
3737
}) => {
3838
const currentSelection = selections[currentSelectionIndex]
3939

40-
// Create a base game structure for MovesContainer
41-
const baseGame = {
42-
id: currentSelection?.id || 'opening-drill',
43-
tree: gameTree,
44-
moves: [],
45-
termination: {
46-
result: '*',
47-
winner: 'none' as const,
48-
condition: 'Normal',
49-
},
50-
}
40+
// Create a base game structure for MovesContainer that uses the live tree
41+
const baseGame = React.useMemo(() => {
42+
return {
43+
id: currentSelection?.id || 'opening-drill',
44+
tree: gameTree, // Use the live gameTree from the controller
45+
moves: [],
46+
termination: {
47+
result: '*',
48+
winner: 'none' as const,
49+
condition: 'Normal',
50+
},
51+
}
52+
}, [currentSelection?.id, gameTree, plyCount]) // Add plyCount to dependencies to force updates
5153

5254
return (
5355
<div className="flex h-[85vh] w-72 min-w-60 max-w-72 flex-col gap-2 overflow-hidden 2xl:min-w-72">

0 commit comments

Comments
 (0)