From 70de6e87e543a811aea4669a575ef605b599f5a4 Mon Sep 17 00:00:00 2001 From: Thibault Reidy Date: Wed, 30 Oct 2024 10:30:01 +0100 Subject: [PATCH] feat: improve null context errors handling --- src/context/SimulationContext.tsx | 29 ++++++++++------------------- src/modules/models/Tree/Tree.tsx | 5 +---- src/modules/scenes/FirstScene.tsx | 7 +++---- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/context/SimulationContext.tsx b/src/context/SimulationContext.tsx index 0433d5c..f114d09 100644 --- a/src/context/SimulationContext.tsx +++ b/src/context/SimulationContext.tsx @@ -33,8 +33,6 @@ import { initSlidingWindow, } from '../models/TemperatureIterator'; -const SIMULATION_ERROR_CONTEXT = undefinedContextErrorFactory('Simulation'); - type SimulationContextType = { status: SimulationStatus; heatLoss: FormattedHeatLoss; @@ -49,21 +47,7 @@ type SimulationContextType = { startSimulation: () => void; }; -const SimulationContext = createContext({ - status: SimulationStatus.IDLE, - heatLoss: { value: 0, unit: 'W' }, - totalHeatLoss: { value: 0, unit: 'W' }, - electricityCost: 0, - indoorTemperature: 0, - outdoorTemperature: 0, - progression: { progress: 0, timeLeft: 0 }, - period: { from: new Date(), to: new Date(), durationInHours: 0 }, - duration: { value: 0, unit: TimeUnit.Hours }, - material: { price: 0, thermalConductivity: 0, thickness: 0 }, - startSimulation: () => { - throw SIMULATION_ERROR_CONTEXT; - }, -}); +const SimulationContext = createContext(null); type Props = { children: ReactNode; @@ -194,5 +178,12 @@ export const SimulationProvider = ({ ); }; -export const useSimulation = (): SimulationContextType => - useContext(SimulationContext); +export const useSimulation = (): SimulationContextType => { + const context = useContext(SimulationContext); + + if (!context) { + throw undefinedContextErrorFactory('Simulation'); + } + + return context; +}; diff --git a/src/modules/models/Tree/Tree.tsx b/src/modules/models/Tree/Tree.tsx index 4873dc2..569d4e7 100644 --- a/src/modules/models/Tree/Tree.tsx +++ b/src/modules/models/Tree/Tree.tsx @@ -6,14 +6,12 @@ Model: "Trees" from: https://www.sloyd.ai/ */ import { GroupProps } from '@react-three/fiber'; -import { Seasons } from '@/types/seasons'; - import { useTree } from './useTree'; type Props = GroupProps; export const Tree = (props: Props): JSX.Element => { - const { nodes, materials, season } = useTree(); + const { nodes, materials } = useTree(); /** * This code has been generated with the command `npx gltfjsx`. @@ -24,7 +22,6 @@ export const Tree = (props: Props): JSX.Element => { { - {/* Ambient Light for overall illumination */} - + {/* Main Sunlight Simulation */}