File size: 2,503 Bytes
1c34a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useCallback, useMemo, useState, type ReactNode } from 'react'
import { AppContext, type InvestigationIntent, type ProjectionInputs } from './context'
import type { RunSetRecord, ScreenName } from '../types'

export function AppProvider({ children }: { children: ReactNode }) {
  const [activeScreen, setActiveScreen] = useState<ScreenName>('Dashboard')
  const [toast, setToast] = useState('')
  const [investigationIntent, setInvestigationIntent] = useState<InvestigationIntent>(null)
  const [projectionSetupActive, setProjectionSetupActive] = useState(false)
  const [runSets, setRunSets] = useState<RunSetRecord[]>([])
  const [activeExecutionRunSetId, setActiveExecutionRunSetId] = useState<string | null>(null)
  const [projectionInputs, setProjectionInputs] = useState<ProjectionInputs>({
    model: null,
    inforce: null,
    assumptionTableIds: [],
    factorTableIds: [],
    scenario: null,
    draft: {
      name: '',
      formulaConfirmed: false,
      inforceConfirmed: false,
      assumptionValidated: false,
      factorValidated: false,
      scenarioCompatible: false,
      startMonth: '2026-01',
      endMonth: '2045-12',
      traceScope: [],
    },
  })
  const notify = useCallback((message: string) => { setToast(message); window.setTimeout(() => setToast(''), 2600) }, [])
  const openInvestigation = useCallback((intent: NonNullable<InvestigationIntent>) => { setInvestigationIntent(intent); setActiveScreen(intent.kind === 'mentor' ? 'Mentor' : 'Investigate') }, [])
  const clearInvestigationIntent = useCallback(() => setInvestigationIntent(null), [])
  const updateProjectionInputs = useCallback((update: (current: ProjectionInputs) => ProjectionInputs) => setProjectionInputs(update), [])
  const addRunSet = useCallback((runSet: RunSetRecord) => setRunSets(current => [runSet, ...current]), [])
  const value = useMemo(() => ({ activeScreen, setActiveScreen, toast, notify, investigationIntent, openInvestigation, clearInvestigationIntent, projectionInputs, updateProjectionInputs, projectionSetupActive, setProjectionSetupActive, runSets, addRunSet, activeExecutionRunSetId, setActiveExecutionRunSetId }), [activeScreen, toast, notify, investigationIntent, openInvestigation, clearInvestigationIntent, projectionInputs, updateProjectionInputs, projectionSetupActive, setProjectionSetupActive, runSets, addRunSet, activeExecutionRunSetId])
  return <AppContext.Provider value={value}>{children}</AppContext.Provider>
}