import React, { useEffect, useRef } from 'react'; import { LyricLine } from '../types'; interface LyricsPanelProps { lyrics: LyricLine[]; currentTime: number; } const LyricsPanel: React.FC = ({ lyrics, currentTime }) => { const containerRef = useRef(null); // Find the index of the last line that has started const activeIndex = lyrics.reduce((acc, line, index) => { if (line.time <= currentTime) { return index; } return acc; }, -1); useEffect(() => { if (activeIndex !== -1 && containerRef.current) { const activeElement = containerRef.current.children[activeIndex] as HTMLElement; if (activeElement) { // Calculate scroll position to center the active element // Using manual calculation is often smoother/more predictable than scrollIntoView for this specific "huge padding" case const containerHeight = containerRef.current.clientHeight; const elementTop = activeElement.offsetTop; const elementHeight = activeElement.clientHeight; containerRef.current.scrollTo({ top: elementTop - containerHeight / 2 + elementHeight / 2, behavior: 'smooth' }); } } }, [activeIndex]); if (lyrics.length === 0) { return (

No lyrics available for this track.

); } return (
{lyrics.map((line, index) => { const isActive = index === activeIndex; return (
{line.text}
); })}
); }; export default LyricsPanel;