262 lines
6.4 KiB
TypeScript
262 lines
6.4 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import Link from "next/link"
|
|
import {
|
|
Disc3,
|
|
ListEnd,
|
|
ListPlus,
|
|
ListStart,
|
|
MoreHorizontal,
|
|
Share2,
|
|
Shuffle,
|
|
Star,
|
|
User,
|
|
} from "lucide-react"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu"
|
|
import {
|
|
copyMediaShareLink,
|
|
type MediaShareContext,
|
|
} from "@/lib/media-links"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
export type MediaContextAction =
|
|
| "playlist"
|
|
| "play-next"
|
|
| "play-last"
|
|
| "shuffle"
|
|
| "favorite"
|
|
|
|
export type MediaContextNavigation = {
|
|
artist?: { id: string }
|
|
album?: { id: string }
|
|
}
|
|
|
|
export type { MediaShareContext } from "@/lib/media-links"
|
|
|
|
function shouldShowShare(
|
|
share: MediaShareContext | undefined,
|
|
selectedCount: number
|
|
) {
|
|
if (!share) {
|
|
return false
|
|
}
|
|
if (share.type === "song") {
|
|
return selectedCount === 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
function actionLabel(base: string, selectedCount: number) {
|
|
return selectedCount > 1 ? `${base} (${selectedCount})` : base
|
|
}
|
|
|
|
export function MediaContextMenuItems({
|
|
selectedCount,
|
|
onAction,
|
|
playlistLabel = "Add to Playlist",
|
|
showFavorite = true,
|
|
favorited = false,
|
|
navigation,
|
|
share,
|
|
}: {
|
|
selectedCount: number
|
|
onAction: (action: MediaContextAction) => void
|
|
playlistLabel?: string
|
|
showFavorite?: boolean
|
|
favorited?: boolean
|
|
navigation?: MediaContextNavigation
|
|
share?: MediaShareContext
|
|
}) {
|
|
const { artist, album } = navigation ?? {}
|
|
const showShare = shouldShowShare(share, selectedCount)
|
|
|
|
return (
|
|
<>
|
|
{showShare ? (
|
|
<>
|
|
<DropdownMenuItem
|
|
className="justify-between"
|
|
onClick={() => void copyMediaShareLink(share!)}
|
|
>
|
|
Share
|
|
<Share2 />
|
|
</DropdownMenuItem>
|
|
<DropdownMenuSeparator />
|
|
</>
|
|
) : null}
|
|
<DropdownMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("playlist")}
|
|
>
|
|
{actionLabel(playlistLabel, selectedCount)}
|
|
<ListPlus />
|
|
</DropdownMenuItem>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("play-next")}
|
|
>
|
|
{actionLabel("Play Next", selectedCount)}
|
|
<ListStart />
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("play-last")}
|
|
>
|
|
{actionLabel("Play Last", selectedCount)}
|
|
<ListEnd />
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("shuffle")}
|
|
>
|
|
{actionLabel("Shuffle", selectedCount)}
|
|
<Shuffle />
|
|
</DropdownMenuItem>
|
|
{artist || album ? (
|
|
<>
|
|
<DropdownMenuSeparator />
|
|
{artist ? (
|
|
<DropdownMenuItem asChild className="justify-between">
|
|
<Link href={`/artist/${artist.id}`}>
|
|
Go to Artist
|
|
<User />
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
) : null}
|
|
{album ? (
|
|
<DropdownMenuItem asChild className="justify-between">
|
|
<Link href={`/album/${album.id}`}>
|
|
Go to Album
|
|
<Disc3 />
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
) : null}
|
|
</>
|
|
) : null}
|
|
{showFavorite ? (
|
|
<>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem
|
|
variant={favorited ? "destructive" : "default"}
|
|
className="justify-between"
|
|
onClick={() => onAction("favorite")}
|
|
>
|
|
{actionLabel(favorited ? "Unfavorite" : "Favorite", selectedCount)}
|
|
<Star />
|
|
</DropdownMenuItem>
|
|
</>
|
|
) : null}
|
|
</>
|
|
)
|
|
}
|
|
|
|
export function mediaContextMenuTriggerClassName({
|
|
inSelectionRange = false,
|
|
trackRow = true,
|
|
className,
|
|
}: {
|
|
inSelectionRange?: boolean
|
|
/** When false, uses default button styling (e.g. page header menus). */
|
|
trackRow?: boolean
|
|
className?: string
|
|
}) {
|
|
if (!trackRow) {
|
|
return cn(className)
|
|
}
|
|
|
|
return cn(
|
|
"media-context-menu-trigger",
|
|
"size-8 shrink-0 transition-opacity",
|
|
inSelectionRange
|
|
? "text-primary-foreground opacity-100 hover:bg-white/15 hover:text-primary-foreground"
|
|
: "opacity-0 group-hover:opacity-100 data-[state=open]:opacity-100",
|
|
inSelectionRange && "media-context-menu-trigger--in-range",
|
|
className
|
|
)
|
|
}
|
|
|
|
export function MediaContextMenu({
|
|
label,
|
|
selectedCount,
|
|
onOpenMenu,
|
|
onAction,
|
|
playlistLabel,
|
|
showFavorite = true,
|
|
favorited = false,
|
|
navigation,
|
|
share,
|
|
inSelectionRange = false,
|
|
trackRowTrigger = true,
|
|
triggerClassName,
|
|
triggerVariant = "ghost",
|
|
triggerIconClassName = "size-4",
|
|
align = "end",
|
|
children,
|
|
}: {
|
|
label: string
|
|
selectedCount: number
|
|
onOpenMenu: () => void
|
|
onAction: (action: MediaContextAction) => void
|
|
playlistLabel?: string
|
|
showFavorite?: boolean
|
|
favorited?: boolean
|
|
navigation?: MediaContextNavigation
|
|
share?: MediaShareContext
|
|
inSelectionRange?: boolean
|
|
/** When false, the trigger uses default button/dropdown styling (page headers). */
|
|
trackRowTrigger?: boolean
|
|
triggerClassName?: string
|
|
triggerVariant?: "ghost" | "secondary"
|
|
triggerIconClassName?: string
|
|
align?: "start" | "end"
|
|
children?: React.ReactNode
|
|
}) {
|
|
return (
|
|
<DropdownMenu
|
|
onOpenChange={(open) => {
|
|
if (open) {
|
|
onOpenMenu()
|
|
}
|
|
}}
|
|
>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
variant={triggerVariant}
|
|
size="icon"
|
|
className={mediaContextMenuTriggerClassName({
|
|
inSelectionRange,
|
|
trackRow: trackRowTrigger,
|
|
className: triggerClassName,
|
|
})}
|
|
aria-label={label}
|
|
onClick={(event) => event.stopPropagation()}
|
|
>
|
|
<MoreHorizontal className={triggerIconClassName} />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align={align} className="w-48">
|
|
<MediaContextMenuItems
|
|
selectedCount={selectedCount}
|
|
onAction={onAction}
|
|
playlistLabel={playlistLabel}
|
|
showFavorite={showFavorite}
|
|
favorited={favorited}
|
|
navigation={navigation}
|
|
share={share}
|
|
/>
|
|
{children}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)
|
|
}
|