DownloadButton.tsx
⎇
Raw
1import "./DownloadButton.css";
2import type { MediaFile, Directory, FileListing } from "music-server-shared/types";
3import { deleteFile, download, getDownloadStatus } 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 [getStatus] = getDownloadStatus(fullPath);
34 const progress = getStatus();
35 if (progress === "UnspecificProgress") return <LoadingSpinner center={true} />;
36 if (props.isOffline()) {
37 return (
38 <button title="Purge this file/these files from local sync storage">
39 <DeleteSVG />
40 </button>
41 );
42 }
43 if (progress === undefined) {
44 return (
45 <button title="Sync this file/directory to your browser for offline use">
46 <SyncSVG />
47 </button>
48 );
49 }
50 if (progress === "Synced") {
51 return (
52 <button title="Re-sync this file">
53 <RefreshSVG />
54 </button>
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