import { useEffect, useMemo, useState } from "react"; import { fetchInfo, downloadImmediate, FORMAT_EXTS, type InfoResponse, parseContentDispositionFilename, } from "../../api/apps/Downloader"; export default function Downloader() { const [url, setUrl] = useState(""); const [probing, setProbing] = useState(false); const [downloading, setDownloading] = useState(false); const [error, setError] = useState(null); const [info, setInfo] = useState(null); const [ext, setExt] = useState("mp4"); const [videoRes, setVideoRes] = useState(undefined); const [audioRes, setAudioRes] = useState(undefined); useEffect(() => { if (info?.video_resolutions?.length && !videoRes) { setVideoRes(info.video_resolutions[0]); } if (info?.audio_resolutions?.length && !audioRes) { setAudioRes(info.audio_resolutions[0]); } }, [info]); async function onProbe(e: React.FormEvent) { e.preventDefault(); setError(null); setInfo(null); setProbing(true); try { const res = await fetchInfo(url); setInfo(res); // reset selections from fresh info setVideoRes(res.video_resolutions?.[0]); setAudioRes(res.audio_resolutions?.[0]); } catch (e: any) { setError( e?.response?.data?.error || e?.response?.data?.detail || e?.message || "Failed to get info." ); } finally { setProbing(false); } } async function onDownload() { setError(null); setDownloading(true); try { const { blob, filename } = await downloadImmediate({ url, ext, videoResolution: videoRes, audioResolution: audioRes, }); const name = filename || parseContentDispositionFilename("") || `download.${ext}`; const href = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = href; a.download = name; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(href); } catch (e: any) { setError( e?.response?.data?.error || e?.response?.data?.detail || e?.message || "Download failed." ); } finally { setDownloading(false); } } const canDownload = useMemo( () => !!url && !!ext && !!videoRes && !!audioRes, [url, ext, videoRes, audioRes] ); return (

Downloader

{error &&
{error}
}
{info && (
{info.thumbnail && ( {info.title )}
Title: {info.title || "-"}
Duration:{" "} {info.duration ? `${Math.round(info.duration)} s` : "-"}
)}
); }