Skip to content

Commit

Permalink
Actually validate timestamp and value of sensor readings
Browse files Browse the repository at this point in the history
  • Loading branch information
user890104 committed Jan 14, 2024
1 parent 190c70a commit 4474bd4
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/widgets/SensorReading/SensorReading.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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];
Expand Down

0 comments on commit 4474bd4

Please sign in to comment.