import "./DownloadButton.css"; import type { AudioFile, Directory, FileListing } from "music-server-shared/types"; import { deleteFile, download, signalMap, type SignalMapEntry } from "../offline"; import { createSignal, type Accessor, type ResourceActions } from "solid-js"; import type { AppOptions } from "../types"; import { joinPath } from "../utils"; import { DeleteSVG, DownloadSVG, RefreshSVG } from "./SVG"; import { LoadingSpinner } from "./LoadingScreen"; interface DownloadButtonProps { item: AudioFile | Directory; fetchFiles: ResourceActions; isOffline: Accessor; currentDir: Accessor; options: Accessor; } function formatBytes(bytes: number): string { if (bytes === 0) return "Queued..."; const k = 1024; const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); const value = bytes / k ** i; return `${value.toFixed(2)} ${sizes[i]}`; } export function DownloadButton(props: DownloadButtonProps) { const fullPath = joinPath(props.currentDir(), props.item.name); let signal = signalMap.get(fullPath); if (!signal) { signal = createSignal(); signalMap.set(fullPath, signal); } const [getProgress] = signal; return (
{ event.stopPropagation(); if (!props.isOffline()) download(props.item, props.currentDir(), props.options(), props.fetchFiles); else deleteFile(props.item, props.currentDir(), props.fetchFiles); }} > {(() => { const progress = getProgress(); if (progress === undefined) { if (props.isOffline()) { //only happens if this is a directory return ( ); } return ( ); } if (progress === "DirectoryProgress") return ; if (progress === "CachedFile") { if (props.isOffline()) { return ( ); } return ( ); } if ("bytes" in progress) return {formatBytes(progress.bytes)}; console.log(progress); console.error("Unexpected", progress); })()}
); }