98 lines
2.6 KiB
TypeScript
98 lines
2.6 KiB
TypeScript
"use client"
|
|
|
|
import type { MouseEvent } from "react"
|
|
import { AudioLines, Pause, Play } from "lucide-react"
|
|
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { Button } from "@/components/ui/button"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
export function TrackNumberPlayButton({
|
|
displayTrack,
|
|
title,
|
|
isActive,
|
|
isNowPlaying,
|
|
inRange,
|
|
onPlay,
|
|
}: {
|
|
displayTrack: number | string
|
|
title: string
|
|
isActive: boolean
|
|
isNowPlaying: boolean
|
|
inRange: boolean
|
|
onPlay: () => void
|
|
}) {
|
|
const { isPlaying, setIsPlaying } = usePlayback()
|
|
const showPlay = isActive || isNowPlaying
|
|
const isCurrentlyPlaying = isNowPlaying && isPlaying
|
|
|
|
const buttonClassName = cn(
|
|
"absolute size-8 opacity-0 transition-opacity group-hover:opacity-100",
|
|
showPlay && "opacity-100",
|
|
inRange
|
|
? "text-primary-foreground hover:bg-white/15 hover:text-primary-foreground"
|
|
: "text-primary hover:text-primary"
|
|
)
|
|
|
|
const handleClick = (event: MouseEvent) => {
|
|
event.stopPropagation()
|
|
if (isNowPlaying) {
|
|
setIsPlaying(!isPlaying)
|
|
return
|
|
}
|
|
onPlay()
|
|
}
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
"relative flex w-8 shrink-0 items-center justify-center",
|
|
inRange ? "text-primary-foreground" : "text-muted-foreground"
|
|
)}
|
|
>
|
|
<span
|
|
className={cn(
|
|
"tabular-nums transition-opacity group-hover:opacity-0",
|
|
showPlay && "opacity-0"
|
|
)}
|
|
>
|
|
{displayTrack}
|
|
</span>
|
|
{isNowPlaying ? (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className={buttonClassName}
|
|
aria-label={isCurrentlyPlaying ? `Pause ${title}` : `Play ${title}`}
|
|
onClick={handleClick}
|
|
>
|
|
<span className="absolute inset-0 flex items-center justify-center transition-opacity group-hover:opacity-0">
|
|
{isCurrentlyPlaying ? (
|
|
<AudioLines className="size-4" />
|
|
) : (
|
|
<Play className="size-4 fill-current" />
|
|
)}
|
|
</span>
|
|
<span className="absolute inset-0 flex items-center justify-center opacity-0 transition-opacity group-hover:opacity-100">
|
|
{isCurrentlyPlaying ? (
|
|
<Pause className="size-4" />
|
|
) : (
|
|
<Play className="size-4 fill-current" />
|
|
)}
|
|
</span>
|
|
</Button>
|
|
) : (
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className={buttonClassName}
|
|
aria-label={`Play ${title}`}
|
|
onClick={handleClick}
|
|
>
|
|
<Play className="size-4 fill-current" />
|
|
</Button>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|