'use client'; import { useChat } from 'ai'; import { Container, ScrollArea, Paper, Group, TextInput, Button, Stack, Text, Box } from '@mantine/core'; import { useEffect, useRef } from 'react'; import { MicrophoneRecorder } from './MicrophoneRecorder'; export function ChatInterface() { const viewport = useRef(null); const { messages, input, handleInputChange, handleSubmit, setInput, isLoading, } = useChat({ api: '/api/chat', }); // Auto-scroll to bottom when new messages arrive useEffect(() => { if (viewport.current) { viewport.current.scrollTo({ top: viewport.current.scrollHeight, behavior: 'smooth', }); } }, [messages]); return ( {/* Chat messages area */} {messages.length === 0 && ( Start a conversation by typing or speaking... )} {messages.map((message) => ( {message.content} ))} {/* Input area */}
{/* Microphone Recorder */} { // Update the input field in real-time setInput(transcript); }} onTranscriptFinalized={(transcript) => { // Set the input and submit setInput(transcript); // Trigger form submission setTimeout(() => { const form = document.querySelector('form'); if (form) { form.requestSubmit(); } }, 100); }} />
); }