import React, { useState, useRef } from 'react'; import { Mic, Play, Square, StopCircle } from 'lucide-react'; import { useChat } from '../context/ChatContext'; const FrenchChat = () => { const { messages, addMessage } = useChat(); const [isRecording, setIsRecording] = useState(false); const [isPlaying, setIsPlaying] = useState(false); const mediaRecorderRef = useRef(null); const chunksRef = useRef([]); const recognitionRef = useRef(null); const startRecording = async () => { try { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; const recognition = new SpeechRecognition(); recognitionRef.current = recognition; recognition.lang = 'fr-FR'; recognition.continuous = true; recognition.interimResults = true; recognition.onresult = (event) => { const transcript = Array.from(event.results) .map(result => result[0].transcript) .join(''); if (event.results[0].isFinal) { const newMessage = { id: Date.now(), text: transcript, isFrench: true, timestamp: new Date(), }; addMessage(newMessage); } }; recognition.start(); setIsRecording(true); const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); const mediaRecorder = new MediaRecorder(stream); mediaRecorderRef.current = mediaRecorder; chunksRef.current = []; mediaRecorder.ondataavailable = (e) => { chunksRef.current.push(e.data); }; mediaRecorder.start(); } catch (err) { console.error('Error accessing microphone:', err); } }; const stopRecording = () => { if (recognitionRef.current) { recognitionRef.current.stop(); } if (mediaRecorderRef.current && isRecording) { mediaRecorderRef.current.stop(); setIsRecording(false); mediaRecorderRef.current.stream.getTracks().forEach(track => track.stop()); } }; const speakText = (text: string) => { if ('speechSynthesis' in window) { window.speechSynthesis.cancel(); // Arrête toute lecture en cours setIsPlaying(true); const utterance = new SpeechSynthesisUtterance(text); utterance.lang = 'fr-FR'; utterance.rate = 0.9; utterance.pitch = 1.0; utterance.volume = 1.0; utterance.onend = () => { setIsPlaying(false); }; utterance.onerror = () => { setIsPlaying(false); }; window.speechSynthesis.speak(utterance); } }; const stopSpeaking = () => { window.speechSynthesis.cancel(); setIsPlaying(false); }; return (
{messages.map((message) => (

{message.isFrench ? message.text : message.translatedText}

{!message.isFrench && ( )}
))}

Cliquer puis parler

); }; export default FrenchChat;