Files
otunes/apps/web/components/playlist-cover-dialog.tsx
T
joshuafhigginsandClaude Opus 5.5 Verified 886c8f0643 feat: playlist cover artwork
Playlists draw a 2x2 of their first albums automatically. Owners can pick
from random grids, reshuffle, or upload an image on web and mobile.
Covers are rendered once into Convex storage and redrawn only when the
playlist's albums change. Web and iPad sidebars show playlist covers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 13:15:21 -05:00

216 lines
5.9 KiB
TypeScript

"use client"
import * as React from "react"
import Image from "next/image"
import { useMutation } from "convex/react"
import { ArrowUp, Shuffle } from "lucide-react"
import { toast } from "sonner"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { albumCoverUrl, playlistCoverUrl } from "@/lib/cover-urls"
import { resizeImageToDataUrl } from "@/lib/resize-image"
import { cn } from "@/lib/utils"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import {
COVER_GRID_ALBUMS,
playlistCoverAlbums,
randomPlaylistCovers,
} from "@otunes/convex/client/playlist-cover"
import type { BrowseSong } from "@otunes/convex/types"
const UPLOAD_SIZE = 1024
/**
* Lets a playlist owner pick generated artwork (random 2x2 grids of the
* playlist's albums, reshuffled on demand) or upload an image. The current
* cover sits apart from the options so the owner can always keep it.
*/
export function PlaylistCoverDialog({
open,
onOpenChange,
playlistId,
title,
coverVersion,
songs,
}: {
open: boolean
onOpenChange: (open: boolean) => void
playlistId: Id<"playlists">
title: string
coverVersion: string | null
songs: BrowseSong[]
}) {
const setCoverAlbums = useMutation(api.playlists.setPlaylistCoverAlbums)
const uploadCover = useMutation(api.playlists.uploadPlaylistCover)
const fileInputRef = React.useRef<HTMLInputElement>(null)
const albums = React.useMemo(() => playlistCoverAlbums(songs), [songs])
const [options, setOptions] = React.useState<Id<"albums">[][]>([])
const canShuffle = albums.length >= COVER_GRID_ALBUMS
function shuffle() {
setOptions(randomPlaylistCovers(albums))
}
// Fresh options each time the dialog opens.
const [wasOpen, setWasOpen] = React.useState(false)
if (open !== wasOpen) {
setWasOpen(open)
if (open) shuffle()
}
async function pick(albumIds: Id<"albums">[]) {
onOpenChange(false)
try {
await setCoverAlbums({ playlistId, albumIds })
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to change cover"
)
}
}
async function upload(file: File) {
if (!file.type.startsWith("image/")) {
toast.error("Choose an image file")
return
}
onOpenChange(false)
try {
const image = await resizeImageToDataUrl(file, UPLOAD_SIZE)
await uploadCover({ playlistId, image })
} catch (error) {
toast.error(
error instanceof Error ? error.message : "Failed to upload cover"
)
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-xl">
<DialogHeader>
<DialogTitle>Playlist Cover</DialogTitle>
</DialogHeader>
<div className="flex items-stretch gap-3">
{coverVersion ? (
<>
<CoverButton
label="Keep current cover"
selected
onClick={() => onOpenChange(false)}
>
<Image
src={playlistCoverUrl(playlistId, coverVersion, 256)}
alt={title}
fill
unoptimized
className="object-cover"
/>
</CoverButton>
<div className="w-px shrink-0 bg-border" />
</>
) : null}
<div className="grid flex-[4] grid-cols-4 gap-3">
{options.map((albumIds) => (
<CoverButton
key={albumIds.join(",")}
label="Use this cover"
onClick={() => void pick(albumIds)}
>
<AlbumGrid albumIds={albumIds} />
</CoverButton>
))}
</div>
</div>
{albums.length === 0 ? (
<p className="text-sm text-muted-foreground">
No album artwork in this playlist yet.
</p>
) : null}
<div className="flex gap-2">
{canShuffle ? (
<Button variant="secondary" onClick={shuffle}>
<Shuffle />
Shuffle
</Button>
) : null}
<Button
variant="secondary"
onClick={() => fileInputRef.current?.click()}
>
<ArrowUp />
Upload Image
</Button>
<input
ref={fileInputRef}
type="file"
accept="image/*"
className="hidden"
onChange={(event) => {
const file = event.target.files?.[0]
event.target.value = ""
if (file) void upload(file)
}}
/>
</div>
</DialogContent>
</Dialog>
)
}
function CoverButton({
label,
selected = false,
onClick,
children,
}: {
label: string
selected?: boolean
onClick: () => void
children: React.ReactNode
}) {
return (
<button
type="button"
aria-label={label}
onClick={onClick}
className={cn(
"relative aspect-square min-w-0 flex-1 overflow-hidden rounded-md ring-offset-2 ring-offset-background transition-shadow outline-none hover:ring-2 hover:ring-foreground/60 focus-visible:ring-2 focus-visible:ring-ring",
selected && "ring-2 ring-foreground hover:ring-foreground"
)}
>
{children}
</button>
)
}
/** Preview of a generated cover: one album alone, or four tiled 2x2. */
function AlbumGrid({ albumIds }: { albumIds: Id<"albums">[] }) {
const grid = albumIds.length > 1
return (
<div className={cn("grid size-full", grid && "grid-cols-2")}>
{albumIds.map((albumId) => (
<div key={albumId} className="relative">
<Image
src={albumCoverUrl(albumId)}
alt=""
fill
sizes={grid ? "64px" : "128px"}
className="object-cover"
/>
</div>
))}
</div>
)
}