|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { useAuth } from "@/src/components/auth-provider"; |
| 6 | +import { fetchUsage, type UsageStats } from "@/src/lib/auth-client"; |
| 7 | + |
| 8 | +function UsageBar({ used, limit, label }: { used: number; limit: number | null; label: string }) { |
| 9 | + const isUnlimited = limit === null; |
| 10 | + const pct = isUnlimited ? 0 : limit > 0 ? Math.min((used / limit) * 100, 100) : 0; |
| 11 | + const isNearLimit = !isUnlimited && limit > 0 && used >= limit * 0.8; |
| 12 | + |
| 13 | + return ( |
| 14 | + <div> |
| 15 | + <div className="flex items-center justify-between mb-1.5"> |
| 16 | + <span className="text-xs font-medium text-slate-700 dark:text-slate-300">{label}</span> |
| 17 | + <span className="text-xs text-slate-500 dark:text-slate-400"> |
| 18 | + {isUnlimited ? ( |
| 19 | + <span className="text-indigo-600 dark:text-indigo-400 font-semibold">Unlimited</span> |
| 20 | + ) : ( |
| 21 | + <>{used} / {limit}</> |
| 22 | + )} |
| 23 | + </span> |
| 24 | + </div> |
| 25 | + {!isUnlimited && ( |
| 26 | + <div className="h-2 bg-slate-100 dark:bg-white/10 rounded-full overflow-hidden"> |
| 27 | + <div |
| 28 | + className={`h-full rounded-full transition-all ${ |
| 29 | + isNearLimit ? "bg-orange-500" : "bg-indigo-500" |
| 30 | + }`} |
| 31 | + style={{ width: `${pct}%` }} |
| 32 | + /> |
| 33 | + </div> |
| 34 | + )} |
| 35 | + </div> |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +export function AccountPage() { |
| 40 | + const { user, status, tier, logout } = useAuth(); |
| 41 | + const [usage, setUsage] = useState<UsageStats | null>(null); |
| 42 | + const [loading, setLoading] = useState(true); |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + if (status === "authenticated") { |
| 46 | + fetchUsage() |
| 47 | + .then(setUsage) |
| 48 | + .catch(() => {}) |
| 49 | + .finally(() => setLoading(false)); |
| 50 | + } else if (status === "anonymous") { |
| 51 | + setLoading(false); |
| 52 | + } |
| 53 | + }, [status]); |
| 54 | + |
| 55 | + if (status === "anonymous") { |
| 56 | + return ( |
| 57 | + <main className="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 dark:from-neutral-950 dark:to-neutral-900 flex items-center justify-center p-4"> |
| 58 | + <div className="text-center max-w-sm space-y-4"> |
| 59 | + <h1 className="text-2xl font-bold text-slate-900 dark:text-white">Sign in</h1> |
| 60 | + <p className="text-sm text-slate-500 dark:text-slate-400"> |
| 61 | + Sign in to view your account, usage stats, and manage your subscription. |
| 62 | + </p> |
| 63 | + <Link |
| 64 | + href="/login?next=/account" |
| 65 | + className="inline-block px-6 py-2.5 bg-slate-900 text-white text-sm font-semibold rounded-xl hover:bg-slate-700 transition-colors" |
| 66 | + > |
| 67 | + Sign in |
| 68 | + </Link> |
| 69 | + </div> |
| 70 | + </main> |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + const isPro = tier === "pro"; |
| 75 | + |
| 76 | + return ( |
| 77 | + <main className="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 dark:from-neutral-950 dark:to-neutral-900 pb-24"> |
| 78 | + <section className="max-w-2xl mx-auto px-4 pt-10 pb-6"> |
| 79 | + <p className="text-xs font-semibold tracking-widest text-slate-400 uppercase mb-1">Account</p> |
| 80 | + <h1 className="text-3xl font-bold text-slate-900 dark:text-white leading-tight"> |
| 81 | + {user?.email ?? "Account"} |
| 82 | + </h1> |
| 83 | + </section> |
| 84 | + |
| 85 | + <div className="max-w-2xl mx-auto px-4 space-y-6"> |
| 86 | + {/* Tier card */} |
| 87 | + <div className="bg-white dark:bg-neutral-900 rounded-2xl border border-slate-100 dark:border-white/10 shadow-sm p-6"> |
| 88 | + <div className="flex items-center justify-between mb-4"> |
| 89 | + <div className="flex items-center gap-3"> |
| 90 | + <div className={`px-3 py-1 rounded-full text-xs font-bold ${ |
| 91 | + isPro |
| 92 | + ? "bg-indigo-600 text-white" |
| 93 | + : "bg-slate-200 dark:bg-white/10 text-slate-600 dark:text-slate-400" |
| 94 | + }`}> |
| 95 | + {isPro ? "PRO" : "FREE"} |
| 96 | + </div> |
| 97 | + <span className="text-sm text-slate-600 dark:text-slate-300"> |
| 98 | + {isPro ? "You have full access to all features." : "Upgrade for unlimited AI and exports."} |
| 99 | + </span> |
| 100 | + </div> |
| 101 | + {!isPro && ( |
| 102 | + <Link |
| 103 | + href="/pro" |
| 104 | + className="px-4 py-2 bg-indigo-600 text-white text-xs font-semibold rounded-xl hover:bg-indigo-500 transition-colors" |
| 105 | + > |
| 106 | + Upgrade to Pro |
| 107 | + </Link> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + |
| 111 | + {isPro && ( |
| 112 | + <p className="text-xs text-slate-400"> |
| 113 | + Thank you for supporting ColorArchive! Contact hello@colorarchive.me for billing questions. |
| 114 | + </p> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + |
| 118 | + {/* Usage stats */} |
| 119 | + {loading ? ( |
| 120 | + <div className="bg-white dark:bg-neutral-900 rounded-2xl border border-slate-100 dark:border-white/10 shadow-sm p-6"> |
| 121 | + <div className="h-24 bg-slate-100 dark:bg-white/5 rounded-xl animate-pulse" /> |
| 122 | + </div> |
| 123 | + ) : usage && ( |
| 124 | + <div className="bg-white dark:bg-neutral-900 rounded-2xl border border-slate-100 dark:border-white/10 shadow-sm p-6 space-y-5"> |
| 125 | + <h2 className="text-sm font-semibold text-slate-800 dark:text-white">Today's Usage</h2> |
| 126 | + <UsageBar |
| 127 | + label="AI Generations" |
| 128 | + used={usage.ai.used} |
| 129 | + limit={usage.ai.limit} |
| 130 | + /> |
| 131 | + <UsageBar |
| 132 | + label="Projects" |
| 133 | + used={usage.projects.count} |
| 134 | + limit={usage.projects.limit} |
| 135 | + /> |
| 136 | + <div className="flex items-center justify-between pt-2 border-t border-slate-100 dark:border-white/10"> |
| 137 | + <span className="text-xs text-slate-500 dark:text-slate-400">Favorites saved</span> |
| 138 | + <span className="text-xs font-semibold text-slate-700 dark:text-slate-300">{usage.favorites.count}</span> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + |
| 143 | + {/* Quick links */} |
| 144 | + <div className="bg-white dark:bg-neutral-900 rounded-2xl border border-slate-100 dark:border-white/10 shadow-sm p-6"> |
| 145 | + <h2 className="text-sm font-semibold text-slate-800 dark:text-white mb-4">Quick Links</h2> |
| 146 | + <div className="grid grid-cols-2 gap-3"> |
| 147 | + {[ |
| 148 | + { href: "/projects/", label: "My Projects", desc: `${usage?.projects.count ?? 0} saved` }, |
| 149 | + { href: "/favorites/", label: "Favorites", desc: `${usage?.favorites.count ?? 0} colors` }, |
| 150 | + { href: "/brand-generator/", label: "Brand Generator", desc: "AI palette" }, |
| 151 | + { href: "/mood-palette/", label: "Mood Palette", desc: "AI moods" }, |
| 152 | + ].map(({ href, label, desc }) => ( |
| 153 | + <Link |
| 154 | + key={href} |
| 155 | + href={href} |
| 156 | + className="flex items-center justify-between p-3 rounded-xl border border-slate-100 dark:border-white/10 hover:border-indigo-200 dark:hover:border-indigo-800 transition-colors" |
| 157 | + > |
| 158 | + <span className="text-sm font-medium text-slate-700 dark:text-slate-300">{label}</span> |
| 159 | + <span className="text-[10px] text-slate-400">{desc}</span> |
| 160 | + </Link> |
| 161 | + ))} |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + |
| 165 | + {/* Sign out */} |
| 166 | + <button |
| 167 | + onClick={() => { logout(); window.location.href = "/"; }} |
| 168 | + className="w-full text-center py-2.5 text-sm text-slate-500 dark:text-slate-400 hover:text-red-500 transition-colors" |
| 169 | + > |
| 170 | + Sign out |
| 171 | + </button> |
| 172 | + </div> |
| 173 | + </main> |
| 174 | + ); |
| 175 | +} |
0 commit comments