"use client";
import { useState, type ReactNode } from "react";
import { usePathname } from "next/navigation";
import Link from "next/link";
import { LogOut, UserRound } from "lucide-react";
import { signOut } from "next-auth/react";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { DiscordAvatar } from "@/components/discord-avatar";
import { DiscordServerTag } from "@/components/discord-server-tag";
import type {
DiscordMemberProfile as DiscordMemberProfileData,
DiscordRole,
} from "@/lib/discord/discord";
import {
discordColorToHex,
} from "@/lib/discord/profile-appearance";
export type DiscordMemberProfile = DiscordMemberProfileData;
const DEFAULT_ROLE_COLOR = "#99aab5";
function initials(value: string) {
return value.trim().slice(0, 2).toLocaleUpperCase("th-TH") || "?";
}
function RolePill({ role }: { role: DiscordRole }) {
const colorHex = role.color === 0
? DEFAULT_ROLE_COLOR
: discordColorToHex(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,
backgroundImage: profile?.bannerUrl ? `url(${profile.bannerUrl})` : undefined,
backgroundSize: "cover" as const,
backgroundPosition: "center" as const,
};
return (
{displayName}
{profile &&
}
{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)}
/>
โปรไฟล์ของฉัน
signOut({ callbackUrl: pathname })}
className="text-destructive"
>
ออกจากระบบ
);
}