"use client" import { useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { UserAvatar } from "@/components/ui/user-avatar" import { getApiUrl } from "@/lib/api-config" import { Check } from "lucide-react" 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" }, ] interface AvatarSettingsProps { currentBgColor?: string currentText?: string username?: string onSave?: (bgColor: string, text: string) => void } export function AvatarSettings({ currentBgColor = "#3B82F6", currentText = "", username = "", onSave }: AvatarSettingsProps) { const [bgColor, setBgColor] = useState(currentBgColor) const [text, setText] = useState(currentText) const [saving, setSaving] = useState(false) const [error, setError] = useState("") const handleTextChange = (value: string) => { // Only allow alphanumeric, max 3 chars const cleaned = value.replace(/[^A-Za-z0-9]/g, '').slice(0, 3).toUpperCase() setText(cleaned) setError("") } const handleSave = async () => { setSaving(true) setError("") 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: bgColor, text: text || null, }), }) if (!res.ok) { const data = await res.json() throw new Error(data.detail || "Failed to save") } onSave?.(bgColor, text) } catch (e: any) { setError(e.message || "Failed to save avatar") } finally { setSaving(false) } } return ( Customize Avatar {/* Preview */}
{/* Color Selection */}
{PRESET_COLORS.map((color) => ( ))}
{/* Text Input */}
handleTextChange(e.target.value)} maxLength={3} className="text-center text-lg font-bold uppercase" />

Leave empty to show first letter of username

{error && (

{error}

)}
) }