Files
otunes/apps/web/components/lyrics-panel.tsx
T

165 lines
4.3 KiB
TypeScript

"use client"
import * as React from "react"
import {
useIsPlaying,
usePlayback,
usePlaybackProgress,
} from "@/components/playback-provider"
import { useLyrics, useLyricsTime } from "@/hooks/use-lyrics"
import { cn } from "@/lib/utils"
import {
activeLineIndex,
type LyricLine,
type Lyrics,
} from "@otunes/convex/client/lyrics"
/** How long a manual scroll keeps the panel from following the song. */
const MANUAL_SCROLL_HOLD_MS = 3000
/** Lyrics beside the page, in the queue's place. Synced lines follow playback. */
export function LyricsPanel() {
const { currentSong } = usePlayback()
const lyrics = useLyrics(currentSong?.id ?? null)
return (
<aside
aria-label="Lyrics"
className={cn(
"flex h-svh w-72 shrink-0 flex-col border-l bg-background",
"max-md:fixed max-md:inset-y-0 max-md:right-0 max-md:z-20 max-md:shadow-xl"
)}
>
{lyrics === null ? (
<p className="m-auto text-sm text-muted-foreground">No lyrics</p>
) : lyrics ? (
<LyricsLines key={currentSong?.id} lyrics={lyrics} />
) : null}
</aside>
)
}
function LyricsLines({ lyrics }: { lyrics: Lyrics }) {
const { seekTo, currentTime } = usePlaybackProgress()
const isPlaying = useIsPlaying()
const { setIsPlaying } = usePlayback()
const time = useLyricsTime(lyrics, currentTime, isPlaying)
const active = activeLineIndex(lyrics, time)
const scrollRef = React.useRef<HTMLDivElement>(null)
const manualScrollAt = React.useRef(0)
const synced = lyrics.synced !== "none"
React.useEffect(() => {
const container = scrollRef.current
const line = container?.querySelector<HTMLElement>(
`[data-line="${Math.max(active, 0)}"]`
)
if (
!container ||
!line ||
!synced ||
Date.now() - manualScrollAt.current < MANUAL_SCROLL_HOLD_MS
) {
return
}
container.scrollTo({
top: line.offsetTop - container.clientHeight * 0.25,
behavior: "smooth",
})
}, [active, synced])
const playFrom = React.useCallback(
(seconds: number) => {
seekTo(seconds)
setIsPlaying(true)
},
[seekTo, setIsPlaying]
)
const holdScroll = () => {
manualScrollAt.current = Date.now()
}
return (
<div
ref={scrollRef}
onWheel={holdScroll}
onTouchMove={holdScroll}
className="relative min-h-0 flex-1 overflow-y-auto px-6 pt-[25svh] pb-[60svh]"
>
{lyrics.lines.map((line, index) =>
synced ? (
<SyncedLine
key={index}
index={index}
line={line}
state={
index === active ? "active" : index < active ? "past" : "next"
}
time={index === active ? time : null}
onSelect={playFrom}
/>
) : (
<p
key={index}
className={cn(
"text-lg leading-snug font-semibold text-foreground/85",
line.text === "" && "h-4"
)}
>
{line.text}
</p>
)
)}
</div>
)
}
/**
* One timed line. Only the active line gets the playback time, so memoized
* lines elsewhere skip re-rendering as the song plays.
*/
const SyncedLine = React.memo(function SyncedLine({
index,
line,
state,
time,
onSelect,
}: {
index: number
line: LyricLine
state: "past" | "active" | "next"
time: number | null
onSelect: (seconds: number) => void
}) {
const active = state === "active"
return (
<button
type="button"
data-line={index}
onClick={() => onSelect(line.start ?? 0)}
className={cn(
"-mx-2 my-1 block w-[calc(100%+1rem)] rounded-md px-2 py-1.5 text-left text-[22px] leading-tight font-bold transition-colors duration-300 hover:bg-foreground/5",
active && !line.words ? "text-foreground" : "text-foreground/30"
)}
>
{active && line.words
? line.words.map((word, wordIndex) => (
<span
key={wordIndex}
className={cn(
"transition-colors duration-200",
time !== null && time >= word.start
? "text-foreground"
: "text-foreground/45"
)}
>
{word.text}
</span>
))
: line.text}
</button>
)
})