'use client'; import { useEffect, useState } from 'react'; import { useSuperAdmin } from '../../_lib/super-admin-context'; import { useRouter } from 'next/navigation'; export default function BotsPage() { const { admin, loading } = useSuperAdmin(); const router = useRouter(); const [bots, setBots] = useState([]); const [initiating, setInitiating] = useState(false); const [pairingInfo, setPairingInfo] = useState<{ token: string; expiresAt: string } | null>(null); async function load() { const res = await fetch('/api/admin/bots'); if (res.ok) setBots(await res.json()); } useEffect(() => { if (loading) return; if (!admin) { router.replace('/admin/login'); return; } void load(); }, [admin, loading, router]); async function initiateBot() { setInitiating(true); try { const res = await fetch('/api/admin/bots', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({}), }); if (res.ok) { const data = await res.json(); setPairingInfo(data); } } finally { setInitiating(false); void load(); } } async function removeBot(id: string) { if (!confirm('Remove this bot? Only possible if no tenants are assigned.')) return; const res = await fetch(`/api/admin/bots/${id}`, { method: 'DELETE' }); if (res.ok) { void load(); } else { const err = await res.json(); alert(err.message ?? 'Failed to remove bot'); } } function getQrUrl() { if (!pairingInfo) return null; return `/api/admin/bots/qr/${pairingInfo.token}`; } if (loading) return

Loading...

; if (!admin) return null; return (

Bot Pool

{pairingInfo && (

New bot created — scan QR to pair

Expires: {pairingInfo.expiresAt}

View QR Code
)}
{bots.map((b: any) => ( ))}
JID Name Status Tenants Created
{b.jid?.slice(0, 30) ?? 'pending...'} {b.displayName ?? '—'} {b.status} {b.tenantCount} {new Date(b.createdAt).toLocaleDateString()}
{bots.length === 0 &&

No bots in the pool.

}
); }