146 lines
4.2 KiB
TypeScript
146 lines
4.2 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { ArrowDownToLine, Check } from "lucide-react"
|
|
|
|
import {
|
|
useDownloads,
|
|
type DownloadTarget,
|
|
} from "@/components/downloads-provider"
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@/components/ui/alert-dialog"
|
|
import { AudioFormatBadge } from "@/components/audio-format-badge"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
useCollectionDownload,
|
|
useIsCollectionDownloaded,
|
|
} from "@/hooks/use-downloads"
|
|
import { downloadQueue } from "@/lib/downloads"
|
|
import type { CollectionRef } from "@otunes/convex/client/download-queue"
|
|
import { DOWNLOAD_QUALITY_LABELS } from "@otunes/convex/download-quality"
|
|
|
|
const RING_RADIUS = 14
|
|
const RING_LENGTH = 2 * Math.PI * RING_RADIUS
|
|
|
|
/** Static ring that fills as songs finish; a square in the middle means "tap to stop". */
|
|
function ProgressRing({ fraction }: { fraction: number }) {
|
|
return (
|
|
<svg viewBox="0 0 36 36" className="size-6" aria-hidden>
|
|
<circle
|
|
cx="18"
|
|
cy="18"
|
|
r={RING_RADIUS}
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeOpacity={0.25}
|
|
strokeWidth={3}
|
|
/>
|
|
<circle
|
|
cx="18"
|
|
cy="18"
|
|
r={RING_RADIUS}
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeWidth={3}
|
|
strokeDasharray={RING_LENGTH}
|
|
strokeDashoffset={RING_LENGTH * (1 - fraction)}
|
|
transform="rotate(-90 18 18)"
|
|
/>
|
|
<rect x="14" y="14" width="8" height="8" fill="currentColor" />
|
|
</svg>
|
|
)
|
|
}
|
|
|
|
/** Download, progress, and downloaded states for an album or playlist header. */
|
|
export function DownloadButton({
|
|
target,
|
|
}: {
|
|
target: Exclude<DownloadTarget, { kind: "songs" }>
|
|
}) {
|
|
const { requestDownload, removeDownload } = useDownloads()
|
|
const { collection, status } = useCollectionDownload(target, target.songIds)
|
|
const [confirmRemove, setConfirmRemove] = React.useState(false)
|
|
|
|
const inProgress = collection !== undefined && status.queued > 0
|
|
const done = collection !== undefined && !inProgress && status.total > 0
|
|
|
|
let label = `Download ${target.title}`
|
|
let icon: React.ReactNode = <ArrowDownToLine />
|
|
if (inProgress) {
|
|
label = `Stop downloading, ${status.downloaded} of ${status.total} done`
|
|
icon = <ProgressRing fraction={status.downloaded / status.total} />
|
|
} else if (done) {
|
|
label = `Remove download of ${target.title}`
|
|
icon = <Check />
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Button
|
|
variant={done ? "default" : "secondary"}
|
|
size="icon"
|
|
className="rounded-full"
|
|
aria-label={label}
|
|
title={label}
|
|
disabled={target.songIds.length === 0}
|
|
onClick={() => {
|
|
if (inProgress) void removeDownload(target)
|
|
else if (done) setConfirmRemove(true)
|
|
else void requestDownload(target)
|
|
}}
|
|
>
|
|
{icon}
|
|
</Button>
|
|
<AlertDialog open={confirmRemove} onOpenChange={setConfirmRemove}>
|
|
<AlertDialogContent size="sm">
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Remove download?</AlertDialogTitle>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
variant="destructive"
|
|
onClick={() => void removeDownload(target)}
|
|
>
|
|
Remove
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</>
|
|
)
|
|
}
|
|
|
|
/** The quality an album or playlist was downloaded in, once downloaded. */
|
|
export function DownloadedQuality({
|
|
collection,
|
|
}: {
|
|
collection: CollectionRef
|
|
}) {
|
|
const downloaded = useIsCollectionDownloaded(collection)
|
|
const quality = downloaded
|
|
? downloadQueue.getCollection(collection)?.quality
|
|
: undefined
|
|
if (!quality) return null
|
|
return (
|
|
<span
|
|
className="inline-flex items-center gap-1"
|
|
aria-label={`Downloaded in ${DOWNLOAD_QUALITY_LABELS[quality]}`}
|
|
>
|
|
<ArrowDownToLine className="size-3.5" aria-hidden />
|
|
{quality === "lossless" || quality === "atmos" ? (
|
|
<AudioFormatBadge format={quality} />
|
|
) : (
|
|
DOWNLOAD_QUALITY_LABELS[quality]
|
|
)}
|
|
</span>
|
|
)
|
|
}
|