From 139a4b63fa7ac5f419ea48cee6187f61aea8be6c Mon Sep 17 00:00:00 2001 From: Arkaprabha Chakraborty Date: Fri, 9 Aug 2024 11:43:59 +0530 Subject: [PATCH] Feature: hide download button when downloading Refactor: move the Download button below the progress bar --- frontend/src/components/DownloadForm.js | 84 ++++++++++++++++--------- 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/frontend/src/components/DownloadForm.js b/frontend/src/components/DownloadForm.js index bffa6f9..21025e0 100644 --- a/frontend/src/components/DownloadForm.js +++ b/frontend/src/components/DownloadForm.js @@ -5,15 +5,30 @@ import Confetti from "react-confetti"; const DownloadForm = () => { const [url, setUrl] = useState(""); - const [quality, setQuality] = useState("720p"); + const [quality, setQuality] = useState("480p"); const [message, setMessage] = useState(""); const [progress, setProgress] = useState(0); const [isProcessing, setIsProcessing] = useState(false); const [showConfetti, setShowConfetti] = useState(false); const confettiRef = useRef(null); + // Validate if the URL is a YouTube domain + const isValidYouTubeUrl = (url) => { + const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/.+$/; + return youtubeRegex.test(url); + }; + const handleDownload = async (e) => { e.preventDefault(); + + if (!url) { + setMessage("maybe enter an URL first"); + return; + } else if (!isValidYouTubeUrl(url)) { + setMessage("doesn't look like YouTube to me"); + return; + } + setMessage("Processing..."); setProgress(0); setIsProcessing(true); @@ -28,6 +43,7 @@ const DownloadForm = () => { const total = progressEvent.total; const current = progressEvent.loaded; setProgress(Math.round((current / total) * 100)); + setMessage("Downloading..."); }, }, ); @@ -42,19 +58,19 @@ const DownloadForm = () => { link.download = filename; link.click(); - setMessage("Download complete"); + setMessage("Download Complete"); setIsProcessing(false); setShowConfetti(true); } catch (error) { - setMessage("Download failed"); + 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"; + 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 }; @@ -109,35 +125,47 @@ const DownloadForm = () => { onChange={(e) => setQuality(e.target.value)} className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" > + - -
- +
+
+ {isProcessing && progress === 0 && ( +
+ )} +
+ + {message} + +
+
+
+ {!isProcessing && !message.startsWith("Downloading") && ( + + )}
-
-
- {isProcessing && progress === 0 && ( -
- )} -
+ {showConfetti && (