'use client'; // 用户主页:资料卡 + 该用户的帖子列表 import { useEffect, useState, useCallback, useRef } from 'react'; import { API } from '@/lib/api'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; import { useOpenPrivateChat } from './chat/ChatManager'; import { renderMarkdown } from '@/lib/markdown'; import { useTranslation } from 'react-i18next'; function avatar(username, size = 128) { return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') + '&background=6366f1&color=fff&size=' + size; } export default function UserProfile({ username, initialUser = null, initialPosts = null }) { const { currentUser, openPost } = useApp(); const { t, i18n } = useTranslation(); const openPrivateChat = useOpenPrivateChat(); const [user, setUser] = useState(initialUser); const [posts, setPosts] = useState(initialPosts || []); const [error, setError] = useState(null); const postsRef = useRef(null); const load = useCallback(async () => { setError(null); try { const u = await API.getUserProfile(username); if (u.error || !u.id) { setError(u.error || '用户不存在'); return; } setUser(u); const result = await API.getPosts('latest', u.id); setPosts(result.data || []); } catch (err) { setError(err.message || '加载失败'); } }, [username]); useEffect(() => { setUser(initialUser); setPosts(initialPosts || []); load(); }, [initialPosts, initialUser, load]); useEffect(() => { const contents = postsRef.current?.querySelectorAll('.user-post-preview') || []; contents.forEach((element) => { element.classList.toggle('has-fade', element.scrollHeight > element.clientHeight + 4); }); }, [posts]); if (error) { return (
{user.bio || '这个人很懒,什么都没写'}
{currentUser && currentUser.username !== user.username && ( )}