223 lines
6.4 KiB
TypeScript
223 lines
6.4 KiB
TypeScript
"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 (
|
|
<div className="inline-flex items-center gap-1 rounded-sm px-2 py-1 text-xs font-medium bg-muted/50 border border-border">
|
|
<div
|
|
className="w-2 h-2 rounded-full shrink-0"
|
|
style={{ backgroundColor: colorHex }}
|
|
/>
|
|
<span className="text-foreground max-w-40 truncate">
|
|
{role.name}
|
|
</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<div>
|
|
<div className="relative w-full bg-muted" style={bannerStyle} />
|
|
<div className="relative px-3 pb-3">
|
|
<div className="absolute" style={{ top: -56, left: 12 }}>
|
|
<div className="relative">
|
|
<div className="rounded-full bg-popover p-1.5">
|
|
<DiscordAvatar
|
|
avatarUrl={displayAvatar || null}
|
|
decorationUrl={profile?.avatarDecorationUrl}
|
|
alt={displayName}
|
|
fallback={initials(displayName)}
|
|
className="size-19"
|
|
imageError={imgError}
|
|
onImageError={onImageError}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div style={{ paddingTop: 36 }}>
|
|
<div className="flex min-w-0 items-center gap-2">
|
|
<div className="truncate font-bold text-lg leading-tight text-foreground">
|
|
{displayName}
|
|
</div>
|
|
{profile && <DiscordServerTag primaryGuild={profile.primaryGuild} />}
|
|
</div>
|
|
<div className="text-xs text-muted-foreground mt-1">
|
|
{username}
|
|
</div>
|
|
{profile && profile.roles.length > 0 && (
|
|
<div className="mt-2">
|
|
<div className="text-xs text-muted-foreground uppercase tracking-wider mb-2">
|
|
Roles / บทบาท
|
|
</div>
|
|
<div className="flex flex-wrap gap-1">
|
|
{profile.roles.map((role) => (
|
|
<RolePill key={role.id} role={role} />
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function DiscordProfileDropdown({
|
|
userName,
|
|
userImage = "",
|
|
profile,
|
|
children,
|
|
}: {
|
|
userName: string;
|
|
userImage?: string;
|
|
profile: DiscordMemberProfile;
|
|
children: ReactNode;
|
|
}) {
|
|
const [imgError, setImgError] = useState(false);
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>{children}</DropdownMenuTrigger>
|
|
<DropdownMenuContent
|
|
align="start"
|
|
className="overflow-hidden p-0"
|
|
style={{ width: 280 }}
|
|
>
|
|
<DiscordProfileCard
|
|
userName={userName}
|
|
userImage={userImage}
|
|
profile={profile}
|
|
imgError={imgError}
|
|
onImageError={() => setImgError(true)}
|
|
/>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" className={showName ? "pl-1 pr-2 flex items-center gap-2" : "h-9 w-9 rounded-full p-0 flex items-center justify-center"}>
|
|
<DiscordAvatar
|
|
avatarUrl={displayAvatar || null}
|
|
decorationUrl={profile?.avatarDecorationUrl}
|
|
alt={displayName}
|
|
fallback={initials(displayName)}
|
|
className="size-8"
|
|
imageError={imgError}
|
|
onImageError={() => setImgError(true)}
|
|
/>
|
|
{showName && <span className="text-sm font-medium">{displayName}</span>}
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent
|
|
align="end"
|
|
className="overflow-hidden p-0"
|
|
style={{ width: 280 }}
|
|
>
|
|
<DiscordProfileCard
|
|
userName={userName}
|
|
userImage={userImage}
|
|
profile={profile}
|
|
imgError={imgError}
|
|
onImageError={() => setImgError(true)}
|
|
/>
|
|
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuGroup className="p-2">
|
|
<DropdownMenuItem asChild>
|
|
<Link href="/profile">
|
|
<UserRound />
|
|
โปรไฟล์ของฉัน
|
|
</Link>
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
onClick={() => signOut({ callbackUrl: pathname })}
|
|
className="text-destructive"
|
|
>
|
|
<LogOut />
|
|
ออกจากระบบ
|
|
</DropdownMenuItem>
|
|
</DropdownMenuGroup>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
);
|
|
}
|