import { useEffect, useRef, useState } from "react"; import { api } from "../api"; import { fmt } from "../utils"; import { Badge } from "./Shared"; // ─── DownloadProgress ──────────────────────────────────────────────────────── export function DownloadProgress({ jobId, onDone }: { jobId: string; onDone?: () => void }) { const [job, setJob] = useState(null); const doneRef = useRef(false); const pollInProgressRef = useRef(false); useEffect(() => { let iv: number | undefined; const poll = async () => { // Prevent concurrent requests if (pollInProgressRef.current) return; pollInProgressRef.current = true; try { const data = await api.getDownloadJob(jobId); setJob(data.job); if (data.job.status !== "running") { if (iv) window.clearInterval(iv); if (!doneRef.current) { doneRef.current = true; onDone?.(); } } } catch { if (iv) window.clearInterval(iv); } finally { pollInProgressRef.current = false; } }; poll(); // Increased polling interval from 1000ms to 2000ms to reduce server load iv = window.setInterval(poll, 2000); return () => { if (iv) window.clearInterval(iv); }; }, [jobId, onDone]); if (!job) return null; const pct = job.totalBytes > 0 ? Math.min(100, (job.bytesDownloaded / job.totalBytes) * 100) : null; return (

{job.filename || "Downloading…"}

{job.url}

{job.status}
{pct == null ? (
) : (
)}
{fmt(job.bytesDownloaded)}{job.totalBytes > 0 ? ` / ${fmt(job.totalBytes)}` : ""} {pct == null ? "…" : `${pct.toFixed(1)}%`}
{job.error &&

{job.error}

} {job.status === "running" && (
)}
); } // ─── RemoteDownloadProgress ────────────────────────────────────────────────── export function RemoteDownloadProgress({ jobId, onDone }: { jobId: string; onDone?: () => void }) { const [job, setJob] = useState(null); const doneRef = useRef(false); const pollInProgressRef = useRef(false); useEffect(() => { let iv: number | undefined; const poll = async () => { // Prevent concurrent requests if (pollInProgressRef.current) return; pollInProgressRef.current = true; try { const data = await api.getRemoteDownloadJob(jobId); setJob(data.job); if (data.job.status !== "running") { if (iv) window.clearInterval(iv); if (!doneRef.current) { doneRef.current = true; onDone?.(); } } } catch { if (iv) window.clearInterval(iv); } finally { pollInProgressRef.current = false; } }; poll(); // Increased polling interval from 1000ms to 2000ms to reduce server load iv = window.setInterval(poll, 2000); return () => { if (iv) window.clearInterval(iv); }; }, [jobId, onDone]); if (!job) return null; const pct = job.totalBytes > 0 ? Math.min(100, (job.bytesDownloaded / job.totalBytes) * 100) : null; return (

{job.filename || "Downloading…"}

{job.url}

{job.status}
{pct == null ? (
) : (
)}
{fmt(job.bytesDownloaded)}{job.totalBytes > 0 ? ` / ${fmt(job.totalBytes)}` : ""} {pct == null ? "…" : `${pct.toFixed(1)}%`}
{job.error &&

{job.error}

} {job.status === "running" && (
)}
); } // ─── UploadProgress ────────────────────────────────────────────────────────── export function UploadProgress({ progress, onClose, }: { progress: { bytesUploaded: number; totalBytes: number; fileCount: number; startTime: number } | null; onClose: () => void; }) { if (!progress) return null; const pct = progress.totalBytes > 0 ? Math.min(100, (progress.bytesUploaded / progress.totalBytes) * 100) : null; const elapsed = (Date.now() - progress.startTime) / 1000; const speed = elapsed > 0 ? progress.bytesUploaded / elapsed : 0; return (

Uploading {progress.fileCount} file{progress.fileCount !== 1 ? "s" : ""}…

{pct == null ? (
) : (
)}
{fmt(progress.bytesUploaded)}{progress.totalBytes > 0 ? ` / ${fmt(progress.totalBytes)}` : ""} {pct == null ? "…" : `${pct.toFixed(1)}%`} · {fmt(speed)}/s
); } // ─── PixeldrainProgress ────────────────────────────────────────────────────── import type { PixeldrainJob } from "../types"; export function PixeldrainProgress({ jobId, onClose }: { jobId: string; onClose: () => void }) { const [job, setJob] = useState(null); const [copied, setCopied] = useState(false); useEffect(() => { const poll = async () => { try { const r = await fetch(`/api/pixeldrain-jobs/${jobId}`); const data = await r.json(); setJob(data.job); if (data.job.status !== "running") clearInterval(iv); } catch { /* ignore */ } }; poll(); const iv = setInterval(poll, 800); return () => clearInterval(iv); }, [jobId]); const copyUrl = () => { if (!job?.pixeldrainUrl) return; navigator.clipboard.writeText(job.pixeldrainUrl); setCopied(true); setTimeout(() => setCopied(false), 2000); }; if (!job) return null; const percent = job.totalBytes > 0 ? Math.min(100, (job.bytesUploaded / job.totalBytes) * 100) : null; return (

☁️ {job.filename}

Uploading to Pixeldrain…

{job.status}
{percent !== null ? (
) : (
)}
{fmt(job.bytesUploaded)}{job.totalBytes > 0 ? ` / ${fmt(job.totalBytes)}` : ""} {percent !== null && {percent.toFixed(1)}%}
{job.status === "done" && job.pixeldrainUrl && (
{job.pixeldrainUrl}
)} {job.status === "failed" && (

{job.error || "Upload failed"}

)}
); }