import React, { Component } from 'react'; import { Grid, Typography, Button, ButtonGroup, Box, Alert } from '@mui/material'; import ControllerCard from './ControllerCard'; import OutputChart from './OutputChart'; import { withI18n } from './I18nContext'; import { withDevices } from './DevicesContext'; class Dashboard extends Component { constructor(props) { super(props); this.state = { range: 'day', // 'day', 'week', 'month' offset: 0 }; } setRange = (range) => { this.setState({ range, offset: 0 }); }; setOffset = (offset) => { if (offset < 0) offset = 0; this.setState({ offset }); }; render() { const { i18n: { t }, devicesCtx: { devices, groupedDevices, loading, error } } = this.props; const { range, offset } = this.state; const nowMs = Date.now(); const durationSec = (range === 'week' ? 7 * 24 * 3600 : (range === 'month' ? 30 * 24 * 3600 : 24 * 3600)); const endMs = nowMs - (offset * durationSec * 1000); const startMs = endMs - (durationSec * 1000); const dateOpts = { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }; const dateRangeLabel = `${new Date(startMs).toLocaleString([], dateOpts)} - ${new Date(endMs).toLocaleString([], dateOpts)}`; if (loading) return {t('dashboard.loading')}; if (error) return {error}; return ( {dateRangeLabel} {Object.entries(groupedDevices).map(([controllerName, ports]) => ( ))} ); } } export default withDevices(withI18n(Dashboard));