import React, { useMemo, useRef, useEffect, useState } from 'react' import { transposeChord } from './ChordDisplay' const VISIBLE_SIDES = 3 // number of chords to show on each side function LiveChordView({ chordsData, currentTime, isPlaying, semitones = 0, onSeek }) { const containerRef = useRef(null) const [prevIndex, setPrevIndex] = useState(-1) const chords = chordsData?.chords || [] const hasChords = chords.length > 0 // Binary search for active chord const activeIndex = useMemo(() => { if (!hasChords) return -1 let lo = 0, hi = chords.length - 1 while (lo <= hi) { const mid = (lo + hi) >> 1 if (currentTime < chords[mid].start_time) hi = mid - 1 else if (currentTime >= chords[mid].end_time) lo = mid + 1 else return mid } return -1 }, [chords, currentTime, hasChords]) // Track previous index for transition direction useEffect(() => { if (activeIndex !== -1 && activeIndex !== prevIndex) { setPrevIndex(activeIndex) } }, [activeIndex, prevIndex]) if (!chordsData || !hasChords) return null // Progress within the current chord (0-1) const chordProgress = activeIndex >= 0 ? (() => { const c = chords[activeIndex] const span = c.end_time - c.start_time return span > 0 ? Math.max(0, Math.min(1, (currentTime - c.start_time) / span)) : 0 })() : 0 // Build the visible window of chords const windowStart = Math.max(0, (activeIndex >= 0 ? activeIndex : 0) - VISIBLE_SIDES) const windowEnd = Math.min(chords.length - 1, (activeIndex >= 0 ? activeIndex : 0) + VISIBLE_SIDES) const visibleChords = [] for (let i = windowStart; i <= windowEnd; i++) { visibleChords.push({ ...chords[i], index: i }) } return (
Now Playing
)}