File size: 3,662 Bytes
3dee94a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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>
  )
}