DownloadButton.tsx
| 1 | import "./DownloadButton.css"; |
| 2 | import type { AudioFile, Directory, FileListing } from "music-server-shared/types"; |
| 3 | import { deleteFile, download, signalMap, type SignalMapEntry } from "../offline"; |
| 4 | import { createSignal, type Accessor, type ResourceActions } from "solid-js"; |
| 5 | import type { AppOptions } from "../types"; |
| 6 | import { formatBytes, joinPath } from "../utils"; |
| 7 | import { DeleteSVG, DownloadSVG, RefreshSVG } from "./SVG"; |
| 8 | import { LoadingSpinner } from "./LoadingScreen"; |
| 9 | |
| 10 | interface DownloadButtonProps { |
| 11 | item: AudioFile | Directory; |
| 12 | fetchFiles: ResourceActions<FileListing>; |
| 13 | isOffline: Accessor<boolean>; |
| 14 | currentDir: Accessor<string>; |
| 15 | options: Accessor<AppOptions>; |
| 16 | } |
| 17 | |
| 18 | export function DownloadButton(props: DownloadButtonProps) { |
| 19 | const fullPath = joinPath(props.currentDir(), props.item.name); |
| 20 | let signal = signalMap.get(fullPath); |
| 21 | if (!signal) { |
| 22 | signal = createSignal<SignalMapEntry>(); |
| 23 | signalMap.set(fullPath, signal); |
| 24 | } |
| 25 | const [getProgress] = signal; |
| 26 | |
| 27 | return ( |
| 28 | <div |
| 29 | class="download-item" |
| 30 | onClick={(event) => { |
| 31 | event.stopPropagation(); |
| 32 | if (!props.isOffline()) download(props.item, props.currentDir(), props.options(), props.fetchFiles); |
| 33 | else deleteFile(props.item, props.currentDir(), props.fetchFiles); |
| 34 | }} |
| 35 | > |
| 36 | {(() => { |
| 37 | const progress = getProgress(); |
| 38 | if (progress === undefined) { |
| 39 | if (props.isOffline()) { |
| 40 | //only happens if this is a directory |
| 41 | return ( |
| 42 | <button title="Purge the cache from all files recursively in this directory"> |
| 43 | <DeleteSVG /> |
| 44 | </button> |
| 45 | ); |
| 46 | } |
| 47 | return ( |
| 48 | <button title="Cache this file/directory on your browser for offline use"> |
| 49 | <DownloadSVG /> |
| 50 | </button> |
| 51 | ); |
| 52 | } |
| 53 | if (progress === "DirectoryProgress") return <LoadingSpinner center={true} />; |
| 54 | if (progress === "CachedFile") { |
| 55 | if (props.isOffline()) { |
| 56 | return ( |
| 57 | <button title="Purge this file from cache"> |
| 58 | <DeleteSVG /> |
| 59 | </button> |
| 60 | ); |
| 61 | } |
| 62 | return ( |
| 63 | <button title="Re-download this file"> |
| 64 | <RefreshSVG /> |
| 65 | </button> |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | if ("bytes" in progress) return <span>{formatBytes(progress.bytes)}</span>; |
| 70 | console.log(progress); |
| 71 | console.error("Unexpected", progress); |
| 72 | })()} |
| 73 | </div> |
| 74 | ); |
| 75 | } |
| 76 |