Files
otunes/apps/web/components/player-bar.tsx
T
joshuafhiggins 9da758b196 perf: optimize app rendering and data loading (#18)
- Virtualize mobile lists and defer queue loading
- Split playback state from actions to reduce rerenders
- Skip invalid or unnecessary Convex queries

Reviewed-on: #18
2026-09-05 16:35:37 -04:00

229 lines
6.8 KiB
TypeScript

"use client"
import * as React from "react"
import Image from "next/image"
import {
ListMusic,
Music,
Pause,
Play,
Repeat,
Repeat1,
Shuffle,
SkipBack,
SkipForward,
Volume2,
} from "lucide-react"
import { PlaybackSongMeta } from "@/components/playback-song-meta"
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 { albumCoverUrl } from "@/lib/cover-urls"
import { cn } from "@/lib/utils"
const PLACEHOLDER_TIME = "--:--"
function formatDuration(seconds: number): string {
const total = Math.max(0, Math.floor(seconds))
const minutes = Math.floor(total / 60)
const remainingSeconds = total % 60
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
}
export function PlayerBar() {
const {
currentSong,
shuffleEnabled,
repeatEnabled,
queueOpen,
isPlaying,
volume,
queueSongIds,
setIsPlaying,
setVolume,
toggleQueueOpen,
toggleShuffle,
cycleRepeat,
skipNext,
skipPrevious,
} = usePlayback()
const { currentTime, duration, seekTo } = usePlaybackProgress()
const { state, isMobile } = useSidebar()
const [isSeeking, setIsSeeking] = React.useState(false)
const [seekValue, setSeekValue] = React.useState(0)
const hasQueue = queueSongIds.length > 0
const hasActiveSong = Boolean(currentSong)
const sidebarExpanded = !isMobile && state === "expanded"
const displayTime = hasActiveSong ? (isSeeking ? seekValue : currentTime) : 0
const maxDuration = Math.max(duration, 1)
return (
<div
className={cn(
"pointer-events-none fixed bottom-4 z-50 flex justify-center px-4 transition-[left,right] duration-200 ease-linear",
sidebarExpanded ? "left-[16rem] right-0" : "inset-x-0",
queueOpen && !isMobile && "right-72"
)}
>
<div className="pointer-events-auto flex w-full max-w-3xl flex-col gap-2 rounded-xl border bg-card/80 px-3 py-2 shadow-lg backdrop-blur">
<div className="grid grid-cols-[1fr_auto_1fr] items-center gap-2">
<div className="flex items-center justify-start gap-1">
<Button
variant="ghost"
size="icon"
aria-label="Shuffle"
aria-pressed={shuffleEnabled}
className={cn(shuffleEnabled && "text-primary")}
onClick={() => void toggleShuffle()}
>
<Shuffle />
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Previous"
disabled={!hasQueue}
onClick={() => void skipPrevious()}
>
<SkipBack />
</Button>
<Button
variant="ghost"
size="icon"
aria-label={isPlaying ? "Pause" : "Play"}
disabled={!hasQueue}
onClick={() => setIsPlaying(!isPlaying)}
>
{isPlaying ? <Pause /> : <Play className="fill-current" />}
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Next"
disabled={!hasQueue}
onClick={() => void skipNext()}
>
<SkipForward />
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Repeat"
aria-pressed={repeatEnabled !== "none"}
className={cn(repeatEnabled !== "none" && "text-primary")}
onClick={() => void cycleRepeat()}
>
{repeatEnabled === "once" ? <Repeat1 /> : <Repeat />}
</Button>
</div>
<div className="flex h-10 min-w-0 items-center justify-center gap-3 px-2">
{currentSong ? (
<>
<div className="relative size-8 shrink-0 overflow-hidden rounded">
<Image
src={albumCoverUrl(currentSong.album.id)}
alt={currentSong.album.title}
fill
sizes="32px"
className="object-cover"
/>
</div>
<div className="flex h-10 min-w-0 max-w-48 flex-col justify-center overflow-hidden sm:max-w-64">
<PlaybackSongMeta song={currentSong} />
</div>
</>
) : (
<div className="flex size-8 shrink-0 items-center justify-center">
<Image
src="/logo-monochrome.svg"
alt="oTunes"
width={14}
height={24}
className="h-6 w-auto opacity-85"
/>
</div>
)}
</div>
<div className="flex items-center justify-end gap-1">
<Button variant="ghost" size="icon" aria-label="Lyrics" disabled>
<Music />
</Button>
<Button
variant="ghost"
size="icon"
aria-label="Queue"
aria-pressed={queueOpen}
className={cn(queueOpen && "text-primary")}
onClick={toggleQueueOpen}
>
<ListMusic />
</Button>
<div className="hidden items-center gap-2 sm:flex">
<Volume2 className="size-4 shrink-0 text-muted-foreground" />
<Slider
className="w-20"
min={0}
max={100}
value={[volume]}
onValueChange={([value]) => setVolume(value)}
aria-label="Volume"
/>
</div>
<Button
variant="ghost"
size="icon"
aria-label="Volume"
className="sm:hidden"
>
<Volume2 />
</Button>
</div>
</div>
<div className="flex items-center gap-2 px-1">
<span
className="w-9 shrink-0 text-right text-xs tabular-nums text-muted-foreground"
aria-hidden
>
{hasActiveSong ? formatDuration(displayTime) : PLACEHOLDER_TIME}
</span>
<Slider
className={cn(
"flex-1",
!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"
/>
<span
className="w-9 shrink-0 text-xs tabular-nums text-muted-foreground"
aria-hidden
>
{hasActiveSong ? formatDuration(duration) : PLACEHOLDER_TIME}
</span>
</div>
</div>
</div>
)
}