"use client" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { MAX_FREE_GENERATION } from "@/lib/constant" import { User } from "@/lib/types" import { useClerk } from "@clerk/nextjs" import { Crown, LayoutDashboard, LogOut, Sparkles, User as UserIcon, } from "lucide-react" import Link from "next/link" import { useRouter } from "next/navigation" import { useEffect, useState } from "react" import Avatar from "./avatar" import { Button } from "./button" import { TIERS } from "@/lib/tiers" // TODO: Remove this once we have a proper tier system const TIER_INFO = { FREE: { color: "text-gray-500", icon: Sparkles, limit: TIERS.FREE.generations, }, PRO: { color: "text-blue-500", icon: Crown, limit: TIERS.PRO.generations, }, ENTERPRISE: { color: "text-purple-500", icon: Crown, limit: TIERS.ENTERPRISE.generations, }, } as const export default function UserButton({ userData: initialUserData }: { userData: User }) { const [userData, setUserData] = useState(initialUserData) const [isOpen, setIsOpen] = useState(false) const { signOut } = useClerk() const router = useRouter() const fetchUserData = async () => { try { const res = await fetch( `${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${userData.id}`, { headers: { Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`, }, cache: 'no-store' } ) if (res.ok) { const updatedUserData = await res.json() setUserData(updatedUserData) } } catch (error) { console.error("Failed to fetch user data:", error) } } useEffect(() => { if (isOpen) { fetchUserData() } }, [isOpen]) const tierInfo = TIER_INFO[userData.tier as keyof typeof TIER_INFO] || TIER_INFO.FREE const TierIcon = tierInfo.icon const usagePercentage = Math.floor((userData.generations || 0) * 100 / tierInfo.limit) const handleUpgrade = async () => { router.push('/upgrade') } return (
{userData.name}
{userData.email}
Dashboard Profile
{userData.tier || "FREE"} Plan
{/* {(userData.tier === "FREE" || userData.tier === "PRO") && ( )} */}
AI Usage {userData.generations}/{tierInfo.limit}
90 ? 'bg-red-500' : usagePercentage > 75 ? 'bg-yellow-500' : tierInfo.color.replace('text-', 'bg-') }`} style={{ width: `${Math.min(usagePercentage, 100)}%`, }} />
{/* Edit Profile */} signOut(() => router.push("/"))} className="!text-destructive cursor-pointer" > Log Out ) }