import React, { useState, useRef, useEffect } from 'react'; import { motion, AnimatePresence } from 'motion/react'; interface VoiceRecorderProps { onTranscription: (text: string) => void; isProcessing: boolean; } export const VoiceRecorder: React.FC = ({ onTranscription, isProcessing }) => { const [isRecording, setIsRecording] = useState(false); const [recordingTime, setRecordingTime] = useState(0); const mediaRecorderRef = useRef(null); const chunksRef = useRef([]); const timerRef = useRef(null); useEffect(() => { if (isRecording) { timerRef.current = setInterval(() => { setRecordingTime(prev => prev + 1); }, 1000); } else { if (timerRef.current) clearInterval(timerRef.current); setRecordingTime(0); } return () => { if (timerRef.current) clearInterval(timerRef.current); }; }, [isRecording]); const startRecording = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); const mediaRecorder = new MediaRecorder(stream); mediaRecorderRef.current = mediaRecorder; chunksRef.current = []; mediaRecorder.ondataavailable = (e) => { if (e.data.size > 0) { chunksRef.current.push(e.data); } }; mediaRecorder.onstop = async () => { const audioBlob = new Blob(chunksRef.current, { type: 'audio/webm' }); const reader = new FileReader(); reader.onloadend = () => { const base64Audio = (reader.result as string).split(',')[1]; handleTranscription(base64Audio); }; reader.readAsDataURL(audioBlob); // Stop all tracks stream.getTracks().forEach(track => track.stop()); }; mediaRecorder.start(); setIsRecording(true); } catch (err) { console.error("Microphone access denied:", err); alert("Mikrofon erişimi reddedildi."); } }; const stopRecording = () => { if (mediaRecorderRef.current && isRecording) { mediaRecorderRef.current.stop(); setIsRecording(false); } }; const handleTranscription = async (base64Audio: string) => { // We'll use Gemini to transcribe. // Since we don't have a direct transcription API here, // we'll send it as a prompt with audio data. try { // This will be handled in App.tsx or a service onTranscription(base64Audio); } catch (error) { console.error("Transcription failed:", error); } }; const formatTime = (seconds: number) => { const mins = Math.floor(seconds / 60); const secs = seconds % 60; return `${mins}:${secs.toString().padStart(2, '0')}`; }; return (
{isRecording && (
{formatTime(recordingTime)}
)}
); };