good forst commit
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
'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<any[]>([]);
|
||||
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 <p className="text-gray-500">Loading...</p>;
|
||||
if (!admin) return null;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<h1 className="text-2xl font-bold">Bot Pool</h1>
|
||||
<button
|
||||
onClick={initiateBot}
|
||||
disabled={initiating}
|
||||
className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm disabled:opacity-50"
|
||||
>
|
||||
{initiating ? 'Creating...' : 'Add Bot'}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{pairingInfo && (
|
||||
<div className="bg-yellow-50 border border-yellow-200 rounded-xl p-4 mb-6">
|
||||
<p className="text-sm font-medium mb-2">New bot created — scan QR to pair</p>
|
||||
<p className="text-xs text-gray-600 mb-2">Expires: {pairingInfo.expiresAt}</p>
|
||||
<a
|
||||
href={getQrUrl() ?? '#'}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-blue-600 text-sm underline"
|
||||
>
|
||||
View QR Code
|
||||
</a>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="bg-white rounded-xl border overflow-hidden">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 text-left">
|
||||
<tr>
|
||||
<th className="px-4 py-3 font-medium">JID</th>
|
||||
<th className="px-4 py-3 font-medium">Name</th>
|
||||
<th className="px-4 py-3 font-medium">Status</th>
|
||||
<th className="px-4 py-3 font-medium">Tenants</th>
|
||||
<th className="px-4 py-3 font-medium">Created</th>
|
||||
<th className="px-4 py-3 font-medium"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{bots.map((b: any) => (
|
||||
<tr key={b.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3 font-mono text-xs">{b.jid?.slice(0, 30) ?? 'pending...'}</td>
|
||||
<td className="px-4 py-3">{b.displayName ?? '—'}</td>
|
||||
<td className="px-4 py-3">
|
||||
<span className={`text-xs font-medium px-2 py-1 rounded-full ${
|
||||
b.status === 'ACTIVE' ? 'bg-green-100 text-green-700' :
|
||||
b.status === 'PAIRING' ? 'bg-yellow-100 text-yellow-700' :
|
||||
'bg-gray-100 text-gray-500'
|
||||
}`}>{b.status}</span>
|
||||
</td>
|
||||
<td className="px-4 py-3">{b.tenantCount}</td>
|
||||
<td className="px-4 py-3 text-xs text-gray-500">{new Date(b.createdAt).toLocaleDateString()}</td>
|
||||
<td className="px-4 py-3">
|
||||
<button
|
||||
onClick={() => removeBot(b.id)}
|
||||
className="text-red-600 text-xs hover:underline"
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{bots.length === 0 && <p className="p-4 text-gray-400">No bots in the pool.</p>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user