"use client";
import React, { useState, useMemo } from "react";
import Image from "next/image";
import { LogOut } from "lucide-react";
import { signOut } from "next-auth/react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { DiscordRole } from "@/app/actions/discord";
export interface DiscordMemberProfile {
id: string;
username: string;
globalName: string | null;
avatarUrl: string | null;
bannerUrl: string | null;
accentColor: number | null;
roles: DiscordRole[];
}
const toHex = (n: number) => `#${n.toString(16).padStart(6, "0")}`;
const DEFAULT_ROLE_COLOR = "#99aab5";
function RolePill({ role }: { role: DiscordRole }) {
const colorHex = role.color === 0 ? DEFAULT_ROLE_COLOR : toHex(role.color);
return (
);
}
export function DropdownMenuAvatar({
userName,
userImage,
profile,
showName = true,
}: {
userName: string;
userImage: string;
profile: DiscordMemberProfile | null;
showName?: boolean;
}) {
const [imgError, setImgError] = useState(false);
const displayAvatar = profile?.avatarUrl || userImage;
const displayName = profile?.globalName || userName;
const username = profile?.username || userName;
const bannerStyle = useMemo(
() => ({
height: 100,
backgroundColor: profile?.accentColor
? toHex(profile.accentColor)
: "hsl(var(--muted))",
backgroundImage: profile?.bannerUrl
? `url(${profile.bannerUrl})`
: "none",
backgroundSize: "cover" as const,
backgroundPosition: "center" as const,
}),
[profile?.accentColor, profile?.bannerUrl]
);
return (
{/* Banner Section */}
{/* Body Area */}
{/* Avatar - Absolute Positioned to overlap banner and body */}
{!imgError && displayAvatar && (
setImgError(true)}
unoptimized
/>
)}
{/* Profile Info Section */}
{displayName}
{username}
{/* Roles Section */}
{profile && profile.roles.length > 0 && (
Roles / บทบาท
{profile.roles.map((role) => (
))}
)}
{/* Footer / Sign Out */}
signOut({ callbackUrl: "/" })}
className="text-destructive"
>
ออกจากระบบ
);
}