175 lines
5.6 KiB
TypeScript
175 lines
5.6 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
import { MoreHorizontal, Play } from "lucide-react"
|
|
import { useConvex } from "convex/react"
|
|
|
|
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import {
|
|
MediaContextMenuItems,
|
|
type MediaContextAction,
|
|
} from "@/components/media-context-menu"
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
|
import { api } from "@/convex/_generated/api"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu"
|
|
import type { BrowseAlbum } from "@/convex/library"
|
|
import type { Id } from "@/convex/_generated/dataModel"
|
|
import { albumCoverUrl } from "@/lib/cover-urls"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
let activeAlbumCardMenuClose: (() => void) | null = null
|
|
|
|
function openExclusiveAlbumCardMenu(closeMenu: () => void) {
|
|
if (activeAlbumCardMenuClose && activeAlbumCardMenuClose !== closeMenu) {
|
|
activeAlbumCardMenuClose()
|
|
}
|
|
activeAlbumCardMenuClose = closeMenu
|
|
}
|
|
|
|
function closeExclusiveAlbumCardMenu(closeMenu: () => void) {
|
|
if (activeAlbumCardMenuClose === closeMenu) {
|
|
activeAlbumCardMenuClose = null
|
|
}
|
|
}
|
|
|
|
export function AlbumCard({ album }: { album: BrowseAlbum }) {
|
|
const convex = useConvex()
|
|
const { openAddAlbumToPlaylist } = useAddToPlaylist()
|
|
const { isAlbumFavorite, toggleAlbumFavorite } = useFavorites()
|
|
const { playAlbum } = usePlayback()
|
|
const { runQueueAction } = useQueueMediaActions()
|
|
const [menuOpen, setMenuOpen] = React.useState(false)
|
|
const [hovered, setHovered] = React.useState(false)
|
|
const showActions = hovered || menuOpen
|
|
|
|
const closeMenu = React.useCallback(() => setMenuOpen(false), [])
|
|
|
|
React.useEffect(() => {
|
|
return () => closeExclusiveAlbumCardMenu(closeMenu)
|
|
}, [closeMenu])
|
|
|
|
function handleMenuOpenChange(open: boolean) {
|
|
if (open) {
|
|
openExclusiveAlbumCardMenu(closeMenu)
|
|
} else {
|
|
closeExclusiveAlbumCardMenu(closeMenu)
|
|
}
|
|
setMenuOpen(open)
|
|
}
|
|
|
|
async function handleCardAction(action: MediaContextAction) {
|
|
switch (action) {
|
|
case "playlist":
|
|
void openAddAlbumToPlaylist(album.id as Id<"albums">)
|
|
break
|
|
case "favorite":
|
|
void toggleAlbumFavorite(album.id as Id<"albums">)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle": {
|
|
const songIds = await convex.query(api.playlists.getAlbumSongIds, {
|
|
albumId: album.id as Id<"albums">,
|
|
})
|
|
void runQueueAction(action, songIds)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<div
|
|
className="group relative aspect-square overflow-hidden rounded-lg ring-1 ring-foreground/10"
|
|
onMouseEnter={() => setHovered(true)}
|
|
onMouseLeave={() => setHovered(false)}
|
|
>
|
|
<Link
|
|
className="absolute inset-0"
|
|
href={"/album/" + album.id}
|
|
>
|
|
<Image
|
|
src={albumCoverUrl(album.id)}
|
|
alt={album.title}
|
|
fill
|
|
sizes="(max-width: 640px) 50vw, (max-width: 1024px) 25vw, 16vw"
|
|
className="object-cover"
|
|
/>
|
|
</Link>
|
|
|
|
<div
|
|
className={cn(
|
|
"pointer-events-none absolute inset-0 transition-colors",
|
|
showActions ? "bg-black/40" : "bg-black/0"
|
|
)}
|
|
/>
|
|
|
|
<div
|
|
className={cn(
|
|
"pointer-events-none absolute inset-0 z-10 flex items-end justify-between p-2 transition-opacity [&_button]:pointer-events-auto",
|
|
showActions ? "opacity-100" : "opacity-0"
|
|
)}
|
|
>
|
|
<Button
|
|
size="icon-sm"
|
|
className="rounded-full"
|
|
aria-label={`Play ${album.title}`}
|
|
onClick={(event) => {
|
|
event.preventDefault()
|
|
void playAlbum(album.id as Id<"albums">)
|
|
}}
|
|
>
|
|
<Play className="fill-current" />
|
|
</Button>
|
|
|
|
<DropdownMenu open={menuOpen} onOpenChange={handleMenuOpenChange}>
|
|
<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 ${album.title}`}
|
|
onClick={(event) => event.preventDefault()}
|
|
>
|
|
<MoreHorizontal />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" className="w-48">
|
|
<MediaContextMenuItems
|
|
selectedCount={1}
|
|
onAction={(action) => void handleCardAction(action)}
|
|
favorited={isAlbumFavorite(album.id)}
|
|
navigation={{ artist: { id: album.artist.id } }}
|
|
share={{ type: "album", albumId: album.id }}
|
|
/>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="min-w-0">
|
|
<Link href={"/album/" + album.id} className="hover:underline">
|
|
<p className="truncate text-sm font-medium">{album.title}</p>
|
|
</Link>
|
|
<Link
|
|
href={"/artist/" + album.artist.id}
|
|
className="text-muted-foreground decoration-muted-foreground hover:underline"
|
|
>
|
|
<p className="truncate text-xs">
|
|
{album.artist.name}
|
|
</p>
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|