refactor(profile): isolate resilient guild reads
This commit is contained in:
+6
-34
@@ -4,13 +4,7 @@ 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 { loadGuildProfile } from "@/lib/profile/guild-profile";
|
||||
import { ProfileLoginCard } from "@/components/profile/profile-login-card";
|
||||
import { ProfilePassport } from "@/components/profile/profile-passport";
|
||||
import { RealtimeRefresh } from "@/components/realtime-refresh";
|
||||
@@ -26,11 +20,6 @@ export const metadata: Metadata = {
|
||||
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);
|
||||
@@ -38,24 +27,7 @@ export default async function ProfilePage() {
|
||||
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);
|
||||
}
|
||||
const guildProfile = await loadGuildProfile(discordId);
|
||||
|
||||
content = (
|
||||
<>
|
||||
@@ -63,10 +35,10 @@ export default async function ProfilePage() {
|
||||
<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}
|
||||
profile={guildProfile.profile}
|
||||
profileUnavailable={guildProfile.profileUnavailable}
|
||||
xp={guildProfile.xp}
|
||||
voice={guildProfile.voice}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user