Spaces:
Running
Running
File size: 22,856 Bytes
5a0e691 2f67ecf 32dd681 349f22c 90b88af 32dd681 305458b 6c03381 32dd681 5a0e691 305458b 32dd681 90b88af 32dd681 5a0e691 305458b 5a0e691 90b88af 32dd681 90b88af 5a0e691 90b88af 32dd681 5a0e691 305458b 5a0e691 90b88af 32dd681 90b88af 32dd681 5a0e691 32dd681 5a0e691 32dd681 6c03381 5a0e691 150c960 32dd681 90b88af 5a0e691 32dd681 5a0e691 32dd681 90b88af 5a0e691 90b88af 32dd681 6fdc858 32dd681 5a0e691 32dd681 5a0e691 32dd681 90b88af 32dd681 90b88af 32dd681 90b88af 5a0e691 32dd681 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 5a0e691 90b88af 2f67ecf 5a0e691 32dd681 90b88af 5a0e691 32dd681 5a0e691 32dd681 5a0e691 32dd681 5a0e691 32dd681 5a0e691 2f67ecf 5a0e691 32dd681 90b88af 32dd681 973dd4c 32dd681 2f67ecf 973dd4c 32dd681 90b88af 32dd681 5a0e691 305458b 6fdc858 305458b 867aec4 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 6c03381 305458b 2f67ecf 5a0e691 32dd681 90b88af 32dd681 5a0e691 32dd681 5a0e691 2f67ecf 32dd681 305458b 6c03381 305458b 6c03381 305458b 32dd681 305458b 2f67ecf 5a0e691 32dd681 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | 'use client';
import { useState, useEffect } from 'react';
import axios from 'axios';
import toast from 'react-hot-toast';
import { motion } from 'framer-motion';
import { fadeUp, stagger } from '@/lib/animations';
import { User, ChartBar as BarChart3, Key, Shield, CircleNotch as LoaderCircle, FloppyDisk as Save, Sun, Envelope as Mail, Building as Building2, SignOut as LogOut, CaretRight as ChevronRight, Lightning as Zap, Trash as Trash2, Plus, Copy } from '@phosphor-icons/react';
import { useAuth } from '@/contexts/auth';
import { getSupabase } from '@/lib/supabase';
import { useRouter } from 'next/navigation';
import { getApiKeys, createApiKey, deleteApiKey } from '@/lib/api';
import type { ApiKey } from '@/lib/api';
import { CriticalButton, FlatInput, PageHeader } from '@/components/ui';
function getInitials(name?: string, email?: string): string {
if (name) return name.split(' ').map(p => p[0]).slice(0, 2).join('').toUpperCase();
if (email) return email[0].toUpperCase();
return 'G';
}
export default function SettingsPage() {
const { user, isGuest, upgradeToAccount } = useAuth();
const router = useRouter();
const [fullName, setFullName] = useState('');
const [institution, setInstitution] = useState('');
const [saving, setSaving] = useState(false);
const [loading, setLoading] = useState(true);
const [jobsToday, setJobsToday] = useState(0);
const [jobsTotal, setJobsTotal] = useState(0);
const [dailyLimit, setDailyLimit] = useState(10);
const [apiKeys, setApiKeys] = useState<ApiKey[]>([]);
const [keysLoading, setKeysLoading] = useState(false);
const [creatingKey, setCreatingKey] = useState(false);
const [newKeyValue, setNewKeyValue] = useState<string | null>(null);
const [keyNameInput, setKeyNameInput] = useState('');
useEffect(() => {
const fetchData = async () => {
try {
const api = axios.create({ baseURL: '/api/backend' });
const supabase = getSupabase();
const { data: { session } } = await supabase.auth.getSession();
if (session?.access_token) api.defaults.headers.Authorization = `Bearer ${session.access_token}`;
const [profileRes, countRes, jobsRes] = await Promise.all([
api.get('/api/profile'),
api.get('/api/jobs/count'),
api.get('/api/jobs'),
]);
if (profileRes.data && !profileRes.data.error) {
setFullName(profileRes.data.full_name || '');
setInstitution(profileRes.data.institution || '');
}
setJobsToday(countRes.data.count || 0);
setDailyLimit(countRes.data.limit || 10);
setJobsTotal((jobsRes.data.jobs || []).length);
} catch { /* guest users may 401 */ } finally { setLoading(false); }
};
fetchData();
if (!isGuest) {
setKeysLoading(true);
getApiKeys().then(setApiKeys).catch(() => {}).finally(() => setKeysLoading(false));
}
}, [isGuest]);
const handleSave = async () => {
setSaving(true);
try {
const api = axios.create({ baseURL: '/api/backend' });
const supabase = getSupabase();
const { data: { session } } = await supabase.auth.getSession();
if (session?.access_token) api.defaults.headers.Authorization = `Bearer ${session.access_token}`;
await api.put('/api/profile', { full_name: fullName, institution });
toast.success('Profile updated');
} catch { toast.error('Failed to save settings'); } finally { setSaving(false); }
};
const handleSignOut = async () => {
const supabase = getSupabase();
await supabase.auth.signOut();
router.push('/auth');
};
const resolvedName = fullName || (user?.user_metadata?.full_name as string | undefined);
const initials = getInitials(resolvedName, user?.email);
const usagePercent = Math.min(Math.round((jobsToday / dailyLimit) * 100), 100);
const memberSince = user?.created_at
? new Date(user.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })
: null;
return (
<div className="max-w-3xl space-y-8">
<PageHeader title="Settings" subtitle="Manage your profile, usage, and preferences" />
<motion.div variants={stagger} animate="show" className="space-y-6">
{/* ββ Profile ββ */}
<motion.section variants={fadeUp} className="glass-card p-6">
<div className="flex items-center gap-2 mb-6">
<User className="w-4 h-4 text-accent-cyan" />
<h2 className="text-base font-semibold text-text-primary">Profile</h2>
</div>
{loading ? (
<div className="flex items-center justify-center py-8">
<LoaderCircle className="w-5 h-5 animate-spin text-accent-cyan" />
</div>
) : (
<div className="space-y-5">
{/* Avatar identity row */}
<div className="flex items-center gap-4 p-4 rounded-xl bg-surface-0 border border-glass-border">
<div className="w-14 h-14 rounded-2xl bg-accent-cyan/15 border border-accent-cyan/25 flex items-center justify-center shrink-0">
<span className="text-accent-cyan font-bold text-xl tracking-tight">{initials}</span>
</div>
<div>
<p className="text-sm font-semibold text-text-primary">
{resolvedName || (isGuest ? 'Guest User' : 'Synteny User')}
</p>
<p className="text-xs text-text-muted mt-0.5">{user?.email ?? (isGuest ? 'No account' : '')}</p>
{memberSince && <p className="text-[11px] text-text-muted/50 mt-0.5">Member since {memberSince}</p>}
{isGuest && <p className="text-[11px] text-accent-amber mt-0.5">Guest session β data not persisted</p>}
</div>
</div>
{/* Email (read-only) */}
<div>
<label className="block text-[11px] font-semibold text-text-muted mb-1.5 uppercase tracking-widest">Email</label>
<div className="relative">
<Mail className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" />
<input
type="email" disabled
value={user?.email ?? (isGuest ? 'Guest session' : '')}
className="w-full pl-10 pr-4 py-2.5 rounded-xl border border-glass-border bg-surface-0 text-text-muted text-sm cursor-not-allowed"
/>
</div>
</div>
{/* Editable fields */}
<div className="grid grid-cols-2 gap-4">
<div>
<label className="block text-[11px] font-semibold text-text-muted mb-1.5 uppercase tracking-widest">Full Name</label>
<div className="relative">
<User className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" />
<input
type="text" value={fullName} onChange={e => setFullName(e.target.value)}
placeholder="Your full name"
className="w-full pl-10 pr-4 py-2.5 rounded-xl border border-glass-border focus:border-accent-cyan/40 focus:ring-2 focus:ring-accent-cyan/10 outline-none transition text-sm text-text-primary bg-surface-1"
/>
</div>
</div>
<div>
<label className="block text-[11px] font-semibold text-text-muted mb-1.5 uppercase tracking-widest">Institution</label>
<div className="relative">
<Building2 className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-text-muted" />
<input
type="text" value={institution} onChange={e => setInstitution(e.target.value)}
placeholder="University or lab"
className="w-full pl-10 pr-4 py-2.5 rounded-xl border border-glass-border focus:border-accent-cyan/40 focus:ring-2 focus:ring-accent-cyan/10 outline-none transition text-sm text-text-primary bg-surface-1"
/>
</div>
</div>
</div>
{isGuest ? (
<div className="rounded-xl bg-accent-cyan/5 border border-accent-cyan/15 p-5 text-center">
<User className="w-10 h-10 text-accent-cyan mx-auto mb-3" />
<h3 className="text-sm font-semibold text-text-primary mb-1">Save your work</h3>
<p className="text-xs text-text-muted mb-4 max-w-sm mx-auto">
Create a free account to keep your analysis history, save settings, and never lose results.
</p>
<CriticalButton onClick={() => upgradeToAccount()}>
<svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/></svg>
Sign in with Google
</CriticalButton>
</div>
) : (
<div className="flex items-center justify-between pt-1">
<CriticalButton onClick={handleSave} disabled={saving} loading={saving}>
{!saving && <Save className="w-4 h-4" />}
Save Profile
</CriticalButton>
</div>
)}
</div>
)}
</motion.section>
{/* ββ Usage & Plan ββ */}
<motion.section variants={fadeUp} className="glass-card p-6">
<div className="flex items-center gap-2 mb-6">
<BarChart3 className="w-4 h-4 text-accent-cyan" />
<h2 className="text-base font-semibold text-text-primary">Usage & Plan</h2>
</div>
<div className="space-y-5">
<div className="grid grid-cols-3 gap-3">
{[
{ label: 'Today', value: jobsToday, sub: `of ${dailyLimit} limit` },
{ label: 'All time', value: jobsTotal, sub: 'total runs' },
{ label: 'Plan', value: 'Free', sub: 'no credit card' },
].map(stat => (
<div key={stat.label} className="rounded-xl p-4 text-center bg-surface-0 border border-glass-border">
<p className="text-xl font-bold text-text-primary">{stat.value}</p>
<p className="text-xs text-text-muted mt-0.5 font-medium">{stat.label}</p>
<p className="text-[10px] text-text-muted/50 mt-0.5">{stat.sub}</p>
</div>
))}
</div>
{/* Usage bar */}
<div>
<div className="flex items-center justify-between mb-2">
<span className="text-xs text-text-muted">Daily analyses used</span>
<span className="text-xs text-text-muted">{jobsToday} / {dailyLimit}</span>
</div>
<div className="h-1.5 bg-surface-0 rounded-full overflow-hidden border border-glass-border">
<motion.div
className={`h-full rounded-full ${usagePercent >= 80 ? 'bg-accent-amber' : 'bg-accent-cyan'}`}
initial={{ width: 0 }}
animate={{ width: `${usagePercent}%` }}
transition={{ duration: 0.9, ease: 'easeOut' }}
/>
</div>
<p className="text-[11px] text-text-muted mt-2">Resets daily at midnight UTC</p>
</div>
{/* Pro upsell stub */}
<div className="flex items-center justify-between p-3.5 rounded-xl bg-accent-cyan/5 border border-accent-cyan/15 cursor-default">
<div className="flex items-center gap-2.5">
<Zap className="w-4 h-4 text-accent-cyan shrink-0" />
<div>
<p className="text-sm font-medium text-text-primary">Need more capacity?</p>
<p className="text-xs text-text-muted">Pro plan with higher limits β coming soon</p>
</div>
</div>
<ChevronRight className="w-4 h-4 text-text-muted shrink-0" />
</div>
</div>
</motion.section>
{/* ββ Appearance ββ */}
<motion.section variants={fadeUp} className="glass-card p-6">
<div className="flex items-center gap-2 mb-4">
<Sun className="w-4 h-4 text-accent-cyan" />
<h2 className="text-base font-semibold text-text-primary">Appearance</h2>
</div>
<div className="flex items-center justify-between p-3.5 rounded-xl bg-surface-0 border border-glass-border">
<div>
<p className="text-sm font-medium text-text-primary">Void Dark</p>
<p className="text-xs text-text-muted mt-0.5">Designed for long research sessions β reduced eye strain</p>
</div>
<span className="text-[10px] px-2.5 py-0.5 rounded-full bg-accent-cyan/10 text-accent-cyan font-medium">Active</span>
</div>
<p className="text-xs text-text-muted mt-3">Additional themes will be available in a future update.</p>
</motion.section>
{/* ββ API Access ββ */}
<motion.section variants={fadeUp} className="glass-card p-6">
<div className="flex items-center gap-2 mb-4">
<Key className="w-4 h-4 text-accent-cyan" />
<h2 className="text-base font-semibold text-text-primary">API Access</h2>
</div>
{isGuest ? (
<div className="p-5 rounded-xl bg-surface-0 border border-glass-border text-center">
<Key className="w-8 h-8 text-text-muted mx-auto mb-3" />
<p className="text-sm font-medium text-text-primary">Sign in to access API keys</p>
<p className="text-xs text-text-muted mt-1 max-w-xs mx-auto">
Create a free account to generate API keys for programmatic access.
</p>
</div>
) : (
<div className="space-y-4">
<p className="text-sm text-text-secondary">
Generate API keys to run Synteny pipelines from your scripts.
Use the <code className="text-accent-cyan text-xs bg-surface-0 px-1.5 py-0.5 rounded">X-API-Key</code> header to authenticate.
</p>
{/* Existing keys */}
{keysLoading ? (
<div className="flex items-center justify-center py-4">
<LoaderCircle className="w-4 h-4 animate-spin text-accent-cyan" />
</div>
) : apiKeys.length > 0 ? (
<div className="space-y-2">
{apiKeys.map(k => (
<div key={k.id} className="flex items-center justify-between p-3 rounded-xl bg-surface-0 border border-glass-border">
<div className="flex items-center gap-3">
<Key className="w-4 h-4 text-text-muted" />
<div>
<p className="text-sm font-medium text-text-primary">{k.name}</p>
<p className="text-xs text-text-muted font-mono">{k.key_prefix}...</p>
</div>
</div>
<div className="flex items-center gap-3">
<span className="text-[10px] text-text-muted">
{k.last_used_at ? `Used ${new Date(k.last_used_at).toLocaleDateString()}` : 'Never used'}
</span>
<button
onClick={async () => {
if (!confirm('Revoke this API key? This cannot be undone.')) return;
try {
await deleteApiKey(k.id);
setApiKeys(prev => prev.filter(x => x.id !== k.id));
toast.success('API key revoked');
} catch { toast.error('Failed to revoke key'); }
}}
className="p-1.5 rounded-lg hover:bg-error/10 text-text-muted hover:text-error transition"
>
<Trash2 className="w-3.5 h-3.5" />
</button>
</div>
</div>
))}
</div>
) : (
<div className="p-4 rounded-xl bg-surface-0 border border-glass-border text-center">
<p className="text-xs text-text-muted">No API keys yet.</p>
</div>
)}
{/* Generate new key */}
{newKeyValue ? (
<div className="p-4 rounded-xl bg-accent-cyan/5 border border-accent-cyan/15">
<p className="text-xs font-semibold text-accent-cyan mb-2">Key generated β copy it now. You won't see it again.</p>
<div className="flex items-center gap-2">
<code className="flex-1 text-xs font-mono bg-surface-0 px-3 py-2 rounded-lg border border-glass-border text-text-primary break-all">
{newKeyValue}
</code>
<button
onClick={() => { navigator.clipboard.writeText(newKeyValue); toast.success('Copied!'); }}
className="px-3 py-2 text-xs flex items-center gap-1 shrink-0 rounded-lg bg-surface-1 border border-surface-3 text-text-secondary hover:text-text-primary hover:border-accent-cyan/40 transition"
>
<Copy className="w-3.5 h-3.5" /> Copy
</button>
</div>
<button
onClick={() => { setNewKeyValue(null); setKeyNameInput(''); }}
className="text-xs text-text-muted mt-2 hover:text-text-primary transition"
>
Dismiss
</button>
</div>
) : (
<div className="flex items-center gap-2">
<FlatInput
type="text" value={keyNameInput}
onChange={e => setKeyNameInput(e.target.value)}
placeholder="e.g. My script"
className="flex-1 text-sm"
/>
<CriticalButton
onClick={async () => {
if (!keyNameInput.trim()) { toast.error('Enter a name'); return; }
setCreatingKey(true);
try {
const res = await createApiKey(keyNameInput.trim());
setNewKeyValue(res.key);
setApiKeys(prev => [...prev, { id: '', name: res.name, key_prefix: res.key_prefix, created_at: new Date().toISOString(), last_used_at: null }]);
toast.success('API key created');
} catch { toast.error('Failed to create key'); } finally { setCreatingKey(false); }
}}
loading={creatingKey}
>
{!creatingKey && <Plus className="w-4 h-4" />}
Generate
</CriticalButton>
</div>
)}
</div>
)}
</motion.section>
{/* ββ Data & Privacy ββ */}
<motion.section variants={fadeUp} className="glass-card p-6">
<div className="flex items-center gap-2 mb-4">
<Shield className="w-4 h-4 text-accent-cyan" />
<h2 className="text-base font-semibold text-text-primary">Data & Privacy</h2>
</div>
<div className="space-y-3">
{[
'Your uploaded sequences and results belong to you. We never train models on your data.',
'Cached query results are stored for 24 hours, keyed by input hash β not your user ID.',
'All AI outputs are labeled "AI-assisted research analysis β not for clinical or diagnostic use."',
].map((line, i) => (
<div key={i} className="flex items-start gap-2.5">
<div className="w-1 h-1 rounded-full bg-accent-cyan/60 mt-2 shrink-0" />
<p className="text-sm text-text-secondary">{line}</p>
</div>
))}
</div>
</motion.section>
{/* ββ Account Actions ββ */}
<motion.section variants={fadeUp} className={`glass-card p-6 ${isGuest ? '' : 'border border-error/10'}`}>
<h2 className="text-base font-semibold text-text-primary mb-4">
{isGuest ? 'Get Started' : 'Account Actions'}
</h2>
{isGuest ? (
<div className="space-y-3">
<p className="text-sm text-text-secondary">
Sign in with Google to save your results, access your history, and unlock higher daily limits.
</p>
<CriticalButton onClick={() => upgradeToAccount()}>
<svg className="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/></svg>
Sign in with Google
</CriticalButton>
</div>
) : (
<button
onClick={handleSignOut}
className="flex items-center gap-2 px-4 py-2.5 rounded-xl border border-error/20 text-error text-sm hover:bg-error/5 transition"
>
<LogOut className="w-4 h-4" />
Sign out
</button>
)}
</motion.section>
</motion.div>
</div>
);
} |