DownloadButton.tsx
⎇
Raw
1import "./DownloadButton.css";
2import type { AudioFile, Directory } from "music-server-shared/types";
3import { download, signalMap, type SignalMapEntry } from "../offline";
4import { createSignal, type Accessor, type Signal } from "solid-js";
5import type { AppOptions } from "../types";
6import { formatBytes, joinPath } from "../utils";
7import { DownloadSVG, RefreshSVG } from "./SVG";
8import { LoadingSpinner } from "./LoadingScreen";
9
10interface DownloadButtonProps {
11 item: AudioFile | Directory;
12 currentDir: Accessor<string>;
13 options: Accessor<AppOptions>;
14}
15
16export function DownloadButton(props: DownloadButtonProps) {
17 const fullPath = joinPath(props.currentDir(), props.item.name);
18 let signal = signalMap.get(fullPath);
19 if (!signal) {
20 signal = createSignal<SignalMapEntry>();
21 signalMap.set(fullPath, signal);
22 }
23 const [getProgress] = signal;
24
25 return (
26 <div
27 class="download-item"
28 onClick={(event) => {
29 event.stopPropagation();
30 download(props.item, props.currentDir(), props.options());
31 }}
32 >
33 {(() => {
34 const progress = getProgress();
35 if (progress === undefined)
36 return (
37 <button>
38 <DownloadSVG />
39 </button>
40 );
41 if (progress === "DirectoryProgress") return <LoadingSpinner center={true} />;
42 if (progress === "CachedFile")
43 return (
44 <button>
45 <RefreshSVG />
46 </button>
47 );
48 if ("bytes" in progress) return <span>{formatBytes(progress.bytes)}</span>;
49 console.log(progress);
50 console.error("Unexpected", progress);
51 })()}
52 </div>
53 );
54}
55