Files
otunes/apps/web/components/player-bar.tsx
T

276 lines
8.9 KiB
TypeScript

"use client"
import * as React from "react"
import Image from "next/image"
import Link from "next/link"
import {
ListMusic,
MessageSquareQuote,
Pause,
Play,
Repeat,
Repeat1,
Shuffle,
SkipBack,
SkipForward,
Volume2,
} from "lucide-react"
import { AudioFormatBadge } from "@/components/audio-format-badge"
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover"
import {
usePlayback,
usePlaybackProgress,
} from "@/components/playback-provider"
import { Button } from "@/components/ui/button"
import { Slider } from "@/components/ui/slider"
import { useSidebar } from "@/components/ui/sidebar"
import { useLyrics } from "@/hooks/use-lyrics"
import { albumCoverUrl } from "@/lib/cover-urls"
import { cn } from "@/lib/utils"
import { formatDuration } from "@/lib/format-duration"
function PlayerSeekBar({ hasActiveSong }: { hasActiveSong: boolean }) {
const { currentTime, duration, seekTo } = usePlaybackProgress()
const [isSeeking, setIsSeeking] = React.useState(false)
const [seekValue, setSeekValue] = React.useState(0)
const displayTime = hasActiveSong ? (isSeeking ? seekValue : currentTime) : 0
const maxDuration = Math.max(duration, 1)
return (
<Slider
className={cn(
"h-3 flex-1 [&_[data-slot=slider-range]]:bg-current/80 [&_[data-slot=slider-thumb]]:size-2 [&_[data-slot=slider-thumb]]:border-current [&_[data-slot=slider-thumb]]:bg-current [&_[data-slot=slider-thumb]]:opacity-0 focus-within:[&_[data-slot=slider-thumb]]:opacity-100 hover:[&_[data-slot=slider-thumb]]:opacity-100 [&_[data-slot=slider-track]]:h-[3px] [&_[data-slot=slider-track]]:bg-current/30 md:[&_[data-slot=slider-track]]:h-0.5",
!hasActiveSong && "[&_[data-slot=slider-thumb]]:hidden"
)}
min={0}
max={maxDuration}
step={0.1}
value={[displayTime]}
disabled={!hasActiveSong}
onValueChange={([value]) => {
setIsSeeking(true)
setSeekValue(value)
}}
onValueCommit={([value]) => {
seekTo(value)
setIsSeeking(false)
}}
aria-label="Seek"
aria-valuetext={`${formatDuration(displayTime)} of ${formatDuration(duration)}`}
/>
)
}
export function PlayerBar() {
const {
currentSong,
currentFormat,
shuffleEnabled,
repeatEnabled,
queueOpen,
lyricsOpen,
isPlaying,
volume,
queueSongIds,
setIsPlaying,
setVolume,
toggleQueueOpen,
toggleLyricsOpen,
toggleShuffle,
cycleRepeat,
skipNext,
skipPrevious,
} = usePlayback()
const { state, isMobile } = useSidebar()
const lyrics = useLyrics(currentSong?.id ?? null)
const hasQueue = queueSongIds.length > 0
const sidebarExpanded = !isMobile && state === "expanded"
return (
<div
className={cn(
"@container pointer-events-none fixed bottom-4 z-50 flex justify-center px-4 transition-[left,right] duration-200 ease-linear",
sidebarExpanded ? "right-0 left-[16rem]" : "inset-x-0",
(queueOpen || lyricsOpen) && !isMobile && "right-72"
)}
>
<div className="pointer-events-auto flex w-full max-w-[40rem] items-center gap-2 rounded-full border border-foreground/20 bg-(--player-background) px-3 py-2 text-card-foreground shadow-lg [--player-background:var(--card)] md:gap-3 md:py-1.5 dark:border-white/20 dark:text-white dark:[--player-background:#303033]">
<div className="flex shrink-0 items-center gap-0 md:gap-0.5 [&_button]:size-7 [&_svg]:size-4">
<Button
variant="ghost"
size="icon"
aria-label="Shuffle"
aria-pressed={shuffleEnabled}
className={cn(
"hidden @min-[640px]:inline-flex",
shuffleEnabled
? "text-card-foreground dark:text-white"
: "text-muted-foreground dark:text-white/40"
)}
onClick={() => void toggleShuffle()}
>
<Shuffle className="size-4" />
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Previous"
disabled={!hasQueue}
onClick={() => void skipPrevious()}
>
<SkipBack className="size-4 fill-current" />
</Button>
<Button
variant="ghost"
size="icon"
aria-label={isPlaying ? "Pause" : "Play"}
disabled={!hasQueue}
onClick={() => setIsPlaying(!isPlaying)}
>
{isPlaying ? (
<Pause className="size-5! fill-current" />
) : (
<Play className="size-5! fill-current" />
)}
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Next"
disabled={!hasQueue}
onClick={() => void skipNext()}
>
<SkipForward className="size-4 fill-current" />
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Repeat"
aria-pressed={repeatEnabled !== "none"}
className={cn(
"hidden @min-[640px]:inline-flex",
repeatEnabled !== "none"
? "text-card-foreground dark:text-white"
: "text-muted-foreground dark:text-white/40"
)}
onClick={() => void cycleRepeat()}
>
{repeatEnabled === "once" ? (
<Repeat1 className="size-4" />
) : (
<Repeat className="size-4" />
)}
</Button>
</div>
<div className="relative flex h-11 min-w-0 flex-1 items-center gap-2 pb-2 md:h-9 md:pb-1">
{currentSong ? (
<>
<div className="relative size-9 shrink-0 overflow-hidden rounded md:size-8">
<Image
src={albumCoverUrl(currentSong.album.id)}
alt={currentSong.album.title}
fill
sizes="36px"
className="object-cover"
/>
</div>
<div className="min-w-0 flex-1">
<Link
href={`/album/${currentSong.album.id}`}
className="block truncate text-sm font-semibold hover:underline md:text-[13px] md:leading-4"
>
{currentSong.title}
</Link>
<p className="truncate text-xs">
<Link
href={`/artist/${currentSong.artist.id}`}
className="hover:underline"
>
{currentSong.artist.name}
</Link>
<span aria-hidden> · </span>
<Link
href={`/album/${currentSong.album.id}`}
className="hover:underline"
>
{currentSong.album.title}
</Link>
</p>
</div>
{currentFormat ? (
<AudioFormatBadge
format={currentFormat}
iconOnly
backgroundColor="var(--player-background)"
/>
) : null}
</>
) : (
<Image
src="/logo.png"
alt="oTunes"
width={24}
height={24}
className="mx-auto size-6 opacity-85"
/>
)}
<div className="absolute inset-x-0 -bottom-1">
<PlayerSeekBar hasActiveSong={Boolean(currentSong)} />
</div>
</div>
<div className="flex shrink-0 items-center gap-0 md:gap-1 [&_button]:size-7 [&_svg]:size-4">
<Button
variant="ghost"
size="icon"
aria-label="Lyrics"
aria-pressed={lyricsOpen}
className={cn(
"hidden @min-[640px]:inline-flex",
lyricsOpen && "text-primary"
)}
disabled={!lyrics && !lyricsOpen}
onClick={toggleLyricsOpen}
>
<MessageSquareQuote className="size-4" />
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Queue"
aria-pressed={queueOpen}
className={cn(queueOpen && "text-primary")}
onClick={toggleQueueOpen}
>
<ListMusic className="size-4" />
</Button>
<Popover>
<PopoverTrigger asChild>
<Button variant="ghost" size="icon" aria-label="Volume">
<Volume2 className="size-4" />
</Button>
</PopoverTrigger>
<PopoverContent side="top" align="end" className="w-40">
<Slider
min={0}
max={100}
value={[volume]}
onValueChange={([value]) => setVolume(value)}
aria-label="Volume"
/>
</PopoverContent>
</Popover>
</div>
</div>
</div>
)
}