import { CheckCircle, Download, Loader2, Package, XCircle } from "lucide-react";
import { useDownloadStore } from "@/stores/download-store";
function formatBytes(bytes: number): string {
if (bytes === 0) return "0 B";
const units = ["B", "KB", "MB", "GB", "TB", "PB"];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const value = bytes / 1024 ** i;
return `${value.toFixed(value < 10 ? 1 : 0)} ${units[i]}`;
}
function shortenFileName(path: string): string {
const parts = path.replace(/\\/g, "/").split("/");
return parts[parts.length - 1] || path;
}
/**
* Inline progress display for use inside dialogs or pages.
* Reads from the global download store.
*/
export function DownloadProgress() {
const {
phase,
totalFiles,
completedFiles,
currentFile,
currentFileStatus,
currentFileDownloaded,
currentFileTotal,
totalDownloadedBytes,
errorMessage,
phaseLabel,
} = useDownloadStore();
if (phase === "idle") return null;
const overallPercent =
totalFiles > 0 ? Math.round((completedFiles / totalFiles) * 100) : 0;
const filePercent =
currentFileTotal > 0
? Math.round((currentFileDownloaded / currentFileTotal) * 100)
: 0;
return (
{/* Phase header */}
{phase === "preparing" && (
)}
{phase === "downloading" && (
)}
{phase === "finalizing" && (
)}
{phase === "installing-mod-loader" && (
)}
{phase === "completed" && (
)}
{phase === "error" && (
)}
{phaseLabel}
{/* Preparing phase — no file counts yet */}
{phase === "preparing" && (
Resolving version and assets...
)}
{/* Overall progress */}
{phase === "downloading" && totalFiles > 0 && (
{/* Overall bar */}
Overall: {completedFiles} / {totalFiles} files
{overallPercent}%
{formatBytes(totalDownloadedBytes)} downloaded
{/* Current file — always reserve space to avoid layout shifts */}
{currentFile && currentFileStatus !== "Finished" && (
{shortenFileName(currentFile)}
{currentFileStatus === "Downloading"
? `${filePercent}%`
: currentFileStatus}
{currentFileStatus === "Downloading" &&
currentFileTotal > 0 && (
)}
)}
)}
{/* Finalizing phase — downloads done, waiting for install to finish */}
{phase === "finalizing" && (
Verifying installation...
)}
{/* Mod loader install phase */}
{phase === "installing-mod-loader" && (
{phaseLabel}
)}
{/* Error */}
{phase === "error" && errorMessage && (
{errorMessage}
)}
{/* Completed */}
{phase === "completed" && (
Successfully installed {totalFiles > 0 ? `${totalFiles} files` : ""}
)}
);
}