"use client" import { useState } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Badge } from "@/components/ui/badge" import { Alert, AlertDescription } from "@/components/ui/alert" import { User, Wallet, CheckCircle2, AlertCircle, Loader2, Sparkles, Shield } from "lucide-react" import { toast } from "react-hot-toast" import api from "@/lib/api" interface UsernameSetupProps { userProfile: { user_id: string wallet_address?: string display_name?: string username_set?: boolean avatar_url?: string } onUsernameSet: (profile: any) => void } export function UsernameSetup({ userProfile, onUsernameSet }: UsernameSetupProps) { const [username, setUsername] = useState("") const [isSubmitting, setIsSubmitting] = useState(false) const handleSubmitUsername = async () => { if (!username.trim()) { toast.error("Please enter a username") return } if (username.length < 3) { toast.error("Username must be at least 3 characters long") return } setIsSubmitting(true) try { let response try { response = await api.post("/api/dashboard/set-username", { username: username.trim() }) } catch (error) { // Fallback to update-profile response = await api.post("/api/dashboard/update-profile", { display_name: username.trim() }) } if (response.success) { toast.success(`Username "${username}" set successfully! 🎉`) onUsernameSet(response.profile || { ...userProfile, display_name: username.trim(), username_set: true }) } else { toast.error(response.error || "Failed to set username") } } catch (error: any) { console.error('Username setting error:', error) toast.error("Failed to set username. Please try again.") } finally { setIsSubmitting(false) } } const walletAddress = userProfile?.wallet_address || userProfile?.user_id return (
Welcome to OpenLearnX! 🎓
MetaMask Connected

{walletAddress?.slice(0, 6)}...{walletAddress?.slice(-4)}

setUsername(e.target.value)} placeholder="Enter your username" maxLength={25} disabled={isSubmitting} />

• 3-25 characters

• Letters, numbers, and underscores recommended

What you'll get:

  • • Personalized learning dashboard
  • • Global leaderboard ranking
  • • Blockchain-verified achievements
  • • Community interaction
) }