diff --git a/src/components/SensorTypePicker.jsx b/src/components/SensorTypePicker.jsx index 17d4ca1..aa612d6 100644 --- a/src/components/SensorTypePicker.jsx +++ b/src/components/SensorTypePicker.jsx @@ -66,18 +66,38 @@ export default function SensorTypePicker(props) { } } + if (props.value === null) { + if (props.allUnits) { + props.onChange(opts[0]) + } else { + props.onChange(opts[0].sensorType) + } + } - let current = "" - if (props.value !== null && typeof (props.value) === "object") { - current = t(getUnitHelper(props.value.sensorType).label) + (props.value.unit ? ` (${t(props.value.unit.translationKey)})` : "") - } else { - let v = props.value || opts[0]; - if (typeof (v) === "object") { - current = t(getUnitHelper(v.sensorType).label) + (v.unit ? ` (${t(v.unit.translationKey)})` : "") - } else current = t(getUnitHelper(v).label) || t(v); + const getLabelForOption = (value) => { + let label = "" + let sensorType = value?.sensorType || value + let unit = value?.unit + if (sensorType === null) return "" + if (props.allUnits) { + if (sensorType === "humidity" && unit?.cloudStoreKey === "0") { + label = t(getUnitHelper(sensorType).label) + " (" + t("humidity_relative_name") +")" + } + else if (sensorType === "humidity" && unit?.cloudStoreKey === "1") { + label = t(getUnitHelper(sensorType).label) + " (" + t("humidity_absolute_name") +")" + } + else if (sensorType === "humidity" && unit?.cloudStoreKey === "2") { + label = t(unit.translationKey) + " (" + t(getUnitHelper("temperature").unit) + ")" + } + else { + label = t(getUnitHelper(sensorType).label) + (unit ? ` (${t(unit.translationKey)})` : "") + } + } else label = t(getUnitHelper(sensorType).label) || t(sensorType); + + let valueTextMaxLength = props.allUnits ? 30 : 15 + if (label.length > valueTextMaxLength) label = label.substring(0, valueTextMaxLength) + "..." + return label } - let valueTextMaxLength = props.allUnits ? 30 : 15 - if (current.length > valueTextMaxLength) current = current.substring(0, valueTextMaxLength) + "..." return (