diff --git a/src/app/Marine2/components/boxes/EnvironmentOverview/EnvironmentOverview.tsx b/src/app/Marine2/components/boxes/EnvironmentOverview/EnvironmentOverview.tsx index 033bd268..ca589458 100644 --- a/src/app/Marine2/components/boxes/EnvironmentOverview/EnvironmentOverview.tsx +++ b/src/app/Marine2/components/boxes/EnvironmentOverview/EnvironmentOverview.tsx @@ -1,6 +1,6 @@ import { observer } from "mobx-react-lite" import { useTemperatures, TemperatureInstanceId } from "@victronenergy/mfd-modules" -import { useState, createContext, useCallback } from "react" +import { useState, createContext, useCallback, useEffect } from "react" import { useVisibilityNotifier } from "../../../modules" import Box from "../../ui/Box" import TemperatureData from "./TemperatureData" @@ -20,19 +20,27 @@ interface Props { } export const VisibleComponentsContext = createContext({ - passVisibility: (id: number, visible: boolean) => {}, + passVisibility: (id: number, type: SensorInformationType, visible: boolean) => {}, }) +type SensorInformationType = "humidity" | "pressure" | "temperature" + const EnvironmentOverview = ({ componentMode = "full", pageSelectorPropsSetter }: Props) => { - const { temperatures } = useTemperatures() + const { temperatures: sensors } = useTemperatures() const [visibleElements, setVisibleElements] = useState({}) const [boxSize, setBoxSize] = useState({ width: 0, height: 0 }) - const passVisibility = useCallback((id: number, visible: boolean) => { - setVisibleElements((prev) => ({ ...prev, [id]: visible })) + const passVisibility = useCallback((id: number, type: SensorInformationType, visible: boolean) => { + setVisibleElements((prev: any) => ({ + ...prev, + [id]: { + ...prev[id], + [type]: visible, + }, + })) }, []) - let temperatureComponents = (temperatures || []).map((temperatureId: TemperatureInstanceId) => ( + let temperatureComponents = (sensors || []).map((temperatureId: TemperatureInstanceId) => ( )) - let humidityComponents = (temperatures || []).map((temperatureId: TemperatureInstanceId) => ( + let humidityComponents = (sensors || []).map((temperatureId: TemperatureInstanceId) => ( )) - let pressureComponents = (temperatures || []).map((temperatureId: TemperatureInstanceId) => ( + let pressureComponents = (sensors || []).map((temperatureId: TemperatureInstanceId) => ( sensor.temperature || sensor.humidity || sensor.pressure + ) - useVisibilityNotifier({ widgetName: BOX_TYPES.ENVIRONMENT, visible: Object.values(visibleElements).includes(true) }) + useVisibilityNotifier({ widgetName: BOX_TYPES.ENVIRONMENT, visible: sensorHasData }) if (componentMode === "compact") { return ( diff --git a/src/app/Marine2/components/boxes/EnvironmentOverview/HumidityData.tsx b/src/app/Marine2/components/boxes/EnvironmentOverview/HumidityData.tsx index 43cf4322..ebed68d8 100644 --- a/src/app/Marine2/components/boxes/EnvironmentOverview/HumidityData.tsx +++ b/src/app/Marine2/components/boxes/EnvironmentOverview/HumidityData.tsx @@ -22,7 +22,7 @@ const HumidityData = ({ dataId, componentMode, boxSize }: Props) => { const handlePassVisibility = useCallback( (id: number, isVisible: boolean) => { - passVisibility(id, isVisible) + passVisibility(id, "humidity", isVisible) }, [passVisibility] ) diff --git a/src/app/Marine2/components/boxes/EnvironmentOverview/PressureData.tsx b/src/app/Marine2/components/boxes/EnvironmentOverview/PressureData.tsx index eea882ff..2ed74d5a 100644 --- a/src/app/Marine2/components/boxes/EnvironmentOverview/PressureData.tsx +++ b/src/app/Marine2/components/boxes/EnvironmentOverview/PressureData.tsx @@ -17,12 +17,11 @@ interface Props { const PressureData = ({ dataId, componentMode, boxSize }: Props) => { const { pressure, customName } = usePressure(dataId) - const { passVisibility } = useContext(VisibleComponentsContext) const handlePassVisibility = useCallback( (id: number, isVisible: boolean) => { - passVisibility(id, isVisible) + passVisibility(id, "pressure", isVisible) }, [passVisibility] ) diff --git a/src/app/Marine2/components/boxes/EnvironmentOverview/TemperatureData.tsx b/src/app/Marine2/components/boxes/EnvironmentOverview/TemperatureData.tsx index 3c63a227..58e3697f 100644 --- a/src/app/Marine2/components/boxes/EnvironmentOverview/TemperatureData.tsx +++ b/src/app/Marine2/components/boxes/EnvironmentOverview/TemperatureData.tsx @@ -21,7 +21,7 @@ const TemperatureData = ({ dataId, componentMode, boxSize }: Props) => { const handlePassVisibility = useCallback( (id: number, isVisible: boolean) => { - passVisibility(id, isVisible) + passVisibility(id, "temperature", isVisible) }, [passVisibility] )