113 lines
3.5 KiB
TypeScript
113 lines
3.5 KiB
TypeScript
"use client"
|
|
|
|
import { useDownloads } from "@/components/downloads-provider"
|
|
import { useSongDownloadState } from "@/hooks/use-downloads"
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
|
import {
|
|
MediaContextMenu,
|
|
type MediaContextAction,
|
|
MediaContextMenuArea,
|
|
type MediaContextMenuProps,
|
|
} from "@/components/media-context-menu"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { BrowseSong } from "@otunes/convex/types"
|
|
import { albumCoverUrl } from "@/lib/cover-urls"
|
|
|
|
export function SearchSongItem({ song }: { song: BrowseSong }) {
|
|
const { openAddToPlaylist } = useAddToPlaylist()
|
|
const { isSongFavorite, toggleSongFavorite } = useFavorites()
|
|
const { runQueueAction, createStation } = useQueueMediaActions()
|
|
const songIds = [song.id as Id<"songs">]
|
|
const { requestDownload, removeDownload } = useDownloads()
|
|
const downloaded = useSongDownloadState(song.id) === "here"
|
|
const downloadTarget = {
|
|
kind: "songs" as const,
|
|
songIds,
|
|
title: song.title,
|
|
}
|
|
|
|
function handleAction(action: MediaContextAction) {
|
|
switch (action) {
|
|
case "create-station":
|
|
void createStation(song.artist.id)
|
|
break
|
|
case "playlist":
|
|
openAddToPlaylist(songIds)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle":
|
|
void runQueueAction(action, songIds)
|
|
break
|
|
case "favorite":
|
|
void toggleSongFavorite(song.id as Id<"songs">)
|
|
break
|
|
case "download":
|
|
void requestDownload(downloadTarget)
|
|
break
|
|
case "remove-download":
|
|
void removeDownload(downloadTarget)
|
|
break
|
|
}
|
|
}
|
|
|
|
const menu: MediaContextMenuProps = {
|
|
label: `More options for ${song.title}`,
|
|
stationArtistId: song.artist.id,
|
|
selectedCount: 1,
|
|
onOpenMenu: () => {},
|
|
onAction: handleAction,
|
|
favorited: isSongFavorite(song.id),
|
|
downloaded,
|
|
navigation: {
|
|
artist: { id: song.artist.id },
|
|
album: { id: song.album.id },
|
|
},
|
|
share: {
|
|
type: "song",
|
|
albumId: song.album.id,
|
|
songId: song.id,
|
|
},
|
|
triggerClassName:
|
|
"size-8 shrink-0 opacity-0 transition-opacity group-hover:opacity-100 data-[state=open]:opacity-100",
|
|
triggerVariant: "ghost",
|
|
}
|
|
|
|
return (
|
|
<MediaContextMenuArea menu={menu}>
|
|
<div className="group flex items-center gap-3 rounded-lg px-1 py-1.5 transition-colors hover:bg-muted/50">
|
|
<Link
|
|
href={`/album/${song.album.id}`}
|
|
className="relative size-11 shrink-0 overflow-hidden rounded ring-1 ring-foreground/10"
|
|
>
|
|
<Image
|
|
src={albumCoverUrl(song.album.id)}
|
|
alt={song.album.title}
|
|
fill
|
|
sizes="44px"
|
|
className="object-cover"
|
|
/>
|
|
</Link>
|
|
|
|
<div className="min-w-0 flex-1">
|
|
<Link href={`/album/${song.album.id}`} className="hover:underline">
|
|
<p className="truncate text-sm font-medium">{song.title}</p>
|
|
</Link>
|
|
<Link
|
|
href={`/artist/${song.artist.id}`}
|
|
className="text-muted-foreground hover:text-foreground hover:underline"
|
|
>
|
|
<p className="truncate text-sm">{song.artist.name}</p>
|
|
</Link>
|
|
</div>
|
|
|
|
<MediaContextMenu {...menu} />
|
|
</div>
|
|
</MediaContextMenuArea>
|
|
)
|
|
}
|