import debug from 'debug'; const rpcLogger = debug('frontend:api:rpc'); import React, { useEffect, useState } from 'react'; import { UserIcon, EditIcon, ExternalLinkIcon, Loader2Icon } from '@heroicons/react/24/outline'; import { useParams, useNavigate } from 'react-router-dom'; import { userClient } from '@/client/api'; import { useAuth } from '@/client/home/hooks/AuthProvider'; import type { UserEntity } from '@/server/modules/users/user.entity'; const UserProfilePage: React.FC = () => { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); const { id: userId } = useParams<{ id: string }>(); const id = Number(userId); const navigate = useNavigate(); const { user: currentUser } = useAuth(); // 获取用户资料 const fetchUserProfile = async () => { if (!id) return; try { setLoading(true); rpcLogger('Fetching user profile for id: %s', id); const response = await userClient[':id'].$get({ param: { id } }); if (!response.ok) throw new Error('获取用户资料失败'); const userData = await response.json(); setUser(userData); } catch (error) { rpcLogger.error('Error fetching user profile:', error); } finally { setLoading(false); } }; useEffect(() => { fetchUserProfile(); }, [id]); if (loading) { return (
); } if (!user) { return (
用户不存在
); } return (
} size={128} style={{ marginBottom: 16 }} /> {user.nickname || user.username}
0
帖子
{followerCount}
粉丝
{followingCount}
关注
{currentUser && currentUser.id !== user.id ? ( ) : currentUser && currentUser.id === user.id ? ( ) : null} {user.bio && ( {user.bio} )}
{user.location && ( {user.location} )} {user.website && ( window.open(user.website, '_blank')} > {user.website} )}
( }> {item.likesCount > 0 && item.likesCount} ]} > {item.content} {item.images?.map((img, idx) => ( {`Post ))} {new Date(item.createdAt).toLocaleString()} } /> )} locale={{ emptyText: '该用户暂无帖子' }} /> ( {/* 关注/取消关注逻辑 */}} > {item.isFollowing ? '已关注' : '关注'} ]} > } />} title={ navigate(`/users/${item.id}`)} > {item.nickname || item.username} } description={item.bio || '暂无简介'} /> )} locale={{ emptyText: '该用户暂无关注' }} /> ( {/* 关注/取消关注逻辑 */}} > {item.isFollowing ? '已关注' : '关注'} ]} > } />} title={ navigate(`/users/${item.id}`)} > {item.nickname || item.username} } description={item.bio || '暂无简介'} /> )} locale={{ emptyText: '该用户暂无粉丝' }} />
); }; export default UserProfilePage;