vi-2 / src /components /Progress.tsx
omarod2in's picture
try-fix-the-lag
0d3e998 verified
Raw History Blame Contribute Delete
12.7 kB
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<import("../types").DownloadJob | null>(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 (
<div className="rounded-2xl border border-slate-200 bg-white/90 p-4 shadow-sm backdrop-blur">
<div className="mb-2 flex items-center justify-between gap-3">
<div className="min-w-0">
<p className="truncate text-sm font-semibold text-slate-800">{job.filename || "Downloading…"}</p>
<p className="truncate text-xs text-slate-500">{job.url}</p>
</div>
<Badge color={
job.status === "done"
? "bg-emerald-100 text-emerald-700"
: job.status === "failed"
? "bg-red-100 text-red-700"
: job.status === "stopped"
? "bg-slate-200 text-slate-700"
: "bg-violet-100 text-violet-700"
}>
{job.status}
</Badge>
</div>
<div className="mb-2 h-2 overflow-hidden rounded-full bg-slate-100">
{pct == null ? (
<div className="h-full w-1/3 animate-pulse rounded-full bg-violet-500" />
) : (
<div className="h-full rounded-full bg-violet-500 transition-all" style={{ width: `${pct}%` }} />
)}
</div>
<div className="flex items-center justify-between text-xs text-slate-500">
<span>{fmt(job.bytesDownloaded)}{job.totalBytes > 0 ? ` / ${fmt(job.totalBytes)}` : ""}</span>
<span>{pct == null ? "…" : `${pct.toFixed(1)}%`}</span>
</div>
{job.error && <p className="mt-2 text-xs text-red-500">{job.error}</p>}
{job.status === "running" && (
<div className="mt-3 flex justify-end">
<button
onClick={() => api.stopDownloadJob(job.id)}
className="rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-50"
>
Stop
</button>
</div>
)}
</div>
);
}
// ─── RemoteDownloadProgress ──────────────────────────────────────────────────
export function RemoteDownloadProgress({ jobId, onDone }: { jobId: string; onDone?: () => void }) {
const [job, setJob] = useState<import("../types").DownloadJob | null>(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 (
<div className="rounded-2xl border border-slate-200 bg-white/90 p-4 shadow-sm backdrop-blur">
<div className="mb-2 flex items-center justify-between gap-3">
<div className="min-w-0">
<p className="truncate text-sm font-semibold text-slate-800">{job.filename || "Downloading…"}</p>
<p className="truncate text-xs text-slate-500">{job.url}</p>
</div>
<Badge color={
job.status === "done"
? "bg-emerald-100 text-emerald-700"
: job.status === "failed"
? "bg-red-100 text-red-700"
: job.status === "stopped"
? "bg-slate-200 text-slate-700"
: "bg-violet-100 text-violet-700"
}>
{job.status}
</Badge>
</div>
<div className="mb-2 h-2 overflow-hidden rounded-full bg-slate-100">
{pct == null ? (
<div className="h-full w-1/3 animate-pulse rounded-full bg-violet-500" />
) : (
<div className="h-full rounded-full bg-violet-500 transition-all" style={{ width: `${pct}%` }} />
)}
</div>
<div className="flex items-center justify-between text-xs text-slate-500">
<span>{fmt(job.bytesDownloaded)}{job.totalBytes > 0 ? ` / ${fmt(job.totalBytes)}` : ""}</span>
<span>{pct == null ? "…" : `${pct.toFixed(1)}%`}</span>
</div>
{job.error && <p className="mt-2 text-xs text-red-500">{job.error}</p>}
{job.status === "running" && (
<div className="mt-3 flex justify-end">
<button
onClick={() => api.stopRemoteDownloadJob(job.id)}
className="rounded-lg border border-slate-200 px-3 py-1.5 text-xs font-semibold text-slate-600 hover:bg-slate-50"
>
Stop
</button>
</div>
)}
</div>
);
}
// ─── 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 (
<div className="rounded-2xl border border-slate-200 bg-white/90 p-4 shadow-sm backdrop-blur">
<div className="mb-2 flex items-center justify-between gap-3">
<p className="text-sm font-semibold text-slate-800">
Uploading {progress.fileCount} file{progress.fileCount !== 1 ? "s" : ""}…
</p>
<button onClick={onClose} className="rounded p-1 text-slate-400 hover:text-slate-600 hover:bg-slate-100">
<svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
</div>
<div className="mb-2 h-2 overflow-hidden rounded-full bg-slate-100">
{pct == null ? (
<div className="h-full w-1/3 animate-pulse rounded-full bg-violet-500" />
) : (
<div className="h-full rounded-full bg-violet-500 transition-all" style={{ width: `${pct}%` }} />
)}
</div>
<div className="flex items-center justify-between text-xs text-slate-500">
<span>{fmt(progress.bytesUploaded)}{progress.totalBytes > 0 ? ` / ${fmt(progress.totalBytes)}` : ""}</span>
<span>{pct == null ? "…" : `${pct.toFixed(1)}%`} Β· {fmt(speed)}/s</span>
</div>
</div>
);
}
// ─── PixeldrainProgress ──────────────────────────────────────────────────────
import type { PixeldrainJob } from "../types";
export function PixeldrainProgress({ jobId, onClose }: { jobId: string; onClose: () => void }) {
const [job, setJob] = useState<PixeldrainJob | null>(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 (
<div className="rounded-xl border border-slate-200 bg-white p-4 space-y-3">
<div className="flex items-start justify-between gap-2">
<div className="min-w-0 flex-1">
<p className="text-xs font-semibold text-slate-700 flex items-center gap-1.5">
<span>☁️</span>
<span className="truncate">{job.filename}</span>
</p>
<p className="text-[10px] text-slate-400 mt-0.5">Uploading to Pixeldrain…</p>
</div>
<div className="flex items-center gap-2 flex-shrink-0">
<span className={`text-xs font-semibold capitalize ${
job.status === "done" ? "text-emerald-600" :
job.status === "failed" ? "text-red-500" : "text-violet-600"
}`}>{job.status}</span>
<button onClick={onClose} className="rounded p-1 text-slate-400 hover:text-slate-600 hover:bg-slate-100">
<svg className="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
</div>
</div>
<div className="space-y-1">
<div className="relative h-2 w-full overflow-hidden rounded-full bg-slate-100">
{percent !== null ? (
<div className={`h-full rounded-full transition-all duration-300 ${
job.status === "done" ? "bg-emerald-500" :
job.status === "failed" ? "bg-red-400" : "bg-violet-500"
}`} style={{ width: `${percent}%` }} />
) : (
<div className="h-full w-1/3 rounded-full bg-violet-500 animate-[slide_1.2s_ease-in-out_infinite]" />
)}
</div>
<div className="flex justify-between text-[10px] text-slate-400">
<span>{fmt(job.bytesUploaded)}{job.totalBytes > 0 ? ` / ${fmt(job.totalBytes)}` : ""}</span>
{percent !== null && <span>{percent.toFixed(1)}%</span>}
</div>
</div>
{job.status === "done" && job.pixeldrainUrl && (
<div className="flex items-center gap-2 rounded-lg bg-emerald-50 border border-emerald-200 px-3 py-2">
<a href={job.pixeldrainUrl} target="_blank" rel="noreferrer"
className="flex-1 truncate text-xs font-medium text-emerald-700 hover:underline">
{job.pixeldrainUrl}
</a>
<button onClick={copyUrl}
className="flex-shrink-0 rounded-md bg-emerald-100 hover:bg-emerald-200 px-2 py-1 text-[10px] font-semibold text-emerald-700 transition-colors">
{copied ? "βœ“ Copied" : "Copy"}
</button>
</div>
)}
{job.status === "failed" && (
<p className="text-xs text-red-500">{job.error || "Upload failed"}</p>
)}
</div>
);
}