Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 54x 54x 4x 4x 50x 50x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 48x 48x 48x 48x 48x 48x 48x 48x 1x 1x 1x 1x 2x 2x 1x 1x 1x 48x 48x 48x 48x 48x 48x 48x 48x 2x 2x 2x 2x 2x 2x 48x 48x 48x 48x 48x 48x 48x 48x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 3x 1x 1x 1x 1x 3x 48x 48x 48x 48x 48x 48x 48x 48x 2x 1x 1x 1x 1x 1x 2x 48x 48x 48x 48x 48x 48x 48x 24x 24x 48x 48x 48x 48x 48x 48x 48x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x | 'use client'
import {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
type ReactNode,
} from 'react'
import {
generateUnifiedInstructionSequence,
type PedagogicalSegment,
type UnifiedInstructionSequence,
type UnifiedStepData,
} from '@/utils/unifiedStepGenerator'
// ============================================================================
// Types
// ============================================================================
export interface DecompositionContextConfig {
/** Starting abacus value */
startValue: number
/** Target abacus value to reach */
targetValue: number
/** Current step index for highlighting (optional) */
currentStepIndex?: number
/** Callback when active segment changes (optional) */
onSegmentChange?: (segment: PedagogicalSegment | null) => void
/** Callback when a term is hovered (optional) */
onTermHover?: (termIndex: number | null, columnIndex: number | null) => void
/** Number of abacus columns for column mapping (default: 5) */
abacusColumns?: number
}
export interface DecompositionContextType {
// Generated data
sequence: UnifiedInstructionSequence
fullDecomposition: string
isMeaningfulDecomposition: boolean
termPositions: Array<{ startIndex: number; endIndex: number }>
segments: PedagogicalSegment[]
steps: UnifiedStepData[]
// Configuration
startValue: number
targetValue: number
currentStepIndex: number
abacusColumns: number
// Highlighting state
activeTermIndices: Set<number>
setActiveTermIndices: (indices: Set<number>) => void
activeIndividualTermIndex: number | null
setActiveIndividualTermIndex: (index: number | null) => void
// Derived functions
getColumnFromTermIndex: (termIndex: number, useGroupColumn?: boolean) => number | null
getTermIndicesFromColumn: (columnIndex: number) => number[]
getGroupTermIndicesFromTermIndex: (termIndex: number) => number[]
// Event handlers
handleTermHover: (termIndex: number, isHovering: boolean) => void
handleColumnHover: (columnIndex: number, isHovering: boolean) => void
}
// ============================================================================
// Context
// ============================================================================
const DecompositionContext = createContext<DecompositionContextType | null>(null)
/**
* Hook to access decomposition context. Throws if not inside DecompositionProvider.
*/
export function useDecomposition(): DecompositionContextType {
const context = useContext(DecompositionContext)
if (!context) {
throw new Error('useDecomposition must be used within a DecompositionProvider')
}
return context
}
/**
* Optional hook that returns null if not in provider (for conditional usage).
*/
export function useDecompositionOptional(): DecompositionContextType | null {
return useContext(DecompositionContext)
}
// ============================================================================
// Provider
// ============================================================================
interface DecompositionProviderProps extends DecompositionContextConfig {
children: ReactNode
}
/**
* Provider for decomposition display.
*
* Wraps any area where you want to show an interactive decomposition display.
* Only requires startValue and targetValue - all other data is derived.
*
* @example
* ```tsx
* <DecompositionProvider startValue={0} targetValue={45}>
* <DecompositionDisplay />
* </DecompositionProvider>
* ```
*/
export function DecompositionProvider({
startValue,
targetValue,
currentStepIndex = 0,
onSegmentChange,
onTermHover,
abacusColumns = 5,
children,
}: DecompositionProviderProps) {
// -------------------------------------------------------------------------
// Generate sequence (memoized on value changes)
// -------------------------------------------------------------------------
const sequence = useMemo(
() => generateUnifiedInstructionSequence(startValue, targetValue),
[startValue, targetValue]
)
// -------------------------------------------------------------------------
// Highlighting state
// -------------------------------------------------------------------------
const [activeTermIndices, setActiveTermIndices] = useState<Set<number>>(new Set())
const [activeIndividualTermIndex, setActiveIndividualTermIndex] = useState<number | null>(null)
// -------------------------------------------------------------------------
// Derived: term positions from steps
// -------------------------------------------------------------------------
const termPositions = useMemo(
() => sequence.steps.map((step) => step.termPosition),
[sequence.steps]
)
// -------------------------------------------------------------------------
// Derived function: Get column index from term index
// -------------------------------------------------------------------------
const getColumnFromTermIndex = useCallback(
(termIndex: number, useGroupColumn = false): number | null => {
const step = sequence.steps[termIndex]
if (!step?.provenance) return null
// For group highlighting: use rhsPlace (target column of the operation)
// For individual term: use termPlace (specific column this term affects)
const placeValue = useGroupColumn
? step.provenance.rhsPlace
: (step.provenance.termPlace ?? step.provenance.rhsPlace)
// Convert place value to column index (rightmost column is highest index)
return abacusColumns - 1 - placeValue
},
[sequence.steps, abacusColumns]
)
// -------------------------------------------------------------------------
// Derived function: Get term indices that affect a given column
// -------------------------------------------------------------------------
const getTermIndicesFromColumn = useCallback(
(columnIndex: number): number[] => {
const placeValue = abacusColumns - 1 - columnIndex
return sequence.steps
.map((step, index) => ({ step, index }))
.filter(({ step }) => {
if (!step.provenance) return false
return step.provenance.rhsPlace === placeValue || step.provenance.termPlace === placeValue
})
.map(({ index }) => index)
},
[sequence.steps, abacusColumns]
)
// -------------------------------------------------------------------------
// Derived function: Get all term indices in the same complement group
// -------------------------------------------------------------------------
const getGroupTermIndicesFromTermIndex = useCallback(
(termIndex: number): number[] => {
const step = sequence.steps[termIndex]
if (!step?.provenance) return [termIndex]
const groupId = step.provenance.groupId
if (!groupId) return [termIndex]
// Find all steps with the same groupId
return sequence.steps
.map((s, i) => ({ step: s, index: i }))
.filter(({ step: s }) => s.provenance?.groupId === groupId)
.map(({ index }) => index)
},
[sequence.steps]
)
// -------------------------------------------------------------------------
// Event handler: Term hover
// -------------------------------------------------------------------------
const handleTermHover = useCallback(
(termIndex: number, isHovering: boolean) => {
if (isHovering) {
// Set individual term highlight
setActiveIndividualTermIndex(termIndex)
// Set group highlights
const groupIndices = getGroupTermIndicesFromTermIndex(termIndex)
setActiveTermIndices(new Set(groupIndices))
// Notify external listener
if (onTermHover) {
const columnIndex = getColumnFromTermIndex(termIndex, true)
onTermHover(termIndex, columnIndex)
}
} else {
setActiveIndividualTermIndex(null)
setActiveTermIndices(new Set())
onTermHover?.(null, null)
}
},
[getGroupTermIndicesFromTermIndex, getColumnFromTermIndex, onTermHover]
)
// -------------------------------------------------------------------------
// Event handler: Column hover (for bidirectional abacus ↔ decomposition)
// -------------------------------------------------------------------------
const handleColumnHover = useCallback(
(columnIndex: number, isHovering: boolean) => {
if (isHovering) {
const termIndices = getTermIndicesFromColumn(columnIndex)
setActiveTermIndices(new Set(termIndices))
} else {
setActiveTermIndices(new Set())
}
},
[getTermIndicesFromColumn]
)
// -------------------------------------------------------------------------
// Effect: Notify when active segment changes
// -------------------------------------------------------------------------
useEffect(() => {
if (!onSegmentChange) return
const segment = sequence.segments.find((seg) => seg.stepIndices?.includes(currentStepIndex))
onSegmentChange(segment || null)
}, [currentStepIndex, sequence.segments, onSegmentChange])
// -------------------------------------------------------------------------
// Context value
// -------------------------------------------------------------------------
const value: DecompositionContextType = useMemo(
() => ({
// Generated data
sequence,
fullDecomposition: sequence.fullDecomposition,
isMeaningfulDecomposition: sequence.isMeaningfulDecomposition,
termPositions,
segments: sequence.segments,
steps: sequence.steps,
// Configuration
startValue,
targetValue,
currentStepIndex,
abacusColumns,
// Highlighting state
activeTermIndices,
setActiveTermIndices,
activeIndividualTermIndex,
setActiveIndividualTermIndex,
// Derived functions
getColumnFromTermIndex,
getTermIndicesFromColumn,
getGroupTermIndicesFromTermIndex,
// Event handlers
handleTermHover,
handleColumnHover,
}),
[
sequence,
termPositions,
startValue,
targetValue,
currentStepIndex,
abacusColumns,
activeTermIndices,
activeIndividualTermIndex,
getColumnFromTermIndex,
getTermIndicesFromColumn,
getGroupTermIndicesFromTermIndex,
handleTermHover,
handleColumnHover,
]
)
return <DecompositionContext.Provider value={value}>{children}</DecompositionContext.Provider>
}
|