Spaces:
Build error
Build error
| import React, { useState, useRef, useEffect } from 'react'; | |
| const ChatScreen = ({ | |
| messages, | |
| handleSendMessage, | |
| handleVoiceInput, | |
| handleLogout, | |
| setCurrentPage | |
| }) => { | |
| const [inputText, setInputText] = useState(''); | |
| const [isRecording, setIsRecording] = useState(false); | |
| const messagesEndRef = useRef(null); | |
| const scrollToBottom = () => { | |
| messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); | |
| }; | |
| useEffect(() => { | |
| scrollToBottom(); | |
| }, [messages]); | |
| const handleSubmit = (e) => { | |
| e.preventDefault(); | |
| if (inputText.trim()) { | |
| handleSendMessage(inputText); | |
| setInputText(''); | |
| } | |
| }; | |
| const handleVoiceToggle = () => { | |
| if (isRecording) { | |
| handleVoiceInput('stop'); | |
| setIsRecording(false); | |
| } else { | |
| handleVoiceInput('start'); | |
| setIsRecording(true); | |
| } | |
| }; | |
| return ( | |
| <div className="min-h-screen bg-gray-50 flex flex-col"> | |
| {/* Header */} | |
| <header className="bg-white shadow-sm border-b border-gray-200 px-4 py-4"> | |
| <div className="flex items-center justify-between"> | |
| <div> | |
| <h1 className="text-xl font-semibold text-gray-800">Hugo Chat</h1> | |
| <p className="text-sm text-gray-500">Asistente virtual</p> | |
| </div> | |
| <div className="flex items-center space-x-3"> | |
| <button | |
| onClick={() => setCurrentPage('providers')} | |
| className="px-4 py-2 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" | |
| > | |
| Ver Proveedores | |
| </button> | |
| <button | |
| onClick={handleLogout} | |
| className="px-4 py-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors" | |
| > | |
| Salir | |
| </button> | |
| </div> | |
| </div> | |
| </header> | |
| {/* Messages Area */} | |
| <div className="flex-1 overflow-y-auto p-4 custom-scrollbar"> | |
| {messages.length === 0 ? ( | |
| <div className="text-center py-12"> | |
| <div className="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-4"> | |
| <svg className="w-8 h-8 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" /> | |
| </svg> | |
| </div> | |
| <h3 className="text-lg font-medium text-gray-700 mb-2">¡Hola! Soy Hugo</h3> | |
| <p className="text-gray-500">¿En qué puedo ayudarte hoy?</p> | |
| </div> | |
| ) : ( | |
| <div className="space-y-4"> | |
| {messages.map((message, index) => ( | |
| <div | |
| key={index} | |
| className={`flex ${message.isUser ? 'justify-end' : 'justify-start'}`} | |
| > | |
| <div | |
| className={`max-w-xs lg:max-w-md px-4 py-3 rounded-2xl ${ | |
| message.isUser | |
| ? 'bg-blue-600 text-white' | |
| : 'bg-white border border-gray-200 text-gray-800' | |
| }`} | |
| > | |
| <p className="text-sm">{message.text}</p> | |
| <p className={`text-xs mt-1 ${message.isUser ? 'text-blue-100' : 'text-gray-500'}`}> | |
| {message.timestamp} | |
| </p> | |
| </div> | |
| </div> | |
| ))} | |
| <div ref={messagesEndRef} /> | |
| </div> | |
| )} | |
| </div> | |
| {/* Input Area */} | |
| <div className="bg-white border-t border-gray-200 p-4"> | |
| <form onSubmit={handleSubmit} className="flex items-center space-x-3"> | |
| <div className="flex-1 relative"> | |
| <input | |
| type="text" | |
| value={inputText} | |
| onChange={(e) => setInputText(e.target.value)} | |
| placeholder="Escribe tu mensaje..." | |
| className="w-full px-4 py-3 border border-gray-300 rounded-2xl focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none" | |
| /> | |
| </div> | |
| <button | |
| type="button" | |
| onClick={handleVoiceToggle} | |
| className={`p-3 rounded-full transition-all ${ | |
| isRecording | |
| ? 'bg-red-500 hover:bg-red-600 text-white animate-pulse' | |
| : 'bg-gray-100 hover:bg-gray-200 text-gray-600' | |
| }`} | |
| > | |
| <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z" /> | |
| </svg> | |
| </button> | |
| <button | |
| type="submit" | |
| disabled={!inputText.trim()} | |
| className="p-3 bg-blue-600 hover:bg-blue-700 disabled:bg-gray-300 text-white rounded-full transition-colors" | |
| > | |
| <svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"> | |
| <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8" /> | |
| </svg> | |
| </button> | |
| </form> | |
| <div className="mt-2 text-xs text-gray-400 text-center"> | |
| {/* TODO: Llamar a la Firebase Cloud Function para la clasificación de Hugo */} | |
| </div> | |
| </div> | |
| </div> | |
| ); | |
| }; | |
| export default ChatScreen; |