Spaces:
Runtime error
Runtime error
File size: 4,046 Bytes
b040061 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | import { useState, useEffect, useRef } from 'react';
const BACKEND_URL = import.meta.env.VITE_BACKEND_URL || (import.meta.env.DEV ? 'http://localhost:8000' : '');
export default function BriefPanel({ briefText }) {
const [displayedText, setDisplayedText] = useState('');
const [isPlaying, setIsPlaying] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const audioRef = useRef(null);
useEffect(() => {
if (!briefText) {
setDisplayedText('');
return;
}
// Typewriter effect
setDisplayedText('');
let i = 0;
const interval = setInterval(() => {
if (i < briefText.length) {
setDisplayedText(briefText.slice(0, i + 1));
i++;
} else {
clearInterval(interval);
}
}, 12);
// Auto-launch TTS in parallel
(async () => {
setIsLoading(true);
try {
const res = await fetch(`${BACKEND_URL}/api/tts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: briefText }),
});
if (!res.ok) throw new Error('TTS failed');
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audioRef.current = audio;
audio.onended = () => {
setIsPlaying(false);
URL.revokeObjectURL(url);
};
setIsPlaying(true);
await audio.play();
} catch (err) {
console.error('TTS auto-play error:', err);
} finally {
setIsLoading(false);
}
})();
return () => clearInterval(interval);
}, [briefText]);
const handleSpeak = async () => {
if (isPlaying) {
if (audioRef.current) {
audioRef.current.pause();
audioRef.current.currentTime = 0;
}
setIsPlaying(false);
return;
}
setIsLoading(true);
try {
const res = await fetch(`${BACKEND_URL}/api/tts`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: briefText }),
});
if (!res.ok) throw new Error('TTS failed');
const blob = await res.blob();
const url = URL.createObjectURL(blob);
const audio = new Audio(url);
audioRef.current = audio;
audio.onended = () => {
setIsPlaying(false);
URL.revokeObjectURL(url);
};
setIsPlaying(true);
await audio.play();
} catch (err) {
console.error('TTS error:', err);
} finally {
setIsLoading(false);
}
};
if (!briefText) {
return null;
}
return (
<div style={{
background: 'var(--card-bg)',
padding: '24px',
borderTop: '1px solid var(--border)',
animation: 'fadeIn 0.5s ease-out',
}}>
<div style={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: '16px',
}}>
<h4 style={{ fontSize: '11px', color: 'var(--text-dim)', letterSpacing: '2px' }}>
BRIEF PROACTIF
</h4>
<button
onClick={handleSpeak}
disabled={isLoading}
style={{
background: 'none',
border: '1px solid var(--green)',
color: 'var(--green)',
padding: '6px 16px',
fontFamily: 'inherit',
fontSize: '12px',
cursor: isLoading ? 'wait' : 'pointer',
transition: 'all 0.2s',
opacity: isLoading ? 0.6 : 1,
}}
>
{isLoading ? '\u23F3 Chargement...' : isPlaying ? '\u23F9 Stop' : '\uD83D\uDD0A Play'}
</button>
</div>
<div style={{
fontSize: '14px',
lineHeight: '1.7',
whiteSpace: 'pre-wrap',
color: '#e0e0e0',
}}>
{displayedText}
{displayedText.length < (briefText?.length || 0) && (
<span style={{ animation: 'pulse 0.8s infinite', color: 'var(--green)' }}>|</span>
)}
</div>
</div>
);
}
|