"use client"; import { createPortal } from "react-dom"; import { Download, Loader2, X } from "lucide-react"; import type { CanvasImageDownloadProgress } from "@/lib/client/canvas-image-download"; import { cn } from "@/lib/utils"; type CanvasImageDownloadProgressDialogProps = { progress: CanvasImageDownloadProgress | null; onCancel?: () => void; }; function formatBytes(bytes?: number) { if (!bytes || !Number.isFinite(bytes) || bytes <= 0) return ""; if (bytes < 1024 * 1024) return `${Math.max(1, Math.round(bytes / 1024))} KB`; return `${(bytes / 1024 / 1024).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0)} MB`; } export function CanvasImageDownloadProgressDialog({ onCancel, progress, }: CanvasImageDownloadProgressDialogProps) { if (!progress || typeof document === "undefined") return null; const percent = typeof progress.percent === "number" && Number.isFinite(progress.percent) ? Math.max(0, Math.min(100, Math.round(progress.percent))) : null; const loadedLabel = formatBytes(progress.loadedBytes); const totalLabel = formatBytes(progress.totalBytes); return createPortal(
{progress.phase === "saving" ? ( ) : ( )}
下载原图
{progress.message}
{onCancel ? ( ) : null}
{loadedLabel && totalLabel ? `${loadedLabel} / ${totalLabel}` : "正在处理文件"} {percent !== null ? `${percent}%` : "..."}
, document.body ); }