DownloadButton.tsx
| 1 | import "./DownloadButton.css"; |
| 2 | import type { MediaFile, Directory, FileListing } from "music-server-shared/types"; |
| 3 | import { deleteFile, download, downloadStatusMap } from "../offline"; |
| 4 | import type { Accessor, ResourceActions } from "solid-js"; |
| 5 | import type { AppOptions } from "../types"; |
| 6 | import { formatBytes, joinPath } from "../utils"; |
| 7 | import { DeleteSVG, RefreshSVG, SyncSVG } from "./SVG"; |
| 8 | import { LoadingSpinner } from "./LoadingScreen"; |
| 9 | |
| 10 | interface 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 | |
| 19 | export 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 (props.isOffline()) { |
| 35 | return ( |
| 36 | <button title="Purge this file/these files from local sync storage"> |
| 37 | <DeleteSVG /> |
| 38 | </button> |
| 39 | ); |
| 40 | } |
| 41 | if (progress === undefined) { |
| 42 | return ( |
| 43 | <button title="Sync this file/directory to your browser for offline use"> |
| 44 | <SyncSVG /> |
| 45 | </button> |
| 46 | ); |
| 47 | } |
| 48 | if (progress === "DirectoryProgress") return <LoadingSpinner center={true} />; |
| 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 |