import "./DownloadButton.css"; import type { MediaFile, Directory, FileListing } from "music-server-shared/types"; import { deleteFile, download, type DownloadStatus, getDownloadStatus, getDownloadStatusWithChildren, type StatusWithChildren, } from "../offline"; import { createMemo, type Accessor, type ResourceActions, type Signal } from "solid-js"; import type { AppOptions } from "../types"; import { formatBytes, joinPath } from "../utils"; import { DeleteSVG, RefreshSVG, SyncSVG } from "./SVG"; import { LoadingSpinner } from "./LoadingScreen"; import type { FlatFileListing } from "../App"; interface DownloadButtonProps { fileName: string; item: MediaFile | Directory; fetchFiles: ResourceActions; isOffline: Accessor; currentDir: Accessor; options: AppOptions; } export function DownloadButton(props: DownloadButtonProps) { const fullPath = joinPath(props.currentDir(), props.fileName); const containsProgressChildren = createMemo(() => { const status = getDownloadStatusWithChildren(fullPath); if (!status.children) return false; function inProgress(signal: Signal) { const status = signal[0](); return status === "UnspecificProgress" || (typeof status === "object" && "bytes" in status); } function recurse(statusWithChildren: StatusWithChildren) { for (const [, child] of statusWithChildren.children || []) { if (inProgress(child.status) || recurse(child)) return true; } return false; } return recurse(status); }); return (
{ event.stopPropagation(); if (!props.isOffline()) download(props.fileName, props.item, props.currentDir(), props.currentDir, props.options, props.fetchFiles); else deleteFile(props.fileName, props.item, props.currentDir(), props.fetchFiles); }} > {(() => { const [getStatus] = getDownloadStatus(fullPath); const progress = getStatus(); if (progress === "UnspecificProgress" || containsProgressChildren()) return ; if (props.isOffline()) { return ( ); } if (progress === undefined) { return ( ); } if (progress === "Synced") { return ( ); } if ("bytes" in progress) return {progress.bytes === 0 ? "Queued..." : formatBytes(progress.bytes)}; console.error("Unexpected", progress); })()}
); }