| 1 | import { useState, useEffect } from 'react'; |
| 2 | import { NavLink, Routes, Route, Navigate, useLocation } from 'react-router-dom'; |
| 3 | import { MessageSquare, LayoutDashboard, Bot, Activity, Inbox, Settings2, LogOut, User, CreditCard } from 'lucide-react'; |
| 4 | import Chat from './pages/Chat.jsx'; |
| 5 | import Dashboard from './pages/Dashboard.jsx'; |
| 6 | import Agents from './pages/Agents.jsx'; |
| 7 | import AgentHub from './pages/AgentHub.jsx'; |
| 8 | import Queue from './pages/Queue.jsx'; |
| 9 | import Tasks from './pages/Tasks.jsx'; |
| 10 | import Settings from './pages/Settings.jsx'; |
| 11 | import Account from './pages/Account.jsx'; |
| 12 | import Billing from './pages/Billing.jsx'; |
| 13 | import Login from './pages/Login.jsx'; |
| 14 | import Landing from './pages/Landing.jsx'; |
| 15 | import Tos from './pages/Tos.jsx'; |
| 16 | import Privacy from './pages/Privacy.jsx'; |
| 17 | import { api } from './api.js'; |
| 18 | |
| 19 | const NAV = [ |
| 20 | { to: '/chat', label: 'Chat', icon: MessageSquare }, |
| 21 | { to: '/dashboard', label: 'Dashboard', icon: LayoutDashboard }, |
| 22 | { to: '/agents', label: 'Agents', icon: Bot }, |
| 23 | { to: '/tasks', label: 'Tasks', icon: Inbox }, |
| 24 | { to: '/hub', label: 'AgentHub', icon: Activity }, |
| 25 | { to: '/billing', label: 'Billing', icon: CreditCard }, |
| 26 | { to: '/account', label: 'Account', icon: User }, |
| 27 | { to: '/settings', label: 'Settings', icon: Settings2, adminOnly: true }, |
| 28 | ]; |
| 29 | |
| 30 | export default function App() { |
| 31 | const [user, setUser] = useState(null); |
| 32 | const [loading, setLoading] = useState(true); |
| 33 | const location = useLocation(); |
| 34 | |
| 35 | useEffect(() => { |
| 36 | api('/api/auth/whoami').then(r => { |
| 37 | if (r.mode === 'none') setUser({ sub: 'anon', name: 'anon', mode: 'none' }); |
| 38 | else if (r.user) setUser(r.user); |
| 39 | setLoading(false); |
| 40 | }).catch(() => setLoading(false)); |
| 41 | }, []); |
| 42 | |
| 43 | async function logout() { |
| 44 | await api('/api/auth/logout', { method: 'POST' }).catch(() => {}); |
| 45 | setUser(null); |
| 46 | } |
| 47 | |
| 48 | const PUBLIC_ROUTES = ['/', '/tos', '/privacy']; |
| 49 | if (PUBLIC_ROUTES.includes(location.pathname)) { |
| 50 | if (location.pathname === '/tos') return <Tos />; |
| 51 | if (location.pathname === '/privacy') return <Privacy />; |
| 52 | return <Landing />; |
| 53 | } |
| 54 | |
| 55 | if (loading) return <div className="h-full flex items-center justify-center text-slate-500">…</div>; |
| 56 | if (!user) return <Login onAuthed={setUser} />; |
| 57 | |
| 58 | return ( |
| 59 | <div className="h-full flex"> |
| 60 | <aside className="w-56 border-r border-border-subtle bg-surface-1 flex flex-col"> |
| 61 | <div className="px-4 py-4 border-b border-border-subtle"> |
| 62 | <div className="text-lg font-semibold text-slate-100">ClonAgent</div> |
| 63 | <div className="text-[11px] text-slate-500">email-triage agents</div> |
| 64 | </div> |
| 65 | <nav className="flex-1 p-2 space-y-1"> |
| 66 | {NAV.filter(n => !n.adminOnly || user.isAdmin).map(({ to, label, icon: Icon }) => ( |
| 67 | <NavLink key={to} to={to} className={({ isActive }) => |
| 68 | `flex items-center gap-2 px-3 py-2 rounded-lg text-sm transition ${ |
| 69 | isActive ? 'bg-violet/15 text-violet-200' : 'text-slate-400 hover:bg-surface-2 hover:text-slate-200' |
| 70 | }`}> |
| 71 | <Icon size={16} />{label} |
| 72 | </NavLink> |
| 73 | ))} |
| 74 | </nav> |
| 75 | <div className="border-t border-border-subtle p-3 flex items-center justify-between"> |
| 76 | <div className="min-w-0"> |
| 77 | <div className="text-xs text-slate-300 truncate">{user.name || user.sub}</div> |
| 78 | <div className="text-[10px] text-slate-600">{user.mode}</div> |
| 79 | </div> |
| 80 | <button onClick={logout} className="btn-ghost text-xs" title="Sign out"> |
| 81 | <LogOut size={12} /> |
| 82 | </button> |
| 83 | </div> |
| 84 | </aside> |
| 85 | <main className="flex-1 overflow-hidden"> |
| 86 | <Routes> |
| 87 | <Route path="/" element={<Landing />} /> |
| 88 | <Route path="/chat" element={<Chat />} /> |
| 89 | <Route path="/dashboard" element={<Dashboard />} /> |
| 90 | <Route path="/agents" element={<Agents />} /> |
| 91 | <Route path="/tasks" element={<Tasks />} /> |
| 92 | <Route path="/tasks/:id" element={<Tasks />} /> |
| 93 | <Route path="/threads" element={<Navigate to="/tasks" replace />} /> |
| 94 | <Route path="/threads/:id" element={<Navigate to="/tasks/:id" replace />} /> |
| 95 | <Route path="/hub" element={<AgentHub />} /> |
| 96 | <Route path="/hub/:id" element={<AgentHub />} /> |
| 97 | <Route path="/queue/:agentId" element={<Queue />} /> |
| 98 | <Route path="/billing" element={<Billing />} /> |
| 99 | <Route path="/account" element={<Account />} /> |
| 100 | <Route path="/settings" element={<Settings />} /> |
| 101 | <Route path="/tos" element={<Tos />} /> |
| 102 | <Route path="/privacy" element={<Privacy />} /> |
| 103 | </Routes> |
| 104 | </main> |
| 105 | </div> |
| 106 | ); |
| 107 | } |