TARSTHENEXT / src /pages /dashboard.jsx
CPISTO's picture
Upload 6721 files
3dee94a verified
Raw History Blame Contribute Delete
3.66 kB
import { useState, useRef, useEffect } from 'react'
import { sendToTARS } from '../services/tarsAPI'
import { getCurrentUser } from '../utils/sessionManager'
import VoiceButton from '../components/VoiceButton'
import LogoTARS from '../components/LogoTARS'
import { FaUserCircle, FaRobot } from 'react-icons/fa'
export default function Dashboard() {
const [input, setInput] = useState('')
const [messages, setMessages] = useState([])
const messagesEndRef = useRef(null)
const user = getCurrentUser()
const handleSend = async () => {
if (!input.trim()) return
const msg = { from: 'user', text: input }
setMessages(prev => [...prev, msg])
setInput('')
const reply = await sendToTARS(input)
setMessages(prev => [...prev, { from: 'tars', text: reply }])
}
const handleKeyDown = e => {
if (e.key === 'Enter') {
e.preventDefault()
handleSend()
}
}
const handleClear = () => setMessages([])
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
}, [messages])
return (
<div className="flex flex-col items-center justify-between h-screen bg-zinc-900 text-white font-sans">
{/* Header */}
<header className="w-full max-w-4xl px-6 py-4 flex items-center justify-between border-b border-zinc-700 bg-zinc-800">
<LogoTARS />
<div className="flex items-center gap-4">
<span className="text-sm">
Bem-vindo, <strong>{user?.username}</strong>
</span>
<button
onClick={handleClear}
className="px-3 py-1 text-sm bg-red-600 hover:bg-red-700 text-white rounded"
>
Limpar
</button>
</div>
</header>
{/* Chat */}
<main className="w-full max-w-4xl flex-1 overflow-y-auto px-6 py-4 space-y-4 bg-zinc-900">
{messages.map((msg, i) => (
<div
key={i}
className={`flex items-end gap-2 ${
msg.from === 'user' ? 'justify-end' : 'justify-start'
}`}
>
{msg.from === 'tars' && (
<FaRobot className="text-2xl text-blue-400 shrink-0" />
)}
<div
className={`p-3 rounded-2xl text-sm whitespace-pre-wrap break-words max-w-[75%] leading-relaxed animate-fadeIn ${
msg.from === 'user'
? 'bg-blue-600 text-white rounded-br-none'
: 'bg-zinc-700 text-white rounded-bl-none'
}`}
>
{msg.text}
</div>
{msg.from === 'user' && (
<FaUserCircle className="text-2xl text-gray-300 shrink-0" />
)}
</div>
))}
<div ref={messagesEndRef} />
</main>
{/* Footer */}
<footer className="w-full max-w-4xl px-6 py-4 bg-zinc-800 border-t border-zinc-700 flex items-center gap-3">
<input
className="flex-1 p-3 rounded-md bg-zinc-700 border border-zinc-600 text-white placeholder-gray-400 outline-none focus:ring-2 focus:ring-blue-500"
value={input}
onChange={e => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Digite sua mensagem..."
/>
<button
onClick={handleSend}
className="bg-blue-600 hover:bg-blue-700 hover:scale-105 transition-transform duration-150 text-white px-5 py-2 rounded-md font-medium"
>
Enviar
</button>
<VoiceButton onResult={setInput} />
</footer>
</div>
)
}