import { useEffect, useRef, useState } from "react"; import { useNavigate, useParams } from "react-router-dom"; import api, { getStreamUrl } from "../lib/api"; import { ArrowLeft } from "lucide-react"; export default function Player() { const { id } = useParams(); const nav = useNavigate(); const [movie, setMovie] = useState(null); const videoRef = useRef(null); const lastSent = useRef(0); useEffect(() => { let cancelled = false; (async () => { const { data } = await api.get(`/movies/${id}`); if (cancelled) return; setMovie(data); // Restore progress try { const { data: p } = await api.get(`/progress/${id}`); if (videoRef.current && p?.position_seconds) { videoRef.current.currentTime = p.position_seconds; } } catch {} })(); return () => { cancelled = true; }; }, [id]); const onTimeUpdate = () => { const v = videoRef.current; if (!v || !v.duration) return; const now = Date.now(); if (now - lastSent.current < 5000) return; lastSent.current = now; api.post("/progress", { movie_id: id, position_seconds: v.currentTime, duration_seconds: v.duration, }).catch(() => {}); }; if (!movie) { return (
Loading…
); } return (

{movie.title}

{movie.year} · {movie.rating}

); }