import { useEffect, useState } from 'react'; import { useAuthStore } from '../store/authStore'; import LoadingState from '../components/LoadingState'; import PurgeDemoModal from '../components/PurgeDemoModal'; import { getDeveloperDashboard, toggleAccountActive, setAccountTrial, getSystemLogs, getExportUrl, type DeveloperDashboard, type SystemLogEntry, type PaginatedLogs, } from '../services/developerService'; import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid, LineChart, Line, } from 'recharts'; import { Building2, Users, Activity, Download, ToggleLeft, ToggleRight, AlertTriangle, Calendar, Search, RefreshCw, Trash2, } from 'lucide-react'; export default function DeveloperPage() { const user = useAuthStore((s) => s.user); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [logs, setLogs] = useState(null); const [logsLoading, setLogsLoading] = useState(false); const [logPage, setLogPage] = useState(1); const [logLevel, setLogLevel] = useState(''); const [searchAcct, setSearchAcct] = useState(''); const [trialDays, setTrialDays] = useState>({}); const [tab, setTab] = useState<'dashboard' | 'accounts' | 'logs'>('dashboard'); const [showPurge, setShowPurge] = useState(false); const loadDashboard = async () => { setLoading(true); try { const d = await getDeveloperDashboard(); setData(d); } catch { /* ignore */ } finally { setLoading(false); } }; const loadLogs = async () => { setLogsLoading(true); try { const l = await getSystemLogs(logPage, 20, logLevel || undefined); setLogs(l); } catch { /* ignore */ } finally { setLogsLoading(false); } }; useEffect(() => { loadDashboard(); }, []); useEffect(() => { if (tab === 'logs') loadLogs(); }, [tab, logPage, logLevel]); const handleToggleActive = async (id: number) => { await toggleAccountActive(id); loadDashboard(); }; const handleSetTrial = async (id: number) => { const days = parseInt(trialDays[id] || ''); if (!days || days < 1) return; await setAccountTrial(id, days); setTrialDays({ ...trialDays, [id]: '' }); loadDashboard(); }; const exportUrl = getExportUrl(); const adminStatCards = data ? [ { label: 'Colegios', value: data.total_accounts, icon: Building2, color: '#3b82f6' }, { label: 'Activos', value: data.active_accounts, icon: Activity, color: '#10b981' }, { label: 'Prueba', value: data.trial_accounts, icon: Calendar, color: '#f59e0b' }, { label: 'Usuarios', value: data.total_users, icon: Users, color: '#8b5cf6' }, ] : []; return (

Panel de Desarrollo

{tab === 'dashboard' && (
{adminStatCards.length > 0 && (
{adminStatCards.map((c) => (
{c.value}
{c.label}
))}
)}

Colegios Registrados (últimos 30 días)

{data && data.accounts_by_date.length > 0 ? ( v.slice(5)} /> ) : (

Sin datos

)}

Errores por Día (últimos 30 días)

{data && data.errors_trend.length > 0 ? ( v.slice(5)} /> ) : (

Sin errores registrados

)}
{data && data.accounts.length > 0 && (

Usuarios por Colegio

({ name: a.name, usuarios: a.user_count }))} layout="vertical">
)} {user?.role === 'developer' && (

Zona peligrosa

Purga todos los datos subidos al demo y restaura el seeder original. Esta acción NO se puede deshacer.

)} setShowPurge(false)} onSuccess={(deleted) => { const total = Object.values(deleted).reduce((a, b) => a + b, 0); alert(`Demo purgado y re-sembrado. Total filas eliminadas: ${total}`); loadDashboard(); }} />
)} {tab === 'accounts' && (
setSearchAcct(e.target.value)} className="w-full border border-gray-300 rounded-lg pl-9 pr-3 py-2 text-sm" />
{loading ? ( ) : null} {!loading && (data?.accounts || []) .filter((a) => !searchAcct || a.name.toLowerCase().includes(searchAcct.toLowerCase())) .map((a) => ( ))} {!loading && (!data?.accounts || data.accounts.length === 0) && ( )}
ID Nombre Plan Usuarios Estado Prueba hasta Acciones
{a.id} {a.name} {a.plan} {a.user_count} {a.is_active ? 'Activo' : 'Inactivo'} {a.trial_end_date ? new Date(a.trial_end_date).toLocaleDateString() : '—'}
setTrialDays({ ...trialDays, [a.id]: e.target.value })} className="w-16 border border-gray-300 rounded px-2 py-1.5 text-xs" />
No hay colegios
)} {tab === 'logs' && (
{logsLoading ? ( ) : null} {!logsLoading && logs?.items.map((l) => ( ))} {!logsLoading && (!logs || logs.items.length === 0) && ( )}
Fecha Nivel Mensaje Endpoint Método Cuenta
{l.created_at ? new Date(l.created_at).toLocaleString() : '-'} {l.level} {l.message} {l.endpoint || '-'} {l.method || '-'} {l.account_id || '-'}
No hay errores registrados
{logs && logs.total_pages > 1 && (
Pág {logs.page} de {logs.total_pages} ({logs.total} registros)
)}
)}
); }