"use client";
import { useState, type ReactNode } from "react";
import { usePathname } from "next/navigation";
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 type { DiscordRole } from "@/lib/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 (
);
}
function DiscordProfileCard({
userName,
userImage,
profile,
imgError,
onImageError,
}: {
userName: string;
userImage: string;
profile: DiscordMemberProfile | null;
imgError: boolean;
onImageError: () => void;
}) {
const displayAvatar = profile?.avatarUrl || userImage;
const displayName = profile?.globalName || userName;
const username = profile?.username || userName;
const bannerStyle = {
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,
};
return (
<>
{!imgError && displayAvatar && (
)}
{displayName}
{username}
{profile && profile.roles.length > 0 && (
Roles / บทบาท
{profile.roles.map((role) => (
))}
)}
>
);
}
export function DiscordProfileDropdown({
userName,
userImage = "",
profile,
children,
}: {
userName: string;
userImage?: string;
profile: DiscordMemberProfile;
children: ReactNode;
}) {
const [imgError, setImgError] = useState(false);
return (
{children}
setImgError(true)}
/>
);
}
export function DropdownMenuAvatar({
userName,
userImage,
profile,
showName = true,
}: {
userName: string;
userImage: string;
profile: DiscordMemberProfile | null;
showName?: boolean;
}) {
const [imgError, setImgError] = useState(false);
const pathname = usePathname();
const displayAvatar = profile?.avatarUrl || userImage;
const displayName = profile?.globalName || userName;
return (
setImgError(true)}
/>
{/* Footer / Sign Out */}
signOut({ callbackUrl: pathname })}
className="text-destructive"
>
ออกจากระบบ
);
}