import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Eye, FileText, Heart, Mail, MessageCircle, UserRound } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { api } from '../api/client'; import type { User, UserActivityStats, UserPublic } from '../api/types'; import { useAuth } from '../hooks/useAuth'; import { loginPath } from '../utils/authRedirect'; import { formatTime } from '../utils/content'; import { userPath } from '../utils/userPath'; import ComposeMessageDialog from './ComposeMessageDialog'; import UserLink from './UserLink'; interface Props { author?: User | null; publishedAt?: string; viewCount?: number; } /** 帖子详情右栏:作者信息卡(私信 / 主页 / 统计) */ export default function PostAuthorCard({ author, publishedAt, viewCount, }: Props) { const nav = useNavigate(); const { user: me } = useAuth(); const [profile, setProfile] = useState(null); const [stats, setStats] = useState(null); const [msgOpen, setMsgOpen] = useState(false); useEffect(() => { if (!author?.id) { setProfile(null); setStats(null); return; } let cancelled = false; api.userProfile(author.id) .then((r) => { if (cancelled) return; setProfile(r.user); setStats(r.stats); }) .catch(() => { if (cancelled) return; // 详情里已有轻量 user,接口失败时仍可展示基本信息 setProfile(null); setStats(null); }); return () => { cancelled = true; }; }, [author?.id]); if (!author?.id) { return (
作者
作者信息加载中…
); } const display = profile ?? author; const nick = display.nickname || display.username || `用户 #${author.id}`; const initial = nick.charAt(0) || '?'; const signature = (profile?.signature ?? author.signature ?? '').trim(); const isSelf = !!me && me.id === author.id; const profileHref = userPath(author.id); const openMessage = () => { if (!me) { nav(loginPath(profileHref)); return; } setMsgOpen(true); }; return (
作者
{display.avatar ? : initial}
{display.banned && 已禁言}
{signature ? (

{signature}

) : null} {(publishedAt || typeof viewCount === 'number') && (

{publishedAt ? {formatTime(publishedAt)} 发布 : null} {publishedAt && typeof viewCount === 'number' ? ( · ) : null} {typeof viewCount === 'number' ? ( {viewCount} ) : null}

)}
{stats?.post_count ?? '—'} 帖子
{stats?.comment_count ?? '—'} 评论
{stats?.like_received ?? '—'} 获赞
{!isSelf && ( )}
{!isSelf && ( nav(`/messages?peer=${author.id}`)} /> )}
); }