import { useState, useRef } from 'react' import { startRecording, stopRecording, sendAudioToBackend, playAudioBlob } from '../services/audioService' export default function Home() { const [recording, setRecording] = useState(false) const [playing, setPlaying] = useState(false) const [isProcessing, setIsProcessing] = useState(false) const mediaRecorderRef = useRef(null) const audioChunksRef = useRef([]) const audioRef = useRef(null) async function handleStartRecording() { await startRecording(mediaRecorderRef, audioChunksRef, setRecording, handleSendAudio) } function handleStopRecording() { // Immediately stop the recording animation setRecording(false) stopRecording(mediaRecorderRef) } async function handleSendAudio(blob) { try { setIsProcessing(true) const audioBlob = await sendAudioToBackend(blob) playAudioBlob(audioBlob, audioRef, setPlaying) } catch (error) { // Show a more user-friendly error notification const errorDiv = document.createElement('div') errorDiv.className = 'fixed top-4 right-4 bg-red-500 text-white px-6 py-4 rounded-lg shadow-lg z-50 transform transition-all duration-300' errorDiv.textContent = `Error: ${error.message}` document.body.appendChild(errorDiv) setTimeout(() => { errorDiv.style.transform = 'translateX(100%)' setTimeout(() => document.body.removeChild(errorDiv), 300) }, 3000) } finally { setIsProcessing(false) } } return (
{/* Header */}

🎤 Voice Assistant

{/* Main Interface */}
{/* Recording Section */}
{/* Animated rings around button when recording */} {recording && ( <>
)}
{/* Status indicators */}
Recording
Processing
{/* Audio Playback Section */}

🔊 Audio Response

Your AI assistant's response will play here

{/* Instructions */}

💡 How to Use

1️⃣

Press & Hold
Hold the button down while speaking

2️⃣

Or Click Toggle
Click once to start, click again to stop

3️⃣

Listen
Your AI assistant will respond with audio

) }