refactor(frontend): move media blob fetch function to endpoint file

This commit is contained in:
2026-02-22 21:38:08 +00:00
parent 0c5b93571b
commit ab943be44e
2 changed files with 23 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
import {useEffect, useState} from "react"; import {useEffect, useState} from "react";
import {useParams} from "react-router-dom"; import {useParams} from "react-router-dom";
import type {Clip} from "../utils/types"; import type {Clip} from "../utils/types";
import {getClipById} from "../utils/endpoints.ts"; import {getClipById, getVideoBlob } from "../utils/endpoints.ts";
import Box from "../components/Box.tsx" import Box from "../components/Box.tsx"
import {dateToTimeAgo, stringToDate} from "../utils/utils.ts"; import {dateToTimeAgo, stringToDate} from "../utils/utils.ts";
@@ -13,28 +13,18 @@ const VideoPlayer = () => {
const [timeAgo, setTimeAgo] = useState<String>(""); const [timeAgo, setTimeAgo] = useState<String>("");
useEffect(() => { useEffect(() => {
// Fetch the video URL from the server
fetch(`/api/v1/download/clip/${id}`)
.then(response => {
if (!response.ok) {
throw new Error("Failed to load video");
}
return response.blob();
})
.then(blob => {
const url = URL.createObjectURL(blob);
setVideoUrl(url);
})
.catch(err => {
console.error("Error fetching video:", err);
setError("Failed to load video. Please try again later.");
});
if (!id) { if (!id) {
setError("Clip ID is required."); setError("Clip ID is required.");
return; return;
} }
getVideoBlob(id)
.then((blob) => {
const url = URL.createObjectURL(blob);
setVideoUrl(url);
})
getClipById(id) getClipById(id)
.then((fetchedClip) => {setClip(fetchedClip)}) .then((fetchedClip) => {setClip(fetchedClip)})
.catch((err) => { .catch((err) => {

View File

@@ -239,6 +239,20 @@ const getClipById = async (id: string): Promise<Clip | null> => {
} }
}; };
const getVideoBlob = async(id: string): Promise<Blob> => {
const response = await fetch(API_URL + `/api/v1/download/clip/${id}`, {credentials: "include",});
if (!response.ok) {
throw new Error(`Failed to fetch video: ${id}: ${response.status}`)
}
try {
return response.blob();
} catch {
throw new Error(`Failed to convert Clip Return Object to blob`);
}
}
const isThumbnailAvailable = async (id: number): Promise<boolean> => { const isThumbnailAvailable = async (id: number): Promise<boolean> => {
const response = await fetch(API_URL + `/api/v1/download/thumbnail/${id}`, {credentials: "include"}); const response = await fetch(API_URL + `/api/v1/download/thumbnail/${id}`, {credentials: "include"});
if (!response.ok) { if (!response.ok) {
@@ -260,5 +274,6 @@ export {
getUser, getUser,
getClips, getClips,
getClipById, getClipById,
getVideoBlob,
isThumbnailAvailable isThumbnailAvailable
}; };