DownloadButton.tsx
⎇
Raw
1import "./DownloadButton.css";
2import type { MediaFile, Directory, FileListing } from "music-server-shared/types";
3import { deleteFile, download, downloadStatusMap } from "../offline";
4import type { Accessor, ResourceActions } from "solid-js";
5import type { AppOptions } from "../types";
6import { formatBytes, joinPath } from "../utils";
7import { DeleteSVG, RefreshSVG, SyncSVG } from "./SVG";
8import { LoadingSpinner } from "./LoadingScreen";
9
10interface DownloadButtonProps {
11 fileName: string;
12 item: MediaFile | Directory;
13 fetchFiles: ResourceActions<FileListing>;
14 isOffline: Accessor<boolean>;
15 currentDir: Accessor<string>;
16 options: AppOptions;
17}
18
19export function DownloadButton(props: DownloadButtonProps) {
20 const fullPath = joinPath(props.currentDir(), props.fileName);
21
22 return (
23 <div
24 class="download-item"
25 onClick={(event) => {
26 event.stopPropagation();
27 if (!props.isOffline())
28 download(props.fileName, props.item, props.currentDir(), props.currentDir, props.options, props.fetchFiles);
29 else deleteFile(props.fileName, props.item, props.currentDir(), props.fetchFiles);
30 }}
31 >
32 {(() => {
33 const progress = downloadStatusMap[fullPath];
34 if (progress === "UnspecificProgress") return <LoadingSpinner center={true} />;
35 if (props.isOffline()) {
36 return (
37 <button title="Purge this file/these files from local sync storage">
38 <DeleteSVG />
39 </button>
40 );
41 }
42 if (progress === undefined) {
43 return (
44 <button title="Sync this file/directory to your browser for offline use">
45 <SyncSVG />
46 </button>
47 );
48 }
49 if (progress === "Synced") {
50 return (
51 <button title="Re-sync this file">
52 <RefreshSVG />
53 </button>
54 );
55 }
56
57 if ("bytes" in progress) return <span>{progress.bytes === 0 ? "Queued..." : formatBytes(progress.bytes)}</span>;
58 console.error("Unexpected", progress);
59 })()}
60 </div>
61 );
62}
63