import { headers } from "next/headers"; import { redirect } from "next/navigation"; import prisma from "@/lib/prisma"; import Link from "next/link"; import { UserAvatar } from "@/app/components/UserAvatar"; import { GradeIcon, getGradeName } from "@/app/components/GradeIcon"; import { HeroBanner } from "@/app/components/HeroBanner"; import { PostList } from "@/app/components/PostList"; import ProfileLabelIcon from "@/app/svgs/profilelableicon"; import { ProfileImageEditor } from "@/app/components/ProfileImageEditor"; export default async function MyPage({ searchParams }: { searchParams: Promise<{ tab?: string; page?: string; sort?: string; q?: string }> }) { const sp = await searchParams; const activeTab = sp?.tab || "posts"; const page = parseInt(sp?.page || "1", 10); const sort = sp?.sort || "recent"; const q = sp?.q || ""; // 현재 로그인한 사용자 정보 가져오기 let currentUser: { userId: string; nickname: string; profileImage: string | null; points: number; level: number; grade: number; } | null = null; try { const h = await headers(); const cookieHeader = h.get("cookie") || ""; let isAdmin = false; const uid = cookieHeader .split(";") .map((s) => s.trim()) .find((pair) => pair.startsWith("uid=")) ?.split("=")[1]; const isAdminVal = cookieHeader .split(";") .map((s) => s.trim()) .find((pair) => pair.startsWith("isAdmin=")) ?.split("=")[1]; isAdmin = isAdminVal === "1"; if (!uid) { redirect(`/login?next=/my-page`); } if (uid) { const user = await prisma.user.findUnique({ where: { userId: decodeURIComponent(uid) }, select: { userId: true, nickname: true, profileImage: true, points: true, level: true, grade: true }, }); if (user) { currentUser = { ...user, isAdmin } as any; } else { redirect(`/login?next=/my-page`); } } } catch (e) { // 에러 무시 } if (!currentUser) redirect(`/login?next=/my-page`); // 통계 정보 가져오기 const [postsCount, commentsCount, receivedMessagesCount, sentMessagesCount] = await Promise.all([ prisma.post.count({ where: { authorId: currentUser.userId } }), prisma.comment.count({ where: { authorId: currentUser.userId } }), prisma.message.count({ where: { receiverId: currentUser.userId } }), prisma.message.count({ where: { senderId: currentUser.userId } }), ]); // 등급 업그레이드에 필요한 포인트 계산 (예시: 다음 등급까지) const nextGradePoints = (() => { // 등급별 포인트 기준을 여기에 설정 (임시로 2M) const gradeThresholds = [0, 200000, 400000, 800000, 1600000, 3200000, 6400000, 10000000]; const currentGradeIndex = Math.min(7, Math.max(0, currentUser.grade)); return gradeThresholds[currentGradeIndex + 1] || 2000000; })(); const tabs = [ { key: "posts", label: "내가 쓴 게시글", count: postsCount }, { key: "comments", label: "내가 쓴 댓글", count: commentsCount }, { key: "points", label: "포인트 히스토리", count: await prisma.pointTransaction.count({ where: { userId: currentUser.userId } }) }, { key: "messages-received", label: "받은 쪽지함", count: receivedMessagesCount }, { key: "messages-sent", label: "보낸 쪽지함", count: sentMessagesCount }, ]; return (