| 1 | import { useState, useEffect } from 'react'; |
| 2 | import { Bot, Mail, Activity, CheckCircle2, AlertCircle, Clock } from 'lucide-react'; |
| 3 | import { api, streamSSE } from '../api.js'; |
| 4 | |
| 5 | export default function Dashboard() { |
| 6 | const [agents, setAgents] = useState([]); |
| 7 | const [runs, setRuns] = useState([]); |
| 8 | const [tick, setTick] = useState(0); |
| 9 | |
| 10 | useEffect(() => { (async () => { |
| 11 | setAgents(await api('/api/agents')); |
| 12 | setRuns(await api('/api/runs')); |
| 13 | })().catch(() => {}); }, [tick]); |
| 14 | |
| 15 | useEffect(() => { |
| 16 | const close = streamSSE('/api/activity/stream', () => setTick(t => t + 1)); |
| 17 | return close; |
| 18 | }, []); |
| 19 | |
| 20 | const enabled = agents.filter(a => a.enabled); |
| 21 | const last24 = runs.filter(r => Date.now() - new Date(r.at).getTime() < 86400000); |
| 22 | const errors = last24.filter(r => r.status === 'error').length; |
| 23 | |
| 24 | return ( |
| 25 | <div className="h-full overflow-y-auto p-6 space-y-6 max-w-6xl"> |
| 26 | <div> |
| 27 | <h1 className="text-lg font-semibold text-slate-100">Dashboard</h1> |
| 28 | <p className="text-xs text-slate-500 mt-0.5">Live status of your ClonAgents.</p> |
| 29 | </div> |
| 30 | |
| 31 | <div className="grid grid-cols-1 md:grid-cols-4 gap-3"> |
| 32 | <Stat label="Agents" value={agents.length} icon={Bot} /> |
| 33 | <Stat label="Active" value={enabled.length} icon={CheckCircle2} tone="ok" /> |
| 34 | <Stat label="Runs (24h)" value={last24.length} icon={Activity} /> |
| 35 | <Stat label="Errors (24h)" value={errors} icon={AlertCircle} tone={errors ? 'err' : 'mute'} /> |
| 36 | </div> |
| 37 | |
| 38 | <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 39 | <div className="card"> |
| 40 | <h2 className="text-sm font-semibold text-slate-200 mb-3 flex items-center gap-2"> |
| 41 | <Bot size={14} className="text-violet-300" /> Agents |
| 42 | </h2> |
| 43 | {agents.length === 0 ? ( |
| 44 | <p className="text-sm text-slate-500">No agents yet. Go to Chat to create your first one.</p> |
| 45 | ) : ( |
| 46 | <ul className="space-y-2"> |
| 47 | {agents.map(a => ( |
| 48 | <li key={a.id} className="flex items-center justify-between text-sm"> |
| 49 | <div className="min-w-0"> |
| 50 | <div className="font-medium text-slate-100 truncate">{a.name}</div> |
| 51 | <div className="text-[11px] text-slate-500 font-mono truncate">{a.botEmail}</div> |
| 52 | </div> |
| 53 | <span className={a.enabled ? 'pill-on' : 'pill-off'}> |
| 54 | {a.enabled ? 'on' : 'off'} |
| 55 | </span> |
| 56 | </li> |
| 57 | ))} |
| 58 | </ul> |
| 59 | )} |
| 60 | </div> |
| 61 | <div className="card"> |
| 62 | <h2 className="text-sm font-semibold text-slate-200 mb-3 flex items-center gap-2"> |
| 63 | <Mail size={14} className="text-violet-300" /> Recent runs |
| 64 | </h2> |
| 65 | {runs.length === 0 ? ( |
| 66 | <p className="text-sm text-slate-500">No runs yet.</p> |
| 67 | ) : ( |
| 68 | <ul className="space-y-2"> |
| 69 | {runs.slice(0, 8).map(r => ( |
| 70 | <li key={r.id} className="flex items-start gap-2 text-sm"> |
| 71 | <Clock size={12} className="text-slate-600 mt-1 flex-shrink-0" /> |
| 72 | <div className="min-w-0 flex-1"> |
| 73 | <div className="text-slate-200 truncate">{r.subject || '(no subject)'}</div> |
| 74 | <div className="text-[11px] text-slate-500 font-mono truncate"> |
| 75 | {r.agentId} · {r.from} · {new Date(r.at).toLocaleTimeString('en', { hour12: false })} |
| 76 | </div> |
| 77 | </div> |
| 78 | <span className={r.status === 'error' ? 'pill bg-rose-500/15 text-rose-300 border border-rose-500/30' |
| 79 | : 'pill bg-violet/15 text-violet-300 border border-violet/30'}> |
| 80 | {r.status} |
| 81 | </span> |
| 82 | </li> |
| 83 | ))} |
| 84 | </ul> |
| 85 | )} |
| 86 | </div> |
| 87 | </div> |
| 88 | </div> |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | function Stat({ label, value, icon: Icon, tone = 'normal' }) { |
| 93 | const toneCls = { |
| 94 | ok: 'text-emerald-300 bg-emerald-500/10 border-emerald-500/30', |
| 95 | err: 'text-rose-300 bg-rose-500/10 border-rose-500/30', |
| 96 | mute: 'text-slate-400 bg-slate-800 border-slate-700', |
| 97 | normal:'text-violet-300 bg-violet/10 border-violet/30', |
| 98 | }[tone]; |
| 99 | return ( |
| 100 | <div className="card"> |
| 101 | <div className="flex items-center justify-between"> |
| 102 | <span className="text-[11px] uppercase tracking-wider text-slate-500">{label}</span> |
| 103 | <span className={`w-7 h-7 rounded-lg border flex items-center justify-center ${toneCls}`}> |
| 104 | <Icon size={14} /> |
| 105 | </span> |
| 106 | </div> |
| 107 | <div className="text-2xl font-semibold text-slate-100 mt-2">{value}</div> |
| 108 | </div> |
| 109 | ); |
| 110 | } |