import { useRef, useEffect } from 'react' import { toRomanNumeral } from '../lib/theory' const HISTORY_SHOWN = 8 function findLoopPosition(chordHistory, progression) { if (!progression?.length || !chordHistory.length) return -1 const last = chordHistory[chordHistory.length - 1] for (let p = progression.length - 1; p >= 0; p--) { if (progression[p] !== last) continue let match = true for (let i = 1; i < Math.min(p + 1, chordHistory.length); i++) { if (progression[p - i] !== chordHistory[chordHistory.length - 1 - i]) { match = false; break } } if (match) return p } return progression.indexOf(last) } export default function ProgressionBanner({ chordHistory, keyInfo, detectedProgression, currentChord }) { const { root, mode, confidence } = keyInfo ?? {} const visible = chordHistory.slice(-HISTORY_SHOWN) const current = visible[visible.length - 1] const loopPos = findLoopPosition(chordHistory, detectedProgression) const currentRN = root && current ? toRomanNumeral(current, root, mode) : '' const currentRef = useRef(null) const prevChord = useRef(null) useEffect(() => { if (current && current !== prevChord.current && currentRef.current) { currentRef.current.animate( [{ opacity: 0, transform: 'scale(0.85)' }, { opacity: 1, transform: 'scale(1)' }], { duration: 200, easing: 'ease-out', fill: 'forwards' } ) prevChord.current = current } }, [current]) return (
Start listening…
) : (Now Playing
Play a chord
)}