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 { formatBytes, 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; } 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); })()}
); }