Files
workspace/apps/whatsapp-crm/src/app/page.tsx
T

132 lines
3.8 KiB
TypeScript

'use client'
import { useState, useEffect } from 'react'
import { useRouter } from 'next/navigation'
export default function HomePage() {
const router = useRouter()
const [connected, setConnected] = useState(false)
const [hasExistingSession, setHasExistingSession] = useState(false)
const [syncedContacts, setSyncedContacts] = useState(0)
const [showQr, setShowQr] = useState(false)
const [qrCode, setQrCode] = useState<string | null>(null)
useEffect(() => {
const checkStatus = async () => {
try {
const res = await fetch('/api/whatsapp')
const data = await res.json()
setConnected(data.status === 'ready')
setHasExistingSession(data.isAuthenticated || false)
setSyncedContacts(data.syncedContacts || 0)
setQrCode(data.qrCode)
setShowQr(data.status === 'qr')
} catch (e) {
setConnected(false)
}
}
checkStatus()
const interval = setInterval(checkStatus, 3000)
return () => clearInterval(interval)
}, [])
useEffect(() => {
if (connected) {
router.push('/kanban')
}
}, [connected, router])
useEffect(() => {
if (hasExistingSession && !connected) {
fetch('/api/whatsapp', { method: 'POST' })
}
}, [hasExistingSession, connected])
const connect = async () => {
await fetch('/api/whatsapp', { method: 'POST' })
}
const disconnect = async () => {
await fetch('/api/whatsapp', { method: 'DELETE' })
setConnected(false)
setHasExistingSession(false)
setSyncedContacts(0)
}
return (
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
minHeight: '100vh',
fontFamily: 'system-ui, sans-serif',
padding: 20
}}>
<h1 style={{ fontSize: 32, marginBottom: 16 }}>WhatsApp CRM</h1>
<p style={{ color: '#666', marginBottom: 32 }}>Conecte seu WhatsApp para começar</p>
{showQr && qrCode && (
<div style={{ textAlign: 'center', marginBottom: 24 }}>
<p style={{ marginBottom: 16 }}>Escaneie o QR code com seu WhatsApp:</p>
<img src={`https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=${encodeURIComponent(qrCode)}`} alt="QR Code" style={{ border: '1px solid #ccc', borderRadius: 8 }} />
</div>
)}
{!connected && (
<button
onClick={connect}
style={{
padding: '12px 24px',
fontSize: 16,
background: '#0079bf',
color: 'white',
border: 'none',
borderRadius: 8,
cursor: 'pointer'
}}
>
Conectar WhatsApp
</button>
)}
{connected && (
<div style={{ textAlign: 'center' }}>
<p style={{ color: 'green', marginBottom: 16 }}>✓ WhatsApp conectado ({syncedContacts} contatos)</p>
<div style={{ display: 'flex', gap: 12 }}>
<a
href="/kanban"
style={{
padding: '12px 24px',
fontSize: 16,
background: '#0079bf',
color: 'white',
border: 'none',
borderRadius: 8,
textDecoration: 'none',
display: 'inline-block'
}}
>
Acessar Kanban
</a>
<button
onClick={disconnect}
style={{
padding: '12px 24px',
fontSize: 16,
background: '#dc3545',
color: 'white',
border: 'none',
borderRadius: 8,
cursor: 'pointer'
}}
>
Desconectar
</button>
</div>
</div>
)}
</div>
)
}