Feature: hide download button when downloading

Refactor: move the Download button below the progress bar
This commit is contained in:
Arkaprabha Chakraborty
2024-08-09 11:43:59 +05:30
parent f13d334382
commit 139a4b63fa

View File

@@ -5,15 +5,30 @@ import Confetti from "react-confetti";
const DownloadForm = () => { const DownloadForm = () => {
const [url, setUrl] = useState(""); const [url, setUrl] = useState("");
const [quality, setQuality] = useState("720p"); const [quality, setQuality] = useState("480p");
const [message, setMessage] = useState(""); const [message, setMessage] = useState("");
const [progress, setProgress] = useState(0); const [progress, setProgress] = useState(0);
const [isProcessing, setIsProcessing] = useState(false); const [isProcessing, setIsProcessing] = useState(false);
const [showConfetti, setShowConfetti] = useState(false); const [showConfetti, setShowConfetti] = useState(false);
const confettiRef = useRef(null); 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) => { const handleDownload = async (e) => {
e.preventDefault(); 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..."); setMessage("Processing...");
setProgress(0); setProgress(0);
setIsProcessing(true); setIsProcessing(true);
@@ -28,6 +43,7 @@ const DownloadForm = () => {
const total = progressEvent.total; const total = progressEvent.total;
const current = progressEvent.loaded; const current = progressEvent.loaded;
setProgress(Math.round((current / total) * 100)); setProgress(Math.round((current / total) * 100));
setMessage("Downloading...");
}, },
}, },
); );
@@ -42,19 +58,19 @@ const DownloadForm = () => {
link.download = filename; link.download = filename;
link.click(); link.click();
setMessage("Download complete"); setMessage("Download Complete");
setIsProcessing(false); setIsProcessing(false);
setShowConfetti(true); setShowConfetti(true);
} catch (error) { } catch (error) {
setMessage("Download failed"); setMessage("Download Failed");
console.error(error); console.error(error);
setIsProcessing(false); setIsProcessing(false);
} }
}; };
const getBarClass = () => { const getBarClass = () => {
if (message === "Download complete") return "bg-green-500"; if (message === "Download Complete") return "bg-green-500";
if (message === "Download failed") return "bg-red-500"; if (message === "Download Failed") return "bg-red-500";
return "bg-blue-500"; // Default color when not processing return "bg-blue-500"; // Default color when not processing
}; };
@@ -109,23 +125,14 @@ const DownloadForm = () => {
onChange={(e) => setQuality(e.target.value)} 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" 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"
> >
<option value="360p">360p</option>
<option value="480p">480p</option> <option value="480p">480p</option>
<option value="720p">720p</option> <option value="720p">720p</option>
<option value="1080p">1080p</option>
</select> </select>
</div> </div>
<div> <div className="w-full bg-white rounded-full mt-4 h-4 relative overflow-hidden">
<button
type="submit"
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-black hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
Download
</button>
</div>
</form>
<div className="w-full bg-white rounded-full mt-4 h-3 relative overflow-hidden">
<div <div
className={`h-3 ${getBarClass()} rounded-full`} className={`h-4 ${getBarClass()} rounded-full`}
style={{ style={{
width: `${progress}%`, width: `${progress}%`,
...getBarStyle(), ...getBarStyle(),
@@ -133,11 +140,32 @@ const DownloadForm = () => {
></div> ></div>
{isProcessing && progress === 0 && ( {isProcessing && progress === 0 && (
<div <div
className={`absolute top-0 left-0 w-full h-full bg-blue-300 rounded-full`} className={`absolute top-0 left-0 w-full h-full bg-blue-500 rounded-full`}
style={getAnimationStyle()} style={getAnimationStyle()}
></div> ></div>
)} )}
<div className="absolute inset-0 flex items-center justify-center">
<span
className={`text-xs ${
message === "Download Failed" ? "text-white" : "text-black"
}`}
>
{message}
</span>
</div> </div>
</div>
<div className="flex justify-center">
{!isProcessing && !message.startsWith("Downloading") && (
<button
type="submit"
className="inline-flex items-center px-8 py-4 border border-transparent text-lg font-bold rounded-lg shadow-lg text-white bg-black hover:bg-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
Download
</button>
)}
</div>
</form>
{showConfetti && ( {showConfetti && (
<div className="fixed top-0 left-0 w-full h-full z-50"> <div className="fixed top-0 left-0 w-full h-full z-50">
<Confetti <Confetti