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>
123 lines
4.2 KiB
TypeScript
123 lines
4.2 KiB
TypeScript
"use client"
|
|
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
|
|
import type { PlaylistSummary } from "@otunes/convex/types"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import { playlistCoverUrl } from "@/lib/cover-urls"
|
|
import { useConvex } from "convex/react"
|
|
import { MoreHorizontal, Play } from "lucide-react"
|
|
|
|
import { useCacheScope } from "@/components/persistent-cache-provider"
|
|
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
|
import {
|
|
MediaContextMenuItems,
|
|
type MediaContextAction,
|
|
} from "@/components/media-context-menu"
|
|
import { usePlaybackActions } from "@/components/playback-provider"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu"
|
|
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import { fetchCachedQuery } from "@otunes/convex/client/cached-query"
|
|
|
|
export function PlaylistCard({ playlist }: { playlist: PlaylistSummary }) {
|
|
const convex = useConvex()
|
|
const cacheScope = useCacheScope()
|
|
const { openAddPlaylistToPlaylist } = useAddToPlaylist()
|
|
const { playPlaylist } = usePlaybackActions()
|
|
const { runQueueAction } = useQueueMediaActions()
|
|
|
|
async function handleCardAction(action: MediaContextAction) {
|
|
switch (action) {
|
|
case "playlist":
|
|
void openAddPlaylistToPlaylist(playlist.id as Id<"playlists">)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle": {
|
|
const songIds = await fetchCachedQuery(
|
|
convex,
|
|
cacheScope,
|
|
api.playlists.getPlaylistSongIds,
|
|
{
|
|
playlistId: playlist.id as Id<"playlists">,
|
|
}
|
|
)
|
|
void runQueueAction(action, songIds)
|
|
break
|
|
}
|
|
case "favorite":
|
|
break
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<div className="group relative aspect-square overflow-hidden rounded-lg ring-1 ring-foreground/10">
|
|
<Link className="absolute inset-0" href={"/playlist/" + playlist.id}>
|
|
<Image
|
|
src={playlistCoverUrl(playlist.id, playlist.cover, 384)}
|
|
alt={playlist.title}
|
|
fill
|
|
unoptimized
|
|
className="object-cover"
|
|
/>
|
|
</Link>
|
|
|
|
<div className="pointer-events-none absolute inset-0 bg-black/0 transition-colors group-hover:bg-black/40" />
|
|
|
|
<div className="pointer-events-none absolute inset-x-0 bottom-0 z-10 flex items-center justify-between p-2 opacity-0 transition-opacity group-hover:opacity-100 [&_button]:pointer-events-auto">
|
|
<Button
|
|
size="icon-sm"
|
|
className="rounded-full"
|
|
aria-label={`Play ${playlist.title}`}
|
|
onClick={(event) => {
|
|
event.preventDefault()
|
|
void playPlaylist(playlist.id as Id<"playlists">)
|
|
}}
|
|
>
|
|
<Play className="fill-current" />
|
|
</Button>
|
|
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className="rounded-full text-white hover:bg-white/20 hover:text-white"
|
|
aria-label={`More options for ${playlist.title}`}
|
|
onClick={(event) => event.preventDefault()}
|
|
>
|
|
<MoreHorizontal />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" className="w-48">
|
|
<MediaContextMenuItems
|
|
selectedCount={1}
|
|
showFavorite={false}
|
|
onAction={(action) => void handleCardAction(action)}
|
|
share={{ type: "playlist", playlistId: playlist.id }}
|
|
/>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="min-w-0">
|
|
<Link href={"/playlist/" + playlist.id} className="hover:underline">
|
|
<p className="truncate text-sm font-medium">{playlist.title}</p>
|
|
</Link>
|
|
<p className="truncate text-xs text-muted-foreground decoration-muted-foreground">
|
|
{playlist.owner}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|