Files
erika/app/profile/page.tsx
T
2026-08-06 22:46:47 +07:00

92 lines
3.5 KiB
TypeScript

import type { Metadata } from "next";
import Link from "next/link";
import { getServerSession } from "next-auth";
import { ArrowLeft } from "lucide-react";
import { authOptions } from "@/lib/auth/auth-options";
import { getSessionDiscordId } from "@/lib/auth/auth";
import { getDiscordMemberProfile } from "@/lib/discord/discord";
import { getCurrentDiscordVoiceChannels } from "@/lib/leaderboard/discord";
import {
getMemberVoiceStats,
getMemberXpStats,
} from "@/lib/leaderboard/database";
import { buildLevelProgress } from "@/lib/leaderboard/level-progress";
import { ProfileLoginCard } from "@/components/profile/profile-login-card";
import { ProfilePassport } from "@/components/profile/profile-passport";
import { RealtimeRefresh } from "@/components/realtime-refresh";
import { ThemeToggle } from "@/components/theme-toggle";
import { Button } from "@/components/ui/button";
export const dynamic = "force-dynamic";
export const metadata: Metadata = {
title: "โปรไฟล์ของฉัน",
description: "ดูเลเวล XP เวลาในห้องเสียง และอันดับ Discord ของคุณใน Erika!",
alternates: { canonical: "/profile" },
robots: { index: false, follow: false },
};
async function loadVoiceStats(discordId: string) {
const channels = await getCurrentDiscordVoiceChannels();
return getMemberVoiceStats(discordId, channels.map((channel) => channel.id));
}
export default async function ProfilePage() {
const session = await getServerSession(authOptions);
const discordId = getSessionDiscordId(session);
let content = <ProfileLoginCard />;
if (session?.user && discordId) {
const [profileResult, xpResult, voiceResult] = await Promise.allSettled([
getDiscordMemberProfile(discordId),
getMemberXpStats(discordId),
loadVoiceStats(discordId),
]);
const profile = profileResult.status === "fulfilled" ? profileResult.value : null;
const xpStats = xpResult.status === "fulfilled" ? xpResult.value : null;
const voiceStats = voiceResult.status === "fulfilled" ? voiceResult.value : null;
if (profileResult.status === "rejected") {
console.error("Failed to load own Discord profile", profileResult.reason);
}
if (xpResult.status === "rejected") {
console.error("Failed to load own XP stats", xpResult.reason);
}
if (voiceResult.status === "rejected") {
console.error("Failed to load own voice stats", voiceResult.reason);
}
content = (
<>
<RealtimeRefresh topic="leaderboards" leaderboard="profile" />
<ProfilePassport
userName={session.user.name ?? ""}
userImage={session.user.image ?? ""}
profile={profile}
profileUnavailable={!profile}
xp={xpStats ? { ...buildLevelProgress(xpStats.level, xpStats.totalXp), rank: xpStats.rank } : null}
voice={voiceStats}
/>
</>
);
}
return (
<main className="relative min-h-screen overflow-hidden bg-background text-foreground">
<div className="relative mx-auto flex w-full max-w-5xl flex-col gap-6 px-4 py-6 sm:px-6 lg:py-10">
<nav aria-label="ทางลัด" className="flex items-center justify-between gap-3">
<Button variant="ghost" asChild>
<Link href="/">
<ArrowLeft data-icon="inline-start" />
กลับหน้าหลัก
</Link>
</Button>
<ThemeToggle />
</nav>
{content}
</div>
</main>
);
}