import {useEffect, useState} from "react"; import {useNavigate, useParams} from "react-router-dom"; import { ArrowLeft } from "lucide-react"; import type {Clip} from "../utils/types"; import {getClipById, getVideoBlob} from "../utils/endpoints.ts"; import Box from "../components/Box.tsx" import {dateToTimeAgo, stringToDate} from "../utils/utils.ts"; const VideoPlayer = () => { const { id } = useParams(); const navigate = useNavigate(); const [videoUrl, setVideoUrl] = useState(undefined); const [error, setError] = useState(null); const [clip, setClip] = useState(null); const [timeAgo, setTimeAgo] = useState(""); useEffect(() => { if (!id) { setError("Clip ID is required."); return; } getVideoBlob(id) .then((blob) => { const url = URL.createObjectURL(blob); setVideoUrl(url); }) getClipById(id) .then((fetchedClip) => {setClip(fetchedClip)}) .catch((err) => { console.error("Error fetching clip:", err); setError("Failed to load clip details. Please try again later."); }); }, [id]); useEffect(() => { if (!clip || !clip.createdAt) return; const update = () => { const date = stringToDate(clip.createdAt); setTimeAgo(dateToTimeAgo(date)); }; update(); const interval = setInterval(update, 1000); return () => clearInterval(interval); }, [clip]); return (
{error &&
{error}
} {!videoUrl && !error &&
Loading video...
}

{clip?.title || "(No Title)"}

{timeAgo}

); }; export default VideoPlayer;