import { FileCheck, FileWarning, Loader2, Server, HardDrive } from "lucide-react"; import { Button } from "../ui/Button"; import { Card, CardContent } from "../ui/Card"; import { Progress } from "../ui/ProgressBar"; import { formatBytes, formatSpeed } from "../../lib/utils"; const API_URL = import.meta.env.VITE_API_URL || "http://localhost:3000"; export type DownloadStatus = "queued" | "downloading" | "completed" | "error"; export type DownloadItemData = { id: string; sessionId: string; url: string; filename: string; status: DownloadStatus; progress: number; totalBytes: number; receivedBytes: number; speed: number; error?: string; createdAt: number; }; interface DownloadItemProps { item: DownloadItemData; } export function DownloadItem({ item }: DownloadItemProps) { const isCompleted = item.status === "completed"; const isError = item.status === "error"; const isQueued = item.status === "queued"; const handleDownloadFile = () => { const download = async () => { try { const sessionId = localStorage.getItem("downloader_session_id") || ""; const res = await fetch(`${API_URL}/api/download/${item.id}`, { headers: { "x-session-id": sessionId } }); if (!res.ok) throw new Error("Download failed"); const blob = await res.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = item.filename; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); } catch (e) { alert("Failed to download file"); } }; download(); }; return (
{isCompleted ? (
) : isError ? (
) : isQueued ? (
) : (
)}

{item.filename || item.url}

{item.url}

{isCompleted && ( )}
{/* Progress Section */} {!isCompleted && !isError && (
{isQueued ? "Queued on server..." : item.status} {formatBytes(item.receivedBytes || 0)} / {item.totalBytes ? formatBytes(item.totalBytes) : "??"}
{item.speed ? formatSpeed(item.speed) : "0 B/s"} {item.progress ? `${Math.round(item.progress)}%` : "0%"}
)} {isCompleted && (
Ready for download ({formatBytes(item.totalBytes)})
)} {isError && (
{item.error || "Unknown error occurred"}
)}
); }