feat: isolate all four portals with route groups + polished design system
Fix blank /org/login (and /admin/login): the guarded portal layout was wrapping its own login route, rendering null when unauthenticated. Move each portal's authed pages into a (authed)/(chapter) route group so login pages live outside the guard. Structure: - app/(chapter)/* — chapter admin pages + guarded layout (was root-level) - app/org/(authed)/* — org pages + guarded layout; /org/login now free - app/admin/(authed)/* — admin pages + guarded layout; /admin/login free - Root layout slimmed to providers only (no shared sidebar) - Convert moved files' relative imports to @/app alias Design system: - PortalShell: shared sidebar shell (brand mark, themed active nav with accent bar, avatar dropdown user menu, loading skeletons) - portal-theme.ts: per-portal theme tokens (violet/slate/blue/emerald) - PortalLoginShell redesigned: two-column with gradient branding panel, dotted pattern, value-prop highlights, built-in back link - New shadcn components: Avatar, DropdownMenu, Skeleton - Move shared DraftCard to _components/draft-card.tsx (used by 2 portals) - Portal selector: remove Super Admin card, icon tiles, hover lift - All 4 login pages use react-hook-form + Zod + themed shell - Member nav restored to full 11-section list with icons Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
'use client';
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useSuperAdmin } from '@/app/_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 [tenants, setTenants] = useState<any[]>([]);
|
||||
const [initiating, setInitiating] = useState(false);
|
||||
const [pairingInfo, setPairingInfo] = useState<{ pairingToken: string; expiresAt: string } | null>(null);
|
||||
const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);
|
||||
const [qrStatus, setQrStatus] = useState<string>('PAIRING');
|
||||
const [assigningBotId, setAssigningBotId] = useState<string | null>(null);
|
||||
const [assignTenantId, setAssignTenantId] = useState('');
|
||||
const [assignBusy, setAssignBusy] = useState(false);
|
||||
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
|
||||
async function load() {
|
||||
const [botsRes, tenantsRes] = await Promise.all([
|
||||
fetch('/api/admin/bots'),
|
||||
fetch('/api/admin/tenants'),
|
||||
]);
|
||||
if (botsRes.ok) setBots(await botsRes.json());
|
||||
if (tenantsRes.ok) setTenants(await tenantsRes.json());
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!admin) { router.replace('/admin/login'); return; }
|
||||
void load();
|
||||
}, [admin, loading, router]);
|
||||
|
||||
const pollQr = useCallback(async (token: string) => {
|
||||
const res = await fetch(`/api/admin/bots/qr/${token}`);
|
||||
if (!res.ok) { clearInterval(pollRef.current!); return; }
|
||||
const data = await res.json();
|
||||
setQrStatus(data.status);
|
||||
if (data.qrDataUrl) {
|
||||
setQrDataUrl(data.qrDataUrl);
|
||||
clearInterval(pollRef.current!);
|
||||
}
|
||||
if (data.status === 'ACTIVE') {
|
||||
clearInterval(pollRef.current!);
|
||||
setPairingInfo(null);
|
||||
void load();
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
return () => { if (pollRef.current) clearInterval(pollRef.current); };
|
||||
}, []);
|
||||
|
||||
async function initiateBot() {
|
||||
setInitiating(true);
|
||||
setQrDataUrl(null);
|
||||
setQrStatus('PAIRING');
|
||||
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);
|
||||
pollRef.current = setInterval(() => void pollQr(data.pairingToken), 2000);
|
||||
void pollQr(data.pairingToken);
|
||||
}
|
||||
} 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');
|
||||
}
|
||||
}
|
||||
|
||||
async function assignBot() {
|
||||
if (!assigningBotId || !assignTenantId) return;
|
||||
setAssignBusy(true);
|
||||
try {
|
||||
const res = await fetch(`/api/admin/bots/${assigningBotId}/assign`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ tenantId: assignTenantId }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
alert(err.message ?? 'Failed to assign bot');
|
||||
return;
|
||||
}
|
||||
setAssigningBotId(null);
|
||||
setAssignTenantId('');
|
||||
void load();
|
||||
} finally {
|
||||
setAssignBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
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 flex items-start gap-6">
|
||||
<div className="flex-1">
|
||||
<p className="text-sm font-medium mb-1">
|
||||
{qrStatus === 'ACTIVE' ? 'Bot connected!' : 'New bot created — waiting for QR scan...'}
|
||||
</p>
|
||||
<p className="text-xs text-gray-600">Status: {qrStatus}</p>
|
||||
<p className="text-xs text-gray-600">Expires: {pairingInfo.expiresAt}</p>
|
||||
</div>
|
||||
{qrDataUrl ? (
|
||||
<img src={qrDataUrl} alt="QR Code" className="w-40 h-40 border-2 border-gray-300 rounded-lg" />
|
||||
) : (
|
||||
<div className="w-40 h-40 bg-gray-100 border-2 border-dashed border-gray-300 rounded-lg flex items-center justify-center text-xs text-gray-400">
|
||||
{qrStatus === 'ACTIVE' ? 'Connected' : 'Waiting for QR...'}
|
||||
</div>
|
||||
)}
|
||||
</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">Chapters</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 flex gap-3">
|
||||
<button
|
||||
onClick={() => { setAssigningBotId(assigningBotId === b.id ? null : b.id); setAssignTenantId(''); }}
|
||||
className="text-blue-600 text-xs hover:underline"
|
||||
>
|
||||
Assign
|
||||
</button>
|
||||
<button
|
||||
onClick={() => removeBot(b.id)}
|
||||
className="text-red-600 text-xs hover:underline"
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
{assigningBotId === b.id && (
|
||||
<tr key={`${b.id}-assign`} className="bg-blue-50">
|
||||
<td colSpan={6} className="px-4 py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-xs font-medium text-gray-600">Assign to chapter:</span>
|
||||
<select
|
||||
value={assignTenantId}
|
||||
onChange={(e) => setAssignTenantId(e.target.value)}
|
||||
className="border rounded-lg px-3 py-1.5 text-sm flex-1 max-w-xs bg-white"
|
||||
>
|
||||
<option value="">— select chapter —</option>
|
||||
{tenants.map((t: any) => (
|
||||
<option key={t.id} value={t.id}>{t.name} ({t.slug})</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
onClick={() => void assignBot()}
|
||||
disabled={!assignTenantId || assignBusy}
|
||||
className="bg-blue-600 text-white rounded-lg px-3 py-1.5 text-xs font-medium hover:bg-blue-700 disabled:opacity-50"
|
||||
>
|
||||
{assignBusy ? 'Assigning...' : 'Confirm'}
|
||||
</button>
|
||||
<button
|
||||
onClick={() => { setAssigningBotId(null); setAssignTenantId(''); }}
|
||||
className="text-xs text-gray-500 hover:text-gray-700"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{bots.length === 0 && <p className="p-4 text-gray-400">No bots in the pool.</p>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { apiFetch } from '@/app/_lib/api';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getToken } from '@/app/_lib/api';
|
||||
import { DraftCard } from '@/app/_components/draft-card';
|
||||
import Link from 'next/link';
|
||||
|
||||
type DraftType = 'EVENT' | 'SEVA_TASK' | 'FAQ_ITEM';
|
||||
|
||||
interface Draft {
|
||||
id: string;
|
||||
type: DraftType;
|
||||
status: string;
|
||||
proposedJson: Record<string, any>;
|
||||
confidence: number | null;
|
||||
createdAt: string;
|
||||
sourceMessage: {
|
||||
id: string;
|
||||
content: string;
|
||||
senderName: string | null;
|
||||
createdAt: string;
|
||||
};
|
||||
}
|
||||
|
||||
const TABS: { label: string; value: DraftType | 'ALL' }[] = [
|
||||
{ label: 'All', value: 'ALL' },
|
||||
{ label: 'Events', value: 'EVENT' },
|
||||
{ label: 'Seva Tasks', value: 'SEVA_TASK' },
|
||||
{ label: 'FAQs', value: 'FAQ_ITEM' },
|
||||
];
|
||||
|
||||
async function fetchDrafts(type?: DraftType): Promise<Draft[]> {
|
||||
const query = new URLSearchParams({ status: 'PENDING_REVIEW' });
|
||||
if (type) query.set('type', type);
|
||||
const res = await apiFetch(`/admin/drafts?${query}`);
|
||||
if (!res.ok) return [];
|
||||
return (await res.json()) as Draft[];
|
||||
}
|
||||
|
||||
export default async function DraftsPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams: Promise<{ type?: string }>;
|
||||
}) {
|
||||
const token = await getToken();
|
||||
if (!token) redirect('/login');
|
||||
|
||||
const { type } = await searchParams;
|
||||
const activeType = (type as DraftType) ?? undefined;
|
||||
const drafts = await fetchDrafts(activeType);
|
||||
|
||||
return (
|
||||
<div className="p-6 max-w-5xl mx-auto space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-semibold text-gray-900">AI Content Drafts</h1>
|
||||
<p className="text-sm text-gray-500 mt-0.5">
|
||||
Events, seva tasks, and FAQs auto-detected from WhatsApp messages
|
||||
</p>
|
||||
</div>
|
||||
<Link href="/admin" className="text-sm text-indigo-600 hover:underline">← Admin</Link>
|
||||
</div>
|
||||
|
||||
{/* Type filter tabs */}
|
||||
<div className="flex gap-1 border-b border-gray-200">
|
||||
{TABS.map((tab) => {
|
||||
const href = tab.value === 'ALL' ? '/admin/drafts' : `/admin/drafts?type=${tab.value}`;
|
||||
const isActive = (tab.value === 'ALL' && !activeType) || tab.value === activeType;
|
||||
return (
|
||||
<Link
|
||||
key={tab.value}
|
||||
href={href}
|
||||
className={`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
|
||||
isActive
|
||||
? 'border-indigo-600 text-indigo-600'
|
||||
: 'border-transparent text-gray-500 hover:text-gray-700'
|
||||
}`}
|
||||
>
|
||||
{tab.label}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{drafts.length === 0 ? (
|
||||
<div className="text-center py-20 text-gray-400">
|
||||
<p className="text-sm font-medium">No pending drafts</p>
|
||||
<p className="text-xs mt-1">
|
||||
Drafts appear here when the AI detects events, volunteer asks, or FAQ patterns in group messages.
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
{drafts.map((d) => (
|
||||
<DraftCard key={d.id} draft={d} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
|
||||
import { PortalShell } from '@/app/_components/portal-shell';
|
||||
import { LayoutDashboard, Building2, Network, Server, FileText } from 'lucide-react';
|
||||
|
||||
const NAV = [
|
||||
{ href: '/admin', label: 'Dashboard', icon: <LayoutDashboard /> },
|
||||
{ href: '/admin/orgs', label: 'Organisations', icon: <Network /> },
|
||||
{ href: '/admin/tenants', label: 'Chapters', icon: <Building2 /> },
|
||||
{ href: '/admin/bots', label: 'Bot Pool', icon: <Server /> },
|
||||
{ href: '/admin/drafts', label: 'AI Drafts', icon: <FileText /> },
|
||||
];
|
||||
|
||||
export default function AdminAuthedLayout({ children }: { children: React.ReactNode }) {
|
||||
const { admin, loading, logout } = useSuperAdmin();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading && !admin) router.replace('/admin/login');
|
||||
}, [admin, loading, router]);
|
||||
|
||||
return (
|
||||
<PortalShell
|
||||
portalName="Super Admin"
|
||||
theme="slate"
|
||||
navItems={NAV}
|
||||
loading={loading}
|
||||
authed={!!admin}
|
||||
user={admin ? { email: admin.email, name: admin.name, role: 'Super Admin' } : undefined}
|
||||
onLogout={() => { void logout(); router.replace('/admin/login'); }}
|
||||
>
|
||||
{children}
|
||||
</PortalShell>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
'use client';
|
||||
|
||||
import { use, useEffect, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
|
||||
|
||||
export default function AdminOrgDetailPage({ params }: { params: Promise<{ id: string }> }) {
|
||||
const { id } = use(params);
|
||||
const { admin, loading } = useSuperAdmin();
|
||||
const router = useRouter();
|
||||
const [org, setOrg] = useState<any>(null);
|
||||
const [showAdminForm, setShowAdminForm] = useState(false);
|
||||
const [adminForm, setAdminForm] = useState({ email: '', password: '', name: '', role: 'ORG_OWNER' });
|
||||
const [tenantId, setTenantId] = useState('');
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
async function load() {
|
||||
const res = await fetch(`/api/admin/orgs/${id}`);
|
||||
if (res.ok) setOrg(await res.json());
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!admin) { router.replace('/admin/login'); return; }
|
||||
void load();
|
||||
}, [admin, loading, router, id]);
|
||||
|
||||
async function handleCreateAdmin(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
setSaving(true);
|
||||
try {
|
||||
const res = await fetch(`/api/admin/orgs/${id}/admins`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(adminForm),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err.message ?? 'Failed');
|
||||
}
|
||||
setShowAdminForm(false);
|
||||
setAdminForm({ email: '', password: '', name: '', role: 'ORG_OWNER' });
|
||||
void load();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Error');
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleAssignTenant(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setSaving(true);
|
||||
try {
|
||||
const res = await fetch(`/api/admin/tenants/${tenantId}/org`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ organizationId: id }),
|
||||
});
|
||||
if (!res.ok) throw new Error('Failed to assign');
|
||||
setTenantId('');
|
||||
void load();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Error');
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (loading || !admin) return <p className="text-gray-500">Loading...</p>;
|
||||
if (!org) return <p className="text-gray-400">Organization not found.</p>;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<button onClick={() => router.back()} className="text-sm text-gray-500 hover:text-gray-700 mb-3">← Back</button>
|
||||
<h1 className="text-2xl font-bold mb-1">{org.name}</h1>
|
||||
<p className="text-sm text-gray-500 mb-6">{org.slug}</p>
|
||||
|
||||
{/* Chapters */}
|
||||
<div className="bg-white rounded-xl border mb-6">
|
||||
<div className="px-5 py-4 border-b flex items-center justify-between">
|
||||
<h2 className="font-semibold">Chapters</h2>
|
||||
<form onSubmit={(e) => void handleAssignTenant(e)} className="flex gap-2">
|
||||
<input
|
||||
type="text"
|
||||
value={tenantId}
|
||||
onChange={(e) => setTenantId(e.target.value)}
|
||||
className="border rounded-lg px-3 py-1.5 text-sm"
|
||||
placeholder="Tenant ID to assign"
|
||||
/>
|
||||
<button type="submit" disabled={!tenantId || saving} className="bg-blue-600 text-white rounded-lg px-3 py-1.5 text-sm font-medium hover:bg-blue-700 disabled:opacity-50">
|
||||
Assign
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 text-left">
|
||||
<tr>
|
||||
<th className="px-4 py-3 font-medium">Name</th>
|
||||
<th className="px-4 py-3 font-medium">Slug</th>
|
||||
<th className="px-4 py-3 font-medium">Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{(org.tenants ?? []).map((t: any) => (
|
||||
<tr key={t.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3 font-medium">{t.name}</td>
|
||||
<td className="px-4 py-3 text-gray-500">{t.slug}</td>
|
||||
<td className="px-4 py-3">
|
||||
<span className={`text-xs font-medium px-2 py-1 rounded-full ${t.isActive ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}>
|
||||
{t.isActive ? 'Active' : 'Inactive'}
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{org.tenants?.length === 0 && <p className="p-4 text-gray-400">No chapters assigned.</p>}
|
||||
</div>
|
||||
|
||||
{/* Org Admins */}
|
||||
<div className="bg-white rounded-xl border">
|
||||
<div className="px-5 py-4 border-b flex items-center justify-between">
|
||||
<h2 className="font-semibold">Org Admins</h2>
|
||||
<button
|
||||
onClick={() => setShowAdminForm(!showAdminForm)}
|
||||
className="bg-blue-600 text-white rounded-lg px-3 py-1.5 text-sm font-medium hover:bg-blue-700"
|
||||
>
|
||||
Add Admin
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{showAdminForm && (
|
||||
<form onSubmit={(e) => void handleCreateAdmin(e)} className="p-5 border-b flex flex-col gap-3">
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label className="block text-xs font-medium mb-1">Email</label>
|
||||
<input type="email" value={adminForm.email} onChange={(e) => setAdminForm({ ...adminForm, email: e.target.value })} className="w-full border rounded-lg px-3 py-2 text-sm" required />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium mb-1">Password</label>
|
||||
<input type="password" value={adminForm.password} onChange={(e) => setAdminForm({ ...adminForm, password: e.target.value })} className="w-full border rounded-lg px-3 py-2 text-sm" required />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium mb-1">Name</label>
|
||||
<input type="text" value={adminForm.name} onChange={(e) => setAdminForm({ ...adminForm, name: e.target.value })} className="w-full border rounded-lg px-3 py-2 text-sm" />
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium mb-1">Role</label>
|
||||
<select value={adminForm.role} onChange={(e) => setAdminForm({ ...adminForm, role: e.target.value })} className="w-full border rounded-lg px-3 py-2 text-sm">
|
||||
<option value="ORG_OWNER">ORG_OWNER</option>
|
||||
<option value="ORG_ADMIN">ORG_ADMIN</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
{error && <p className="text-red-600 text-sm">{error}</p>}
|
||||
<div className="flex gap-2">
|
||||
<button type="submit" disabled={saving} className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm font-medium hover:bg-blue-700 disabled:opacity-50">{saving ? 'Saving...' : 'Create'}</button>
|
||||
<button type="button" onClick={() => setShowAdminForm(false)} className="border rounded-lg px-4 py-2 text-sm hover:bg-gray-50">Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 text-left">
|
||||
<tr>
|
||||
<th className="px-4 py-3 font-medium">Email</th>
|
||||
<th className="px-4 py-3 font-medium">Name</th>
|
||||
<th className="px-4 py-3 font-medium">Role</th>
|
||||
<th className="px-4 py-3 font-medium">Created</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{(org.orgAdmins ?? []).map((a: any) => (
|
||||
<tr key={a.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3">{a.email}</td>
|
||||
<td className="px-4 py-3 text-gray-500">{a.name ?? '—'}</td>
|
||||
<td className="px-4 py-3">
|
||||
<span className="text-xs font-medium px-2 py-1 rounded-full bg-purple-100 text-purple-700">{a.role}</span>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-gray-500">{new Date(a.createdAt).toLocaleDateString()}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{org.orgAdmins?.length === 0 && <p className="p-4 text-gray-400">No org admins yet.</p>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
|
||||
export default function AdminOrgsPage() {
|
||||
const { admin, loading } = useSuperAdmin();
|
||||
const router = useRouter();
|
||||
const [orgs, setOrgs] = useState<any[]>([]);
|
||||
const [showAdd, setShowAdd] = useState(false);
|
||||
const [form, setForm] = useState({ slug: '', name: '' });
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
async function load() {
|
||||
const res = await fetch('/api/admin/orgs');
|
||||
if (res.ok) setOrgs(await res.json());
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!admin) { router.replace('/admin/login'); return; }
|
||||
void load();
|
||||
}, [admin, loading, router]);
|
||||
|
||||
async function handleAdd(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
setSaving(true);
|
||||
try {
|
||||
const res = await fetch('/api/admin/orgs', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(form),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
throw new Error(err.message ?? 'Failed');
|
||||
}
|
||||
setShowAdd(false);
|
||||
setForm({ slug: '', name: '' });
|
||||
void load();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Error');
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}
|
||||
|
||||
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">Organizations</h1>
|
||||
<button
|
||||
onClick={() => setShowAdd(!showAdd)}
|
||||
className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm font-medium hover:bg-blue-700"
|
||||
>
|
||||
Create Org
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{showAdd && (
|
||||
<form onSubmit={(e) => void handleAdd(e)} className="bg-white rounded-xl border p-5 mb-6 flex flex-col gap-4">
|
||||
<h2 className="font-semibold">New Organization</h2>
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">Name</label>
|
||||
<input
|
||||
type="text"
|
||||
value={form.name}
|
||||
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
||||
className="w-full border rounded-lg px-3 py-2 text-sm"
|
||||
placeholder="UP Parivaar"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">Slug</label>
|
||||
<input
|
||||
type="text"
|
||||
value={form.slug}
|
||||
onChange={(e) => setForm({ ...form, slug: e.target.value })}
|
||||
className="w-full border rounded-lg px-3 py-2 text-sm"
|
||||
placeholder="org_up_parivaar"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{error && <p className="text-red-600 text-sm">{error}</p>}
|
||||
<div className="flex gap-2">
|
||||
<button type="submit" disabled={saving} className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm font-medium hover:bg-blue-700 disabled:opacity-50">
|
||||
{saving ? 'Creating...' : 'Create'}
|
||||
</button>
|
||||
<button type="button" onClick={() => setShowAdd(false)} className="border rounded-lg px-4 py-2 text-sm hover:bg-gray-50">
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
|
||||
<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">Name</th>
|
||||
<th className="px-4 py-3 font-medium">Slug</th>
|
||||
<th className="px-4 py-3 font-medium">Chapters</th>
|
||||
<th className="px-4 py-3 font-medium">Org Admins</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{orgs.map((org: any) => (
|
||||
<tr key={org.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3">
|
||||
<Link href={`/admin/orgs/${org.id}`} className="text-blue-600 hover:underline font-medium">
|
||||
{org.name}
|
||||
</Link>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-gray-500">{org.slug}</td>
|
||||
<td className="px-4 py-3">{org._count?.tenants ?? 0}</td>
|
||||
<td className="px-4 py-3">{org._count?.orgAdmins ?? 0}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{orgs.length === 0 && <p className="p-4 text-gray-400">No organizations yet.</p>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
|
||||
export default function AdminDashboard() {
|
||||
const { admin, loading } = useSuperAdmin();
|
||||
const router = useRouter();
|
||||
const [tenants, setTenants] = useState<any[]>([]);
|
||||
const [bots, setBots] = useState<any[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!admin) { router.replace('/admin/login'); return; }
|
||||
fetch('/api/admin/tenants').then(r => r.ok && r.json()).then(d => setTenants(d ?? [])).catch(() => {});
|
||||
fetch('/api/admin/bots').then(r => r.ok && r.json()).then(d => setBots(d ?? [])).catch(() => {});
|
||||
}, [admin, loading, router]);
|
||||
|
||||
if (loading) return <p className="text-gray-500">Loading...</p>;
|
||||
if (!admin) return null;
|
||||
|
||||
const totalTenants = tenants.length;
|
||||
const activeTenants = tenants.filter((t: any) => t.isActive).length;
|
||||
const totalBots = bots.length;
|
||||
const totalMessages = tenants.reduce((s: number, t: any) => s + (t.stats?.messages ?? 0), 0);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold mb-6">Admin Dashboard</h1>
|
||||
<div className="grid grid-cols-4 gap-4 mb-8">
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase tracking-wide">Tenants</div>
|
||||
<div className="text-2xl font-bold mt-1">{totalTenants}</div>
|
||||
<div className="text-xs text-gray-400">{activeTenants} active</div>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase tracking-wide">Bot Accounts</div>
|
||||
<div className="text-2xl font-bold mt-1">{totalBots}</div>
|
||||
<div className="text-xs text-gray-400">in pool</div>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase tracking-wide">Messages</div>
|
||||
<div className="text-2xl font-bold mt-1">{totalMessages}</div>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase tracking-wide">Avg tenants/bot</div>
|
||||
<div className="text-2xl font-bold mt-1">{totalBots ? Math.round(totalTenants / totalBots) : 0}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-4 flex-wrap">
|
||||
<Link href="/admin/tenants" className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm">Manage Tenants</Link>
|
||||
<Link href="/admin/bots" className="bg-blue-600 text-white rounded-lg px-4 py-2 text-sm">Manage Bots</Link>
|
||||
<Link href="/admin/drafts" className="bg-indigo-600 text-white rounded-lg px-4 py-2 text-sm">AI Content Drafts</Link>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
|
||||
import { useRouter, useParams } from 'next/navigation';
|
||||
|
||||
export default function TenantDetailPage() {
|
||||
const { admin, loading } = useSuperAdmin();
|
||||
const router = useRouter();
|
||||
const params = useParams();
|
||||
const [tenant, setTenant] = useState<any>(null);
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
async function load() {
|
||||
const res = await fetch(`/api/admin/tenants/${params.id}`);
|
||||
if (res.ok) setTenant(await res.json());
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!admin) { router.replace('/admin/login'); return; }
|
||||
void load();
|
||||
}, [admin, loading, router, params.id]);
|
||||
|
||||
async function toggleActive() {
|
||||
setBusy(true);
|
||||
await fetch(`/api/admin/tenants/${params.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ isActive: !tenant.isActive }),
|
||||
});
|
||||
await load();
|
||||
setBusy(false);
|
||||
}
|
||||
|
||||
async function togglePaused() {
|
||||
setBusy(true);
|
||||
await fetch(`/api/admin/tenants/${params.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ isForwardingPaused: !tenant.isForwardingPaused }),
|
||||
});
|
||||
await load();
|
||||
setBusy(false);
|
||||
}
|
||||
|
||||
if (loading) return <p className="text-gray-500">Loading...</p>;
|
||||
if (!admin) return null;
|
||||
if (!tenant) return <p className="text-gray-500">Loading tenant...</p>;
|
||||
|
||||
return (
|
||||
<div className="max-w-2xl">
|
||||
<h1 className="text-2xl font-bold mb-2">{tenant.name}</h1>
|
||||
<p className="text-gray-500 text-sm mb-6">Slug: {tenant.slug}</p>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4 mb-8">
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase">Status</div>
|
||||
<button
|
||||
onClick={toggleActive}
|
||||
disabled={busy}
|
||||
className={`mt-1 text-sm font-medium px-3 py-1 rounded-full ${tenant.isActive ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}
|
||||
>
|
||||
{tenant.isActive ? 'Active' : 'Inactive'}
|
||||
</button>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase">Forwarding</div>
|
||||
<button
|
||||
onClick={togglePaused}
|
||||
disabled={busy}
|
||||
className={`mt-1 text-sm font-medium px-3 py-1 rounded-full ${tenant.isForwardingPaused ? 'bg-yellow-100 text-yellow-700' : 'bg-green-100 text-green-700'}`}
|
||||
>
|
||||
{tenant.isForwardingPaused ? 'Paused' : 'Active'}
|
||||
</button>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase">Groups</div>
|
||||
<div className="text-xl font-bold mt-1">{tenant.stats?.groups ?? 0}</div>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase">Messages</div>
|
||||
<div className="text-xl font-bold mt-1">{tenant.stats?.messages ?? 0}</div>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase">Rules</div>
|
||||
<div className="text-xl font-bold mt-1">{tenant.stats?.rules ?? 0}</div>
|
||||
</div>
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<div className="text-xs text-gray-500 uppercase">Routes</div>
|
||||
<div className="text-xl font-bold mt-1">{tenant.stats?.routes ?? 0}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{tenant.bot && (
|
||||
<div className="bg-white rounded-xl border p-4 mb-6">
|
||||
<h2 className="font-semibold text-sm mb-2">Assigned Bot</h2>
|
||||
<div className="text-xs space-y-1">
|
||||
<p><span className="text-gray-500">JID:</span> <span className="font-mono">{tenant.bot.jid}</span></p>
|
||||
<p><span className="text-gray-500">Name:</span> {tenant.bot.displayName ?? '—'}</p>
|
||||
<p><span className="text-gray-500">Status:</span> {tenant.bot.status}</p>
|
||||
<p><span className="text-gray-500">Linked:</span> {tenant.bot.linkedSince}</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tenant.admins && tenant.admins.length > 0 && (
|
||||
<div className="bg-white rounded-xl border p-4">
|
||||
<h2 className="font-semibold text-sm mb-2">Admins</h2>
|
||||
<div className="text-xs space-y-1">
|
||||
{tenant.admins.map((a: any) => (
|
||||
<p key={a.id}>{a.email} — <span className="uppercase">{a.role}</span></p>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSuperAdmin } from '@/app/_lib/super-admin-context';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import Link from 'next/link';
|
||||
|
||||
export default function TenantsPage() {
|
||||
const { admin, loading } = useSuperAdmin();
|
||||
const router = useRouter();
|
||||
const [tenants, setTenants] = useState<any[]>([]);
|
||||
|
||||
async function load() {
|
||||
const res = await fetch('/api/admin/tenants');
|
||||
if (res.ok) setTenants(await res.json());
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) return;
|
||||
if (!admin) { router.replace('/admin/login'); return; }
|
||||
void load();
|
||||
}, [admin, loading, router]);
|
||||
|
||||
async function toggleActive(id: string, current: boolean) {
|
||||
await fetch(`/api/admin/tenants/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ isActive: !current }),
|
||||
});
|
||||
void load();
|
||||
}
|
||||
|
||||
async function togglePaused(id: string, current: boolean) {
|
||||
await fetch(`/api/admin/tenants/${id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ isForwardingPaused: !current }),
|
||||
});
|
||||
void load();
|
||||
}
|
||||
|
||||
if (loading) return <p className="text-gray-500">Loading...</p>;
|
||||
if (!admin) return null;
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold mb-6">Tenants</h1>
|
||||
<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">Name</th>
|
||||
<th className="px-4 py-3 font-medium">Slug</th>
|
||||
<th className="px-4 py-3 font-medium">Bot</th>
|
||||
<th className="px-4 py-3 font-medium">Groups</th>
|
||||
<th className="px-4 py-3 font-medium">Messages</th>
|
||||
<th className="px-4 py-3 font-medium">Active</th>
|
||||
<th className="px-4 py-3 font-medium">Paused</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y">
|
||||
{tenants.map((t: any) => (
|
||||
<tr key={t.id} className="hover:bg-gray-50">
|
||||
<td className="px-4 py-3">
|
||||
<Link href={`/admin/tenants/${t.id}`} className="text-blue-600 hover:underline font-medium">
|
||||
{t.name}
|
||||
</Link>
|
||||
</td>
|
||||
<td className="px-4 py-3 text-gray-500">{t.slug}</td>
|
||||
<td className="px-4 py-3 text-xs">
|
||||
{t.bot ? <span className="font-mono">{t.bot.jid?.slice(0, 20)}...</span> : <span className="text-gray-400">—</span>}
|
||||
</td>
|
||||
<td className="px-4 py-3">{t.stats.groups}</td>
|
||||
<td className="px-4 py-3">{t.stats.messages}</td>
|
||||
<td className="px-4 py-3">
|
||||
<button
|
||||
onClick={() => toggleActive(t.id, t.isActive)}
|
||||
className={`text-xs font-medium px-2 py-1 rounded-full ${t.isActive ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}
|
||||
>
|
||||
{t.isActive ? 'Active' : 'Inactive'}
|
||||
</button>
|
||||
</td>
|
||||
<td className="px-4 py-3">
|
||||
<button
|
||||
onClick={() => togglePaused(t.id, t.isForwardingPaused)}
|
||||
className={`text-xs font-medium px-2 py-1 rounded-full ${t.isForwardingPaused ? 'bg-yellow-100 text-yellow-700' : 'bg-gray-100 text-gray-500'}`}
|
||||
>
|
||||
{t.isForwardingPaused ? 'Paused' : 'Active'}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
{tenants.length === 0 && <p className="p-4 text-gray-400">No tenants yet.</p>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user