"use client" import { useState, useEffect } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Label } from "@/components/ui/label" import { Switch } from "@/components/ui/switch" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Button } from "@/components/ui/button" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Separator } from "@/components/ui/separator" import { usePreferences } from "@/contexts/preferences-context" import { useAuth } from "@/contexts/auth-context" import { getApiUrl } from "@/lib/api-config" import { UserAvatar } from "@/components/ui/user-avatar" import { User, Palette, Bell, Eye, Shield, Sparkles, Check, ArrowLeft } from "lucide-react" import Link from "next/link" // Avatar color palette - more distinct colors const PRESET_COLORS = [ { value: "#1E3A8A", name: "Deep Blue" }, { value: "#DC2626", name: "Bright Red" }, { value: "#059669", name: "Emerald" }, { value: "#D97706", name: "Amber" }, { value: "#7C3AED", name: "Violet" }, { value: "#DB2777", name: "Magenta" }, { value: "#0F766E", name: "Teal" }, { value: "#1F2937", name: "Slate" }, { value: "#B45309", name: "Burnt Orange" }, { value: "#4338CA", name: "Indigo" }, { value: "#0891B2", name: "Cyan" }, { value: "#65A30D", name: "Lime" }, ] export default function SettingsPage() { const { preferences, updatePreferences, loading } = usePreferences() const { user, refreshUser } = useAuth() // Profile state const [bio, setBio] = useState("") const [username, setUsername] = useState("") const [profileSaving, setProfileSaving] = useState(false) const [profileSaved, setProfileSaved] = useState(false) // Avatar state const [avatarBgColor, setAvatarBgColor] = useState("#1E3A8A") const [avatarText, setAvatarText] = useState("") const [avatarSaving, setAvatarSaving] = useState(false) const [avatarSaved, setAvatarSaved] = useState(false) const [avatarError, setAvatarError] = useState("") useEffect(() => { if (user) { const extUser = user as any setBio(extUser.bio || "") setUsername(extUser.email?.split('@')[0] || "") setAvatarBgColor(extUser.avatar_bg_color || "#1E3A8A") setAvatarText(extUser.avatar_text || "") } }, [user]) const handleSaveProfile = async () => { setProfileSaving(true) setProfileSaved(false) const token = localStorage.getItem("token") try { await fetch(`${getApiUrl()}/users/me`, { method: "PATCH", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}` }, body: JSON.stringify({ bio, username }) }) setProfileSaved(true) setTimeout(() => setProfileSaved(false), 2000) } catch (e) { console.error(e) } finally { setProfileSaving(false) } } const handleAvatarTextChange = (value: string) => { const cleaned = value.replace(/[^A-Za-z0-9]/g, '').slice(0, 3).toUpperCase() setAvatarText(cleaned) setAvatarError("") } const handleSaveAvatar = async () => { setAvatarSaving(true) setAvatarSaved(false) setAvatarError("") try { const token = localStorage.getItem("token") const res = await fetch(`${getApiUrl()}/users/me/avatar`, { method: "PATCH", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ bg_color: avatarBgColor, text: avatarText || null, }), }) if (!res.ok) { const data = await res.json() throw new Error(data.detail || "Failed to save") } setAvatarSaved(true) refreshUser?.() setTimeout(() => setAvatarSaved(false), 2000) } catch (e: any) { setAvatarError(e.message || "Failed to save avatar") } finally { setAvatarSaving(false) } } if (loading) { return (
You need to be logged in to access settings.
Manage your account and preferences
This will be shown on your profile and comments
Leave empty to show first letter of username
{error}
)}Account deletion is permanent and cannot be undone.
{description}