From 4474bd42b0e18ccc11dc7ddc7fe475a6aa3b49e5 Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Sun, 14 Jan 2024 17:18:14 +0200 Subject: [PATCH] Actually validate timestamp and value of sensor readings --- src/widgets/SensorReading/SensorReading.jsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/widgets/SensorReading/SensorReading.jsx b/src/widgets/SensorReading/SensorReading.jsx index f18db29..613afdd 100644 --- a/src/widgets/SensorReading/SensorReading.jsx +++ b/src/widgets/SensorReading/SensorReading.jsx @@ -2,6 +2,7 @@ import { Card, Col, Container, Row } from 'react-bootstrap'; import PropTypes from 'prop-types'; import './SensorReading.css'; import { useDateTimeFormatter } from '../../utils/useDateTimeFormatter.js'; +import { isValid } from 'date-fns'; const units = { Temperature: ['°C', 1], @@ -20,6 +21,11 @@ const SensorReading = ({ formatDefault, formatDistanceToNow, } = useDateTimeFormatter(); + + if (!isValid(timestamp) || typeof value !== 'number') { + return null; + } + const lastUpdate = new Date(timestamp); const formattedTimestamp = formatDefault(lastUpdate) + ' (' + formatDistanceToNow(lastUpdate) + ')'; const unit = units[type];