All files / web/src/app/create/worksheets/components WorksheetConfigContext.tsx

0% Statements 0/59
0% Branches 0/1
0% Functions 0/1
0% Lines 0/59

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                                                                                                                       
'use client'

import { createContext, useContext, useMemo } from 'react'
import type { WorksheetFormState } from '@/app/create/worksheets/types'

/**
 * Context for worksheet configuration state
 * Eliminates prop drilling for formState, onChange, and operator
 */
export interface WorksheetConfigContextValue {
  formState: WorksheetFormState
  onChange: (updates: Partial<WorksheetFormState>) => void
  operator: 'addition' | 'subtraction' | 'mixed'
  isReadOnly?: boolean
}

export const WorksheetConfigContext = createContext<WorksheetConfigContextValue | null>(null)

/**
 * Hook to access worksheet configuration context
 * @throws Error if used outside of WorksheetConfigProvider
 */
export function useWorksheetConfig() {
  const context = useContext(WorksheetConfigContext)
  if (!context) {
    throw new Error('useWorksheetConfig must be used within WorksheetConfigProvider')
  }
  return context
}

export interface WorksheetConfigProviderProps {
  formState: WorksheetFormState
  updateFormState: (updates: Partial<WorksheetFormState>) => void
  children: React.ReactNode
  isReadOnly?: boolean
}

/**
 * Provider component for worksheet configuration context
 * Wrap config panel components to provide access to formState, onChange, and operator
 */
export function WorksheetConfigProvider({
  formState,
  updateFormState,
  children,
  isReadOnly = false,
}: WorksheetConfigProviderProps) {
  const value = useMemo(
    () => ({
      formState,
      onChange: updateFormState,
      operator: formState.operator || 'addition',
      isReadOnly,
    }),
    [formState, updateFormState, isReadOnly]
  )

  return <WorksheetConfigContext.Provider value={value}>{children}</WorksheetConfigContext.Provider>
}