356 lines
9.2 KiB
TypeScript
356 lines
9.2 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import Link from "next/link"
|
|
import {
|
|
ArrowDownToLine,
|
|
CircleX,
|
|
GalleryVerticalEnd,
|
|
ListEnd,
|
|
ListPlus,
|
|
ListStart,
|
|
MicVocal,
|
|
MoreHorizontal,
|
|
Share2,
|
|
Shuffle,
|
|
Star,
|
|
} from "lucide-react"
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
ContextMenu,
|
|
ContextMenuContent,
|
|
ContextMenuItem,
|
|
ContextMenuSeparator,
|
|
ContextMenuTrigger,
|
|
} from "@/components/ui/context-menu"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu"
|
|
import { copyMediaShareLink, type MediaShareContext } from "@/lib/media-links"
|
|
import { shouldShowShare } from "@otunes/convex/client/media-links"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
export type MediaContextAction =
|
|
| "playlist"
|
|
| "play-next"
|
|
| "play-last"
|
|
| "shuffle"
|
|
| "create-station"
|
|
| "favorite"
|
|
| "download"
|
|
| "remove-download"
|
|
|
|
export type MediaContextNavigation = {
|
|
artist?: { id: string }
|
|
album?: { id: string }
|
|
}
|
|
|
|
export type { MediaShareContext } from "@/lib/media-links"
|
|
|
|
// Lets the same items render inside a dropdown or a right-click menu.
|
|
const MenuKindContext = React.createContext<"dropdown" | "context">("dropdown")
|
|
|
|
/** Menu item that works in both `MediaContextMenu` and `MediaContextMenuArea`. */
|
|
export function MediaMenuItem(
|
|
props: React.ComponentProps<typeof DropdownMenuItem>
|
|
) {
|
|
return React.use(MenuKindContext) === "context" ? (
|
|
<ContextMenuItem {...props} />
|
|
) : (
|
|
<DropdownMenuItem {...props} />
|
|
)
|
|
}
|
|
|
|
export function MediaMenuSeparator() {
|
|
return React.use(MenuKindContext) === "context" ? (
|
|
<ContextMenuSeparator />
|
|
) : (
|
|
<DropdownMenuSeparator />
|
|
)
|
|
}
|
|
|
|
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,
|
|
stationArtistId,
|
|
navigation,
|
|
share,
|
|
downloaded,
|
|
}: {
|
|
selectedCount: number
|
|
onAction: (action: MediaContextAction) => void
|
|
playlistLabel?: string
|
|
showFavorite?: boolean
|
|
favorited?: boolean
|
|
stationArtistId?: string
|
|
navigation?: MediaContextNavigation
|
|
share?: MediaShareContext
|
|
/** Shows Download, or Remove Download when true. Omit to hide both. */
|
|
downloaded?: boolean
|
|
}) {
|
|
const { artist, album } = navigation ?? {}
|
|
const showShare = shouldShowShare(share, selectedCount)
|
|
|
|
return (
|
|
<>
|
|
{showShare ? (
|
|
<>
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => void copyMediaShareLink(share!)}
|
|
>
|
|
Share
|
|
<Share2 />
|
|
</MediaMenuItem>
|
|
<MediaMenuSeparator />
|
|
</>
|
|
) : null}
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("playlist")}
|
|
>
|
|
{actionLabel(playlistLabel, selectedCount)}
|
|
<ListPlus />
|
|
</MediaMenuItem>
|
|
{downloaded !== undefined ? (
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction(downloaded ? "remove-download" : "download")}
|
|
>
|
|
{actionLabel(
|
|
downloaded ? "Remove Download" : "Download",
|
|
selectedCount
|
|
)}
|
|
{downloaded ? <CircleX /> : <ArrowDownToLine />}
|
|
</MediaMenuItem>
|
|
) : null}
|
|
<MediaMenuSeparator />
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("play-next")}
|
|
>
|
|
{actionLabel("Play Next", selectedCount)}
|
|
<ListStart />
|
|
</MediaMenuItem>
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("play-last")}
|
|
>
|
|
{actionLabel("Play Last", selectedCount)}
|
|
<ListEnd />
|
|
</MediaMenuItem>
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("shuffle")}
|
|
>
|
|
{actionLabel("Shuffle", selectedCount)}
|
|
<Shuffle />
|
|
</MediaMenuItem>
|
|
{stationArtistId ? (
|
|
<MediaMenuItem
|
|
className="justify-between"
|
|
onClick={() => onAction("create-station")}
|
|
>
|
|
Create Station
|
|
<Shuffle />
|
|
</MediaMenuItem>
|
|
) : null}
|
|
{artist || album ? (
|
|
<>
|
|
<MediaMenuSeparator />
|
|
{artist ? (
|
|
<MediaMenuItem asChild className="justify-between">
|
|
<Link href={`/artist/${artist.id}`}>
|
|
Go to Artist
|
|
<MicVocal />
|
|
</Link>
|
|
</MediaMenuItem>
|
|
) : null}
|
|
{album ? (
|
|
<MediaMenuItem asChild className="justify-between">
|
|
<Link href={`/album/${album.id}`}>
|
|
Go to Album
|
|
<GalleryVerticalEnd />
|
|
</Link>
|
|
</MediaMenuItem>
|
|
) : null}
|
|
</>
|
|
) : null}
|
|
{showFavorite ? (
|
|
<>
|
|
<MediaMenuSeparator />
|
|
<MediaMenuItem
|
|
variant={favorited ? "destructive" : "default"}
|
|
className="justify-between"
|
|
onClick={() => onAction("favorite")}
|
|
>
|
|
{actionLabel(favorited ? "Unfavorite" : "Favorite", selectedCount)}
|
|
<Star />
|
|
</MediaMenuItem>
|
|
</>
|
|
) : 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 type MediaContextMenuProps = {
|
|
label: string
|
|
selectedCount: number
|
|
onOpenMenu: () => void
|
|
onAction: (action: MediaContextAction) => void
|
|
playlistLabel?: string
|
|
showFavorite?: boolean
|
|
favorited?: boolean
|
|
stationArtistId?: string
|
|
navigation?: MediaContextNavigation
|
|
share?: MediaShareContext
|
|
downloaded?: boolean
|
|
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"
|
|
/** Extra items; use `MediaMenuItem` so they also work in the right-click menu. */
|
|
children?: React.ReactNode
|
|
}
|
|
|
|
/**
|
|
* Opens `menu` (the props given to the row's `MediaContextMenu`) when
|
|
* `children`, a single element such as a track row, is right-clicked.
|
|
*/
|
|
export function MediaContextMenuArea({
|
|
menu,
|
|
children,
|
|
}: {
|
|
menu: MediaContextMenuProps
|
|
children: React.ReactElement
|
|
}) {
|
|
return (
|
|
<ContextMenu
|
|
onOpenChange={(open) => {
|
|
if (open) {
|
|
menu.onOpenMenu()
|
|
}
|
|
}}
|
|
>
|
|
<ContextMenuTrigger asChild>{children}</ContextMenuTrigger>
|
|
<ContextMenuContent className="w-48">
|
|
<MenuKindContext value="context">
|
|
<MediaContextMenuItems
|
|
selectedCount={menu.selectedCount}
|
|
onAction={menu.onAction}
|
|
playlistLabel={menu.playlistLabel}
|
|
showFavorite={menu.showFavorite}
|
|
favorited={menu.favorited}
|
|
stationArtistId={menu.stationArtistId}
|
|
navigation={menu.navigation}
|
|
share={menu.share}
|
|
downloaded={menu.downloaded}
|
|
/>
|
|
{menu.children}
|
|
</MenuKindContext>
|
|
</ContextMenuContent>
|
|
</ContextMenu>
|
|
)
|
|
}
|
|
|
|
export function MediaContextMenu({
|
|
label,
|
|
selectedCount,
|
|
onOpenMenu,
|
|
onAction,
|
|
playlistLabel,
|
|
showFavorite = true,
|
|
favorited = false,
|
|
stationArtistId,
|
|
navigation,
|
|
share,
|
|
downloaded,
|
|
inSelectionRange = false,
|
|
trackRowTrigger = true,
|
|
triggerClassName,
|
|
triggerVariant = "ghost",
|
|
triggerIconClassName = "size-4",
|
|
align = "end",
|
|
children,
|
|
}: MediaContextMenuProps) {
|
|
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}
|
|
stationArtistId={stationArtistId}
|
|
navigation={navigation}
|
|
share={share}
|
|
downloaded={downloaded}
|
|
/>
|
|
{children}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)
|
|
}
|