feat(rithmomachia): add CaptureContext for capture dialog state management
Add centralized context provider for capture dialog subsystem. Provides: - Layout configuration (targetPos, cellSize, gap, padding) - Piece references (mover, target, helper) - Selection state (selectedRelation, closing) - Helper functions (findValidHelpers, selectRelation, selectHelper, dismissDialog) This eliminates 8-15 props from each capture component and provides a single source of truth for capture state. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
'use client'
|
||||
|
||||
import { createContext, type ReactNode, useContext } from 'react'
|
||||
import type { Piece, RelationKind } from '../types'
|
||||
|
||||
/**
|
||||
* Layout information for the capture dialog
|
||||
*/
|
||||
export interface CaptureLayout {
|
||||
targetPos: { x: number; y: number }
|
||||
cellSize: number
|
||||
gap: number
|
||||
padding: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Pieces involved in the current capture attempt
|
||||
*/
|
||||
export interface CapturePieces {
|
||||
mover: Piece
|
||||
target: Piece
|
||||
helper: Piece | null
|
||||
}
|
||||
|
||||
/**
|
||||
* Context value for the capture dialog subsystem
|
||||
*/
|
||||
export interface CaptureContextValue {
|
||||
// Layout (calculated once, shared by all capture components)
|
||||
layout: CaptureLayout
|
||||
|
||||
// Pieces involved in capture
|
||||
pieces: CapturePieces
|
||||
|
||||
// Capture state
|
||||
selectedRelation: RelationKind | null
|
||||
closing: boolean
|
||||
|
||||
// All pieces on the board (for validation)
|
||||
allPieces: Piece[]
|
||||
|
||||
// Helper functions
|
||||
findValidHelpers: (moverValue: number, targetValue: number, relation: RelationKind) => Piece[]
|
||||
|
||||
// Actions
|
||||
selectRelation: (relation: RelationKind) => void
|
||||
selectHelper: (pieceId: string) => void
|
||||
dismissDialog: () => void
|
||||
}
|
||||
|
||||
const CaptureContext = createContext<CaptureContextValue | null>(null)
|
||||
|
||||
/**
|
||||
* Hook to access capture context
|
||||
*/
|
||||
export function useCaptureContext(): CaptureContextValue {
|
||||
const context = useContext(CaptureContext)
|
||||
if (!context) {
|
||||
throw new Error('useCaptureContext must be used within CaptureProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for capture dialog context
|
||||
*/
|
||||
export function CaptureProvider({
|
||||
children,
|
||||
value,
|
||||
}: {
|
||||
children: ReactNode
|
||||
value: CaptureContextValue
|
||||
}) {
|
||||
return <CaptureContext.Provider value={value}>{children}</CaptureContext.Provider>
|
||||
}
|
||||
Reference in New Issue
Block a user