"use client"; import { useState } from "react"; import Link from "next/link"; import { FileText, MessageCircle } from "lucide-react"; import { fetchUserComments, type UserProfile, type UserCommentsResponse } from "@/lib/api"; import Pagination from "@/components/Pagination"; import { IconComment, IconEye, IconHeart } from "@/components/Icons"; interface UserProfileClientProps { userId: string; initialPosts: UserProfile; initialPage: number; } type Tab = "posts" | "comments"; export default function UserProfileClient({ userId, initialPosts, initialPage }: UserProfileClientProps) { const [tab, setTab] = useState("posts"); const [commentData, setCommentData] = useState(null); const [commentLoading, setCommentLoading] = useState(false); const [commentPage, setCommentPage] = useState(1); const loadComments = async (page: number) => { setCommentLoading(true); try { const data = await fetchUserComments(userId, page); setCommentData({ ...data, comments: data.comments || [] }); setCommentPage(page); } catch { setCommentData({ comments: [], total: 0, page, size: 20 }); } setCommentLoading(false); }; const handleTabChange = (t: Tab) => { setTab(t); if (t === "comments" && commentData === null) { loadComments(1); } }; const tabBtn = (t: Tab, label: string, count: number, icon: React.ReactNode) => { const active = tab === t; return ( ); }; return (
{tabBtn("posts", "发帖", initialPosts.stats.post_count, )} {tabBtn("comments", "评论", initialPosts.stats.comment_count, )}
{tab === "posts" ? (
{initialPosts.posts.length > 0 ? (
{initialPosts.posts.map((post, i) => (
{post.board.name} {post.pinned > 0 && 置顶} {post.recommended && 推荐}

{post.title}

{new Date(post.created_at).toLocaleDateString("zh-CN")} {post.comment_count} {post.view_count} {post.like_count}
))}
) : (

暂无发帖

)}
) : (
{commentLoading ? (

正在加载评论…

) : commentData && commentData.comments.length > 0 ? ( <>
{commentData.comments.map((c, i) => (
评论于《{c.post_title}》

{c.content}

{new Date(c.created_at).toLocaleString("zh-CN")}
))}
) : (

暂无评论

)}
)}
); }