import { useState, useEffect, useRef } from 'react' import { useParams, Link } from 'react-router-dom' import { useQuery } from '@tanstack/react-query' import { ArrowLeftIcon, PlayIcon, PauseIcon, ForwardIcon, BackwardIcon, } from '@heroicons/react/24/solid' import clsx from 'clsx' import { viajesApi } from '@/api' import { MapContainer } from '@/components/mapa' import RutaLayer from '@/components/mapa/RutaLayer' import Card from '@/components/ui/Card' import Button from '@/components/ui/Button' import { SkeletonMap } from '@/components/ui/Skeleton' export default function ViajeReplay() { const { id } = useParams<{ id: string }>() const [isPlaying, setIsPlaying] = useState(false) const [currentIndex, setCurrentIndex] = useState(0) const [playbackSpeed, setPlaybackSpeed] = useState(1) const intervalRef = useRef | null>(null) const { data: replayData, isLoading } = useQuery({ queryKey: ['viaje-replay', id], queryFn: () => viajesApi.getReplayData(id!), enabled: !!id, }) const puntos = replayData?.puntos || [] const viaje = replayData?.viaje // Playback control useEffect(() => { if (isPlaying && puntos.length > 0) { intervalRef.current = setInterval(() => { setCurrentIndex((prev) => { if (prev >= puntos.length - 1) { setIsPlaying(false) return prev } return prev + 1 }) }, 1000 / playbackSpeed) } return () => { if (intervalRef.current) { clearInterval(intervalRef.current) } } }, [isPlaying, playbackSpeed, puntos.length]) const handlePlayPause = () => { setIsPlaying(!isPlaying) } const handleSpeedChange = () => { const speeds = [1, 2, 4, 8] const currentSpeedIndex = speeds.indexOf(playbackSpeed) const nextIndex = (currentSpeedIndex + 1) % speeds.length setPlaybackSpeed(speeds[nextIndex]) } const handleSeek = (e: React.ChangeEvent) => { setCurrentIndex(parseInt(e.target.value)) } const handleStepBack = () => { setCurrentIndex(Math.max(0, currentIndex - 10)) } const handleStepForward = () => { setCurrentIndex(Math.min(puntos.length - 1, currentIndex + 10)) } // Current point const currentPoint = puntos[currentIndex] if (isLoading) { return (
) } if (!viaje) { return (

Viaje no encontrado

Volver a viajes
) } return (
{/* Header */}

Replay de viaje - {viaje.vehiculo?.nombre}

{viaje.vehiculo?.placa} | {new Date(viaje.inicio).toLocaleString()}

{/* Map and controls */}
{/* Map */}
{puntos.length > 0 && ( )}
{/* Info panel */}
{/* Current stats */}

Posicion actual

{currentPoint ? (
Hora {currentPoint.timestamp ? new Date(currentPoint.timestamp).toLocaleTimeString() : '--'}
Velocidad {currentPoint.velocidad || 0} km/h
Coordenadas {currentPoint.lat.toFixed(5)}, {currentPoint.lng.toFixed(5)}
) : (

Sin datos

)}
{/* Trip summary */}

Resumen del viaje

Distancia {viaje.distancia?.toFixed(1) || 0} km
Duracion {viaje.duracion || 0} min
Vel. promedio {viaje.velocidadPromedio?.toFixed(0) || 0} km/h
Vel. maxima {viaje.velocidadMaxima?.toFixed(0) || 0} km/h
{/* Playback controls */}
{/* Play controls */}
{/* Speed */} {/* Timeline */}
{currentIndex + 1} / {puntos.length}
) }