import { Plazbot } from 'plazbot'
import {
PlazbotProvider,
useChat,
useAgent,
useSession,
PoweredBy,
} from 'plazbot/react'
import { useState } from 'react'
const sdk = new Plazbot({
workspaceId: 'YOUR_WORKSPACE_ID',
apiKey: 'YOUR_API_KEY',
zone: 'LA',
})
function CustomChat() {
const { agent, isLoading: agentLoading } = useAgent()
const sessionId = useSession()
const { messages, isLoading, error, sendMessage, clearMessages, retry } = useChat()
const [input, setInput] = useState('')
const handleSend = async () => {
if (!input.trim()) return
const text = input
setInput('')
await sendMessage(text)
}
if (agentLoading) return <p>Cargando...</p>
return (
<div>
<h2>{agent?.person?.name ?? agent?.name}</h2>
<small>Session: {sessionId.slice(0, 8)}...</small>
<button onClick={clearMessages}>Limpiar</button>
<div>
{messages.map((msg) => (
<div key={msg.id} style={{
textAlign: msg.role === 'user' ? 'right' : 'left',
marginBottom: 8,
}}>
<span style={{
display: 'inline-block',
padding: '8px 12px',
borderRadius: 6,
background: msg.role === 'user' ? '#2D84C7' : '#f3f4f6',
color: msg.role === 'user' ? '#fff' : '#111',
}}>
{msg.content}
</span>
</div>
))}
{isLoading && <p>Escribiendo...</p>}
{error && <p>Error: {error} <button onClick={retry}>Reintentar</button></p>}
</div>
<div style={{ display: 'flex', gap: 8 }}>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleSend()}
placeholder="Escribe aqui..."
/>
<button onClick={handleSend} disabled={isLoading}>Enviar</button>
</div>
<PoweredBy label="Powered by" brandName="Mi Empresa" href="https://miempresa.com" />
</div>
)
}
export default function App() {
return (
<PlazbotProvider sdk={sdk} agentId="YOUR_AGENT_ID">
<CustomChat />
</PlazbotProvider>
)
}