diff --git a/frontend/src/pages/VideoPlayer.tsx b/frontend/src/pages/VideoPlayer.tsx index 00bfb2c..c73f45b 100644 --- a/frontend/src/pages/VideoPlayer.tsx +++ b/frontend/src/pages/VideoPlayer.tsx @@ -3,12 +3,14 @@ import {useParams} from "react-router-dom"; import type {Clip} from "../utils/types"; import {getClipById} from "../utils/endpoints.ts"; import Box from "../components/Box.tsx" +import {dateToTimeAgo, stringToDate} from "../utils/utils.ts"; const VideoPlayer = () => { const { id } = useParams(); - const [videoUrl, setVideoUrl] = useState(""); + const [videoUrl, setVideoUrl] = useState(undefined); const [error, setError] = useState(null); const [clip, setClip] = useState(null); + const [timeAgo, setTimeAgo] = useState(""); useEffect(() => { // Fetch the video URL from the server @@ -33,14 +35,34 @@ const VideoPlayer = () => { return; } - getClipById(id).then((fetchedClip) => {setClip(fetchedClip)}) + 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]); + // Update timeAgo live every second + useEffect(() => { + if (!clip || !clip.createdAt) return; + + const update = () => { + const date = stringToDate(clip.createdAt); + setTimeAgo(dateToTimeAgo(date)); + }; + + update(); // initial update + const interval = setInterval(update, 1000); + + return () => clearInterval(interval); // cleanup + }, [clip]); + return ( -
+