import React, { useRef, useState, useEffect } from 'react' export default function GenerationOutput({ sessionId, generationResult, onRegenerate, onUseAsSource, onPlaySection, }) { const audioRef = useRef(null) const [isPlaying, setIsPlaying] = useState(false) const [currentTime, setCurrentTime] = useState(0) const [duration, setDuration] = useState(0) const [abSource, setAbSource] = useState('generated') const [downloadFormat, setDownloadFormat] = useState('wav') const [audioSrc, setAudioSrc] = useState(null) const [loadError, setLoadError] = useState(false) // Only set the audio src when we get a NEW generation result useEffect(() => { if (generationResult && sessionId) { setAudioSrc(`/api/stem/${sessionId}/_continuation?format=wav&v=${Date.now()}`) setIsPlaying(false) setCurrentTime(0) setDuration(0) setAbSource('generated') setLoadError(false) } else { setAudioSrc(null) } }, [generationResult, sessionId]) const handleTimeUpdate = () => { if (audioRef.current) setCurrentTime(audioRef.current.currentTime) } const handleLoadedMetadata = () => { if (audioRef.current) setDuration(audioRef.current.duration) } const handleEnded = () => setIsPlaying(false) // Stop retrying on error — clear the src so the browser doesn't loop const handleError = () => { setLoadError(true) setIsPlaying(false) } const togglePlay = () => { if (!audioRef.current || loadError) return if (isPlaying) { audioRef.current.pause() setIsPlaying(false) } else { audioRef.current.play().catch(() => setIsPlaying(false)) setIsPlaying(true) } } const handleSeek = (e) => { if (!audioRef.current || !duration) return const rect = e.currentTarget.getBoundingClientRect() const pct = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width)) audioRef.current.currentTime = pct * duration } const formatTime = (t) => { const m = Math.floor(t / 60) const s = Math.floor(t % 60).toString().padStart(2, '0') return `${m}:${s}` } const handleDownload = () => { if (!sessionId) return const url = `/api/stem/${sessionId}/_continuation?format=${downloadFormat}` const a = document.createElement('a') a.href = url a.download = `generation.${downloadFormat}` a.click() } // Don't render anything until we have a generation result if (!generationResult) return null return (
{/* Only mount