import type { Download } from "../types"; import * as api from "../api"; /** * What is being downloaded, and what just was. * * WebKit saves the file on its own and says nothing about it, so without this * a download is indistinguishable from a click that did nothing. */ export default function Downloads({ items, onDismiss, }: { items: Download[]; onDismiss: (id: number) => void; }) { if (items.length === 0) return null; return (
{items.map((d) => (
{d.state === "done" ? ( ) : d.state === "failed" ? ( ) : ( /* No total to divide by — the webview reports a start and a finish and nothing between — so this spins rather than fills. */ )} {d.name} {d.state === "done" ? "Downloaded" : d.state === "failed" ? "Download failed" : d.bytes > 0 ? `${formatBytes(d.bytes)} so far` : "Starting…"} {d.state === "done" && ( )}
))}
); } function formatBytes(n: number): string { if (n < 1024) return `${n} B`; if (n < 1024 * 1024) return `${(n / 1024).toFixed(0)} KB`; return `${(n / (1024 * 1024)).toFixed(1)} MB`; }