Skip to content

Commit

Permalink
feat: improve null context errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ReidyT committed Oct 30, 2024
1 parent 734373f commit 70de6e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
29 changes: 10 additions & 19 deletions src/context/SimulationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import {
initSlidingWindow,
} from '../models/TemperatureIterator';

const SIMULATION_ERROR_CONTEXT = undefinedContextErrorFactory('Simulation');

type SimulationContextType = {
status: SimulationStatus;
heatLoss: FormattedHeatLoss;
Expand All @@ -49,21 +47,7 @@ type SimulationContextType = {
startSimulation: () => void;
};

const SimulationContext = createContext<SimulationContextType>({
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<SimulationContextType | null>(null);

type Props = {
children: ReactNode;
Expand Down Expand Up @@ -194,5 +178,12 @@ export const SimulationProvider = ({
);
};

export const useSimulation = (): SimulationContextType =>
useContext<SimulationContextType>(SimulationContext);
export const useSimulation = (): SimulationContextType => {
const context = useContext(SimulationContext);

if (!context) {
throw undefinedContextErrorFactory('Simulation');
}

return context;
};
5 changes: 1 addition & 4 deletions src/modules/models/Tree/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -24,7 +22,6 @@ export const Tree = (props: Props): JSX.Element => {
<mesh
geometry={nodes.Lofted_Patch.geometry}
material={materials.Leaf}
visible={season !== Seasons.Winter}
position={[0, 1.227, 0]}
/>
<mesh
Expand Down
7 changes: 3 additions & 4 deletions src/modules/scenes/FirstScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ const FirstSceneComponent = (): JSX.Element => {
</Stack>
<Canvas
style={{ height: '100vh', width: '100vw' }}
camera={{ position: [10, 1, -15], fov: 75 }}
camera={{ position: [10, 1, -20], fov: 75 }}
>
<color attach="background" args={[0x87ceeb]} />
{/* Ambient Light for overall illumination */}
<ambientLight intensity={1.2} />
<ambientLight intensity={1.5} />
{/* Main Sunlight Simulation */}
<directionalLight
position={[6, 30, -10]}
intensity={2}
intensity={2.5}
color={0xffffff}
/>
<OrbitControls
Expand Down

0 comments on commit 70de6e8

Please sign in to comment.