import React, { useState, useEffect, useRef } from 'react'; import { MessageSquare, Users, Send, QrCode, CheckCircle2, Play, Terminal, LogOut, AlertCircle, Unlock, ShieldCheck, RefreshCw } from 'lucide-react'; type Status = 'DISCONNECTED' | 'INITIALIZING' | 'QR_CODE' | 'CONNECTED' | 'ERROR'; function App() { const [isAuthenticated, setIsAuthenticated] = useState(false); const [passkey, setPasskey] = useState(''); const [loginError, setLoginError] = useState(''); const [isLoggingIn, setIsLoggingIn] = useState(false); const [status, setStatus] = useState('DISCONNECTED'); const [qrCode, setQrCode] = useState(''); const [logs, setLogs] = useState([]); const [phone, setPhone] = useState(''); const [message, setMessage] = useState(''); const [isSending, setIsSending] = useState(false); const [activeTab, setActiveTab] = useState<'direct' | 'group'>('direct'); const [groups, setGroups] = useState([]); const [selectedGroup, setSelectedGroup] = useState(''); const logsEndRef = useRef(null); // Check initial authentication useEffect(() => { const savedPasskey = localStorage.getItem('passkey'); if (savedPasskey) { checkInitialAuth(savedPasskey); } }, []); const checkInitialAuth = async (key: string) => { try { const response = await fetch('/api/status', { headers: { 'x-passkey': key } }); if (response.ok) { setIsAuthenticated(true); } else { localStorage.removeItem('passkey'); } } catch (e) { console.error('Initial auth check failed:', e); } }; const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); if (!passkey) return; setIsLoggingIn(true); setLoginError(''); try { const response = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ passkey }) }); if (response.ok) { localStorage.setItem('passkey', passkey); setIsAuthenticated(true); setLoginError(''); } else { setLoginError('Invalid passkey. Please try again.'); } } catch (err) { setLoginError('Could not connect to server.'); } finally { setIsLoggingIn(false); } }; const handleLogout = () => { localStorage.removeItem('passkey'); setIsAuthenticated(false); setPasskey(''); }; useEffect(() => { if (!isAuthenticated) return; const pollStatus = async () => { const savedPasskey = localStorage.getItem('passkey') || ''; try { const response = await fetch('/api/status', { headers: { 'x-passkey': savedPasskey } }); if (response.status === 401) { handleLogout(); return; } if (response.ok) { const data = await response.json(); setStatus(data.status); setQrCode(data.qrCode); setLogs(data.logs); if (data.status === 'CONNECTED' && groups.length === 0) { fetchGroups(); } } } catch (err) { console.error('Polling error:', err); } }; pollStatus(); const interval = setInterval(pollStatus, 3000); return () => clearInterval(interval); }, [isAuthenticated, groups.length]); const fetchGroups = async () => { const savedPasskey = localStorage.getItem('passkey') || ''; try { const response = await fetch('/api/groups', { headers: { 'x-passkey': savedPasskey } }); if (response.ok) { const data = await response.json(); if (data.groups) setGroups(data.groups); } } catch (err) { console.error('Fetch groups error:', err); } }; const startService = async () => { const savedPasskey = localStorage.getItem('passkey') || ''; try { await fetch('/api/start', { method: 'POST', headers: { 'x-passkey': savedPasskey } }); } catch (err) { console.error('Start service error:', err); } }; const sendMessage = async (e: React.FormEvent) => { e.preventDefault(); setIsSending(true); const savedPasskey = localStorage.getItem('passkey') || ''; const recipient = activeTab === 'direct' ? phone : selectedGroup; try { const response = await fetch('/api/send', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-passkey': savedPasskey }, body: JSON.stringify({ phone: recipient, message, isGroup: activeTab === 'group' }) }); if (response.ok) { setMessage(''); alert('Message sent successfully!'); } else { const data = await response.json(); alert(`Error: ${data.error}`); } } catch (err) { alert(`Network error: ${err}`); } finally { setIsSending(false); } }; useEffect(() => { logsEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [logs]); if (!isAuthenticated) { return (

Restricted Access

Enter your security passkey to manage the WPPConnect server.

setPasskey(e.target.value)} className="w-full px-6 py-4 bg-slate-900/50 border border-slate-700 rounded-2xl focus:ring-4 focus:ring-orange-500/20 focus:border-orange-500 outline-none transition-all text-center text-xl font-mono tracking-widest text-white placeholder:tracking-normal placeholder:font-sans placeholder:text-slate-500" autoFocus /> {loginError && (
{loginError}
)}

Hugging Face Space Security

); } return (

WPPConnect Portal

{status}
{status !== 'CONNECTED' ? (

WhatsApp Offline

Authenticate to enable messaging features.

) : (
{activeTab === 'direct' ? (
setPhone(e.target.value)} className="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-orange-500 focus:border-orange-500 outline-none transition-all" />
) : (
)}