import React, { useState, useEffect, useRef } from 'react'; import { Grid, FormControlLabel, Switch, IconButton, TextField, makeStyles, Paper, Slider, Toolbar, Tooltip, Typography, } from '@material-ui/core'; import ArrowBackIcon from '@material-ui/icons/ArrowBack'; import SettingsIcon from '@material-ui/icons/Settings'; import PlayArrowIcon from '@material-ui/icons/PlayArrow'; import PauseIcon from '@material-ui/icons/Pause'; import FastForwardIcon from '@material-ui/icons/FastForward'; import FastRewindIcon from '@material-ui/icons/FastRewind'; import { useHistory } from 'react-router-dom'; import { useSelector } from 'react-redux'; import Map from '../map/Map'; import ReplayPathMap from '../map/ReplayPathMap'; import PositionsMap from '../map/PositionsMap'; import { formatTime } from '../common/formatter'; import ReportFilter from './ReportFilter'; import { useTranslation } from '../LocalizationProvider'; const useStyles = makeStyles((theme) => ({ root: { height: '100vh', }, sidebar: { position: 'absolute', left: 0, top: 0, margin: theme.spacing(1.5), width: theme.dimensions.drawerWidthDesktop, [theme.breakpoints.down('md')]: { width: '100%', margin: 0, }, }, formControlLabel: { height: '100%', width: '100%', paddingRight: theme.spacing(1), justifyContent: 'space-between', alignItems: 'center', }, reportFilterContainer: { flex: 1, padding: theme.spacing(2), [theme.breakpoints.down('md')]: { margin: theme.spacing(1), }, }, sliderContainer: { padding: theme.spacing(2), }, })); const TimeLabel = ({ children, open, value }) => ( {children} ); const ReplayPage = () => { const t = useTranslation(); const classes = useStyles(); const history = useHistory(); const timerRef = useRef(); const [positions, setPositions] = useState([]); const [index, setIndex] = useState(0); const [selectedDeviceId, setSelectedDeviceId] = useState(); const [playbackSpeed, setPlaybackSpeed] = useState(''); const [expanded, setExpanded] = useState(true); const [isPlaying, setIsPlaying] = useState(false); const deviceName = useSelector((state) => { if (selectedDeviceId) { const device = state.devices.items[selectedDeviceId]; if (device) { return device.name; } } return null; }); useEffect(() => { if (isPlaying && positions.length > 0) { timerRef.current = setInterval(() => { setIndex((index) => index + 1); }, 500); } else { clearInterval(timerRef.current); } return () => clearInterval(timerRef.current); }, [isPlaying, positions]); useEffect(() => { if (index >= positions.length) { clearInterval(timerRef.current); } }, [index, positions]); const handleSubmit = async (deviceId, from, to, _, headers) => { setSelectedDeviceId(deviceId); const query = new URLSearchParams({ deviceId, from, to }); const response = await fetch(`/api/positions?${query.toString()}`, { headers }); if (response.ok) { setIndex(0); setPositions(await response.json()); setExpanded(false); } }; return (
{index < positions.length && }
history.push('/')}> {t('reportReplay')} {!expanded && ( setExpanded(true)}> )} {!expanded ? ( {deviceName} ({ value: index }))} value={index} onChange={(_, index) => setIndex(index)} valueLabelDisplay="auto" valueLabelFormat={(i) => (i < positions.length ? formatTime(positions[i]) : '')} ValueLabelComponent={TimeLabel} /> {`${index}/${positions.length}`} setIndex((index) => index - 1)} disabled={isPlaying}> setIsPlaying(!isPlaying)}> {isPlaying ? : } setIndex((index) => index + 1)} disabled={isPlaying}> {formatTime(positions[index])} ) : ( setPlaybackSpeed(e.target.value)} variant="filled" /> setIsPlaying(e.target.checked)} color="primary" edge="start" /> )} label={t('reportAutoPlay')} labelPlacement="start" /> )}
); }; export default ReplayPage;