refactor(guide-contexts): ♻️ Implement cleaner context provider and state management structure

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Claude Code 2026-04-01 06:23:51 -07:00
parent 3003be5004
commit b542987d42

View file

@ -0,0 +1,107 @@
import { createContext, useContext, type ReactNode } from 'react'
import type {
Keyword, Race, EncyclopediaEntry,
Unit, Building, Spell, Tech, Terrain, Resource,
Improvement, Government, Era, Victory, MapType, Item,
PromotionsData, DisciplinesData,
EcologicalEventCategory, CrossTriggers,
WeatherEvent, LeyChanneling, TerrainModifier,
InfusionTree, InfusionConfig,
EncyclopediaCategoryMeta,
} from '../types/game-data'
// ---------------------------------------------------------------------------
// Context value — comprehensive data surface for all shared pages/components
// ---------------------------------------------------------------------------
/** Wind band definition from climate_params. */
export interface WindBandData {
name: string
dir: 'east' | 'west' | 'calm'
dirLabel: string
speed: number
}
/** Merged weather event with effects/thresholds for the events page. */
export interface MergedWeatherEvent extends WeatherEvent {
effects: Record<string, number>
thresholds: Record<string, number>
}
export interface GuideDataContextValue {
// Game identity
packId: string
packName: string
// Core game-pack data
races: Race[]
units: Unit[]
buildings: Building[]
spells: Spell[]
techs: Tech[]
keywords: Keyword[]
terrains: Terrain[]
resources: Resource[]
improvements: Improvement[]
governments: Government[]
eras: Era[]
victories: Victory[]
mapTypes: MapType[]
items: Item[]
encyclopediaEntries: EncyclopediaEntry[]
encyclopediaCategories: EncyclopediaCategoryMeta[]
promotionsData: PromotionsData
disciplinesData: DisciplinesData
infusionConfig: InfusionConfig
infusionTrees: InfusionTree[]
// Derived building splits
playerWonders: Building[]
playerBuildings: Building[]
npcBuildings: Building[]
// Climate / engine data (per-pack subscriptions to shared resources)
climateSpec: Record<string, unknown>
climateParams: Record<string, unknown>
hydrologyParams: Record<string, unknown>
weatherEvents: MergedWeatherEvent[]
windBands: WindBandData[]
qualityUpThreshold: number
qualityDownThreshold: number
mountainRainShadowBlock: number
allEventCategories: Record<string, EcologicalEventCategory>
crossTriggers: CrossTriggers
leyChanneling: LeyChanneling
terrainModifiers: Record<string, TerrainModifier>
// Derived / computed
techTierMap: ReadonlyMap<string, number>
}
// ---------------------------------------------------------------------------
// Context + provider
// ---------------------------------------------------------------------------
const GuideDataContext = createContext<GuideDataContextValue | null>(null)
export function GuideDataProvider({
data,
children,
}: {
data: GuideDataContextValue
children: ReactNode
}) {
return (
<GuideDataContext.Provider value={data}>
{children}
</GuideDataContext.Provider>
)
}
export function useGuideData(): GuideDataContextValue {
const ctx = useContext(GuideDataContext)
if (!ctx) {
throw new Error('useGuideData must be used within a <GuideDataProvider>')
}
return ctx
}