import React, { useState, useRef, useEffect } from "react"; import axios from "axios"; import { v4 as uuidv4 } from "uuid"; import Confetti from "react-confetti"; const DownloadForm = () => { const [url, setUrl] = useState(""); const [quality, setQuality] = useState("720p"); const [message, setMessage] = useState(""); const [progress, setProgress] = useState(0); const [isProcessing, setIsProcessing] = useState(false); const [showConfetti, setShowConfetti] = useState(false); const confettiRef = useRef(null); const handleDownload = async (e) => { e.preventDefault(); setMessage("Processing..."); setProgress(0); setIsProcessing(true); try { const response = await axios.post( `${process.env.REACT_APP_BACKEND_URL}/downlink/download`, { url, quality }, { responseType: "blob", onDownloadProgress: (progressEvent) => { const total = progressEvent.total; const current = progressEvent.loaded; setProgress(Math.round((current / total) * 100)); }, }, ); const disposition = response.headers["content-disposition"]; const filename = disposition ? disposition.split("filename=")[1].replace(/"/g, "") : `${uuidv4()}.mp4`; const link = document.createElement("a"); link.href = window.URL.createObjectURL(new Blob([response.data])); link.download = filename; link.click(); setMessage("Download complete"); setIsProcessing(false); setShowConfetti(true); } catch (error) { setMessage("Download failed"); console.error(error); setIsProcessing(false); } }; const getBarClass = () => { if (message === "Download complete") return "bg-green-500"; if (message === "Download failed") return "bg-red-500"; return "bg-blue-500"; // Default color when not processing }; const getBarStyle = () => ({ transition: "width 0.5s ease-in-out", }); const getAnimationStyle = () => { // Hide the loading animation when progress is greater than 0 return progress === 0 ? { animation: "loading 1.5s infinite" } : {}; }; useEffect(() => { if (showConfetti) { const timer = setTimeout(() => { setShowConfetti(false); }, 5000); return () => clearTimeout(timer); } }, [showConfetti]); return (