| 1 | import { useEffect, useState } from 'react'; |
| 2 | import { User, Save, KeyRound, Cpu, Server, Eye, EyeOff, CreditCard, ArrowRight } from 'lucide-react'; |
| 3 | import { Link } from 'react-router-dom'; |
| 4 | import { api } from '../api.js'; |
| 5 | |
| 6 | export default function Account() { |
| 7 | const [me, setMe] = useState(null); |
| 8 | const [billing, setBilling] = useState(null); |
| 9 | const [s, setS] = useState({}); |
| 10 | const [pw, setPw] = useState(''); |
| 11 | const [show, setShow] = useState(false); |
| 12 | const [busy, setBusy] = useState(false); |
| 13 | const [msg, setMsg] = useState(''); |
| 14 | |
| 15 | async function reload() { |
| 16 | const [u, b] = await Promise.all([ |
| 17 | api('/api/auth/me'), |
| 18 | api('/api/billing/me').catch(() => null), |
| 19 | ]); |
| 20 | setMe(u); |
| 21 | setS(u.settings || {}); |
| 22 | setBilling(b); |
| 23 | } |
| 24 | useEffect(() => { reload().catch(() => {}); }, []); |
| 25 | |
| 26 | function field(k, v) { setS(prev => ({ ...prev, [k]: v })); } |
| 27 | |
| 28 | async function save() { |
| 29 | setBusy(true); setMsg(''); |
| 30 | try { |
| 31 | await api('/api/auth/me', { method: 'PATCH', body: JSON.stringify({ |
| 32 | settings: s, |
| 33 | password: pw || undefined, |
| 34 | }) }); |
| 35 | setMsg('Saved.'); |
| 36 | setPw(''); |
| 37 | reload(); |
| 38 | } catch (e) { setMsg(e.message); } |
| 39 | finally { setBusy(false); } |
| 40 | } |
| 41 | |
| 42 | if (!me) return <div className="p-6 text-slate-500 text-sm">Loading…</div>; |
| 43 | |
| 44 | return ( |
| 45 | <div className="h-full overflow-y-auto p-6 max-w-2xl"> |
| 46 | <h1 className="text-lg font-semibold text-slate-100 mb-1 flex items-center gap-2"> |
| 47 | <User size={16} className="text-violet-300" /> My account |
| 48 | </h1> |
| 49 | <p className="text-xs text-slate-500 mb-6">Your personal credentials for the chat. Never shared with other users.</p> |
| 50 | |
| 51 | {billing && ( |
| 52 | <div className="card mb-4 flex items-center justify-between"> |
| 53 | <div> |
| 54 | <div className="text-xs uppercase tracking-wider text-slate-500 mb-0.5">Plan</div> |
| 55 | <div className="text-sm font-semibold text-slate-100">{billing.planName}</div> |
| 56 | <div className="text-[11px] text-slate-500"> |
| 57 | {billing.maxAgents} agents · {billing.maxRunsPerMonth ? `${billing.maxRunsPerMonth} runs/mo` : 'unlimited runs'} |
| 58 | </div> |
| 59 | </div> |
| 60 | <Link to="/billing" className="btn-ghost text-xs"> |
| 61 | <CreditCard size={12} /> Manage <ArrowRight size={11} /> |
| 62 | </Link> |
| 63 | </div> |
| 64 | )} |
| 65 | |
| 66 | <div className="space-y-4"> |
| 67 | <Section icon={Cpu} title="Model" desc="Claude model used by your chat (overrides the system default)."> |
| 68 | <Field label="Model name" value={s.model} onChange={v => field('model', v)} placeholder="claude-opus-4-7" mono /> |
| 69 | </Section> |
| 70 | |
| 71 | <Section icon={KeyRound} title="API key" desc="Your key for calling the model. Anthropic API key goes directly to anthropic.com. Relay key uses the system relay with your token."> |
| 72 | <Field label="Anthropic API key" value={s.anthropicApiKey} onChange={v => field('anthropicApiKey', v)} placeholder="sk-ant-…" mono secret={!show} /> |
| 73 | <Field label="Relay key (alternative)" value={s.relayKey} onChange={v => field('relayKey', v)} placeholder="optional" mono secret={!show} /> |
| 74 | <button type="button" className="btn-ghost text-xs" onClick={() => setShow(v => !v)}> |
| 75 | {show ? <EyeOff size={11} /> : <Eye size={11} />} {show ? 'Hide' : 'Show'} |
| 76 | </button> |
| 77 | </Section> |
| 78 | |
| 79 | <Section icon={Server} title="Relay (advanced)" desc="Override the relay URL. By default uses the system relay."> |
| 80 | <Field label="Relay URL" value={s.relayUrl} onChange={v => field('relayUrl', v)} placeholder="http://localhost:3202" mono /> |
| 81 | </Section> |
| 82 | |
| 83 | <Section icon={KeyRound} title="Change password"> |
| 84 | <input type="password" autoComplete="new-password" className="input font-mono" |
| 85 | placeholder="new password (≥6 chars; leave blank to keep current)" |
| 86 | value={pw} onChange={e => setPw(e.target.value)} /> |
| 87 | </Section> |
| 88 | </div> |
| 89 | |
| 90 | <div className="flex items-center gap-3 mt-6"> |
| 91 | <button className="btn-primary" onClick={save} disabled={busy}> |
| 92 | <Save size={13} /> {busy ? 'Saving…' : 'Save'} |
| 93 | </button> |
| 94 | {msg && <span className="text-xs text-slate-400">{msg}</span>} |
| 95 | </div> |
| 96 | |
| 97 | <div className="mt-8 pt-4 border-t border-border-subtle text-xs text-slate-500 space-y-1"> |
| 98 | <div>Username: <span className="font-mono text-slate-300">{me.username}</span> {me.isAdmin && <span className="pill bg-amber-500/10 text-amber-300 border border-amber-500/30 ml-1">admin</span>}</div> |
| 99 | <div>Email: {me.email || '—'}</div> |
| 100 | <div>Joined: {new Date(me.createdAt).toLocaleDateString('en')}</div> |
| 101 | {me.lastLoginAt && <div>Last login: {new Date(me.lastLoginAt).toLocaleString('en')}</div>} |
| 102 | </div> |
| 103 | </div> |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | function Section({ icon: Icon, title, desc, children }) { |
| 108 | return ( |
| 109 | <div className="card space-y-3"> |
| 110 | <div className="flex items-start gap-2"> |
| 111 | <div className="w-7 h-7 rounded-lg bg-violet/15 border border-violet/30 flex items-center justify-center"> |
| 112 | <Icon size={13} className="text-violet-300" /> |
| 113 | </div> |
| 114 | <div> |
| 115 | <div className="text-sm font-semibold text-slate-200">{title}</div> |
| 116 | {desc && <div className="text-[11px] text-slate-500">{desc}</div>} |
| 117 | </div> |
| 118 | </div> |
| 119 | <div className="space-y-2">{children}</div> |
| 120 | </div> |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | function Field({ label, value, onChange, placeholder, mono, secret }) { |
| 125 | return ( |
| 126 | <div> |
| 127 | <label className="text-[11px] uppercase tracking-wider text-slate-500">{label}</label> |
| 128 | <input |
| 129 | type={secret ? 'password' : 'text'} |
| 130 | value={value || ''} |
| 131 | onChange={e => onChange(e.target.value)} |
| 132 | placeholder={placeholder} |
| 133 | className={`input mt-1 ${mono ? 'font-mono' : ''}`} |
| 134 | /> |
| 135 | </div> |
| 136 | ); |
| 137 | } |