Spaces:
Running
Running
File size: 1,502 Bytes
eb854b6 02398a6 eb854b6 02398a6 eb854b6 02398a6 eb854b6 02398a6 eb854b6 02398a6 eb854b6 02398a6 eb854b6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | ```javascript
import { useState } from 'react'
import { Send, Loader } from 'react-feather'
export default function ChatInterface({ messages, onSend, isLoading }) {
const [input, setInput] = useState('')
const handleSubmit = (e) => {
e.preventDefault()
if (!input.trim()) return
onSend(input)
setInput('')
}
return (
<div className="flex flex-col h-full">
<div className="flex-1 overflow-y-auto p-4">
{messages.map((msg, i) => (
<div key={i} className={`mb-4 ${msg.role === 'user' ? 'text-right' : 'text-left'}`}>
<div className={`inline-block p-3 rounded-lg ${msg.role === 'user'
? 'bg-blue-500 text-white'
: 'bg-gray-200 text-gray-800'}`}>
{msg.content}
</div>
</div>
))}
</div>
<form onSubmit={handleSubmit} className="p-4 border-t">
<div className="flex">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
className="flex-1 p-2 border rounded-l-lg focus:outline-none"
placeholder="Type your message..."
/>
<button
type="submit"
disabled={isLoading}
className="bg-blue-500 text-white p-2 rounded-r-lg hover:bg-blue-600 disabled:bg-blue-300">
{isLoading ? <Loader className="animate-spin" /> : <Send />}
</button>
</div>
</form>
</div>
)
}
``` |