DownloadButton.tsx
| 1 | import "./DownloadButton.css"; |
| 2 | import type { Directory, MediaFile } from "music-server-shared/types"; |
| 3 | import { type Accessor, createMemo, type ResourceActions } from "solid-js"; |
| 4 | import type { FlatFileListing } from "../App"; |
| 5 | import { deleteFile, download, getDownloadStatus } from "../offline"; |
| 6 | import type { AppOptions } from "../types"; |
| 7 | import { formatBytes } from "../utils"; |
| 8 | import { LoadingSpinner } from "./LoadingScreen"; |
| 9 | import { DeleteSVG, RefreshSVG, SyncSVG } from "./SVG"; |
| 10 | |
| 11 | interface DownloadButtonProps { |
| 12 | fullPath: string; |
| 13 | item: MediaFile | Directory; |
| 14 | fetchFiles: ResourceActions<FlatFileListing>; |
| 15 | isOffline: Accessor<boolean>; |
| 16 | currentDir: Accessor<string>; |
| 17 | options: AppOptions; |
| 18 | } |
| 19 | |
| 20 | export function DownloadButton(props: DownloadButtonProps) { |
| 21 | return ( |
| 22 | <div |
| 23 | class="download-item" |
| 24 | onClick={(event) => { |
| 25 | event.stopPropagation(); |
| 26 | if (!props.isOffline()) |
| 27 | download(props.fullPath, props.item, props.options, props.fetchFiles, "metadata" in props.item); |
| 28 | else deleteFile(props.fullPath, props.item, props.fetchFiles); |
| 29 | }} |
| 30 | > |
| 31 | {(() => { |
| 32 | //memo needed so progress amount doesn't cause rerenders |
| 33 | type ButtonType = "Spinner" | "Delete" | "Sync" | "Refresh" | { status: number }; |
| 34 | const progressMemo = createMemo<ButtonType>(() => { |
| 35 | const progress = getDownloadStatus(props.fullPath); |
| 36 | if (typeof progress === "object" && "amount" in progress) return "Spinner"; |
| 37 | if (props.isOffline()) return "Delete"; |
| 38 | if (progress === "Synced") return "Refresh"; |
| 39 | if (progress === undefined || ("status" in progress && typeof progress.status !== "number")) return "Sync"; |
| 40 | if (typeof progress.status === "number") return { status: progress.status }; |
| 41 | throw new Error(`Unexpected: ${progress}`); |
| 42 | }); |
| 43 | const progress = progressMemo(); |
| 44 | if (progress === "Spinner") return <LoadingSpinner center={true} />; |
| 45 | if (progress === "Delete") { |
| 46 | return ( |
| 47 | <button title="Purge this file/these files from local sync storage"> |
| 48 | <DeleteSVG /> |
| 49 | </button> |
| 50 | ); |
| 51 | } |
| 52 | if (progress === "Sync") { |
| 53 | return ( |
| 54 | <button title="Sync missing files this file/directory to your browser for offline use"> |
| 55 | <SyncSVG /> |
| 56 | </button> |
| 57 | ); |
| 58 | } |
| 59 | if (progress === "Refresh") { |
| 60 | return ( |
| 61 | <button title="Re-sync this file"> |
| 62 | <RefreshSVG /> |
| 63 | </button> |
| 64 | ); |
| 65 | } |
| 66 | if ("status" in progress) return <span>{formatBytes(progress.status)}</span>; |
| 67 | console.error("Unexpected", progress); |
| 68 | })()} |
| 69 | </div> |
| 70 | ); |
| 71 | } |
| 72 |