feat: animate user profile transitions

This commit is contained in:
lezi-fun committed 2026-08-12 10:24:20 +08:00
1 parent 8e67e82eb0
commit bbf3262aff
6 files changed
+467 -63

No files matched your search

+51 -39
View File
@@ -12,10 +12,10 @@ function avatar(username, size = 128) {
'&background=6366f1&color=fff&size=' + size;
}
export default function UserProfile({ username }) {
export default function UserProfile({ username, initialUser = null }) {
const { currentUser, openPost } = useApp();
const openPrivateChat = useOpenPrivateChat();
const [user, setUser] = useState(null);
const [user, setUser] = useState(initialUser);
const [posts, setPosts] = useState([]);
const [error, setError] = useState(null);
@@ -35,60 +35,72 @@ export default function UserProfile({ username }) {
}
}, [username]);
useEffect(() => { load(); }, [load]);
useEffect(() => {
setUser(initialUser);
setPosts([]);
load();
}, [initialUser, load]);
if (error) {
return (
<div className="page-slide active">
<div id="pageUser" className="page-slide active">
<div style={{ textAlign: 'center', color: '#ef4444', padding: '40px 0' }}>加载失败:{error}</div>
</div>
);
}
if (!user) {
return <div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}><span className="spinner-sm" />加载中...</div>;
return <div id="pageUser" className="page-slide active" style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}><span className="spinner-sm" />加载中...</div>;
}
return (
<div className="page-slide active">
<div id="pageUser" className="page-slide active">
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<h2><Icon name="users" size={20} /> 用户主页</h2>
</div>
<div style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<div style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 32, textAlign: 'center' }}>
<img src={user.avatar_url || avatar(user.username)} style={{ width: 96, height: 96, borderRadius: '50%', objectFit: 'cover', border: '3px solid var(--primary)' }} alt="" />
<h2 style={{ margin: '16px 0 4px', fontSize: 24 }}>{user.username}</h2>
<p style={{ color: 'var(--text-secondary)', fontSize: 14 }}>{user.bio || '这个人很懒,什么都没写'}</p>
{currentUser && currentUser.id !== user.id && (
<button
className="btn-primary"
style={{ marginTop: 12, padding: '8px 20px' }}
onClick={() => openPrivateChat(user.id, user.username)}
>
<Icon name="message" size={14} /> 发私信
</button>
)}
<div style={{ display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, fontSize: 14, color: 'var(--text-secondary)', flexWrap: 'wrap' }}>
<span><Icon name="calendar" size={14} /> 加入于 {user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
<span><Icon name="file" size={14} /> 发了 {posts.length} 个帖子</span>
{user.role === 'admin' && <span style={{ color: 'var(--primary)', fontWeight: 600 }}><Icon name="shield" size={14} /> 管理员</span>}
<div id="userProfileContent" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<div className="user-profile-card" style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 32, textAlign: 'center' }}>
<img className="user-profile-avatar" src={user.avatar_url || avatar(user.username)} style={{ width: 96, height: 96, borderRadius: '50%', objectFit: 'cover', border: '3px solid var(--primary)' }} alt={user.username || ''} />
<h2 className="user-profile-name" style={{ margin: '16px 0 4px', fontSize: 24, color: 'var(--text)' }}>{user.username}</h2>
{user.id && (
<div className="user-profile-details">
<p style={{ color: 'var(--text-secondary)', fontSize: 14 }}>{user.bio || '这个人很懒,什么都没写'}</p>
{currentUser && currentUser.username !== user.username && (
<button
className="btn-primary"
style={{ marginTop: 12, padding: '8px 20px' }}
onClick={() => openPrivateChat(user.id, user.username)}
>
<Icon name="message" size={14} /> 发私信
</button>
)}
<div style={{ display: 'flex', justifyContent: 'center', gap: 32, marginTop: 16, fontSize: 14, color: 'var(--text-secondary)', flexWrap: 'wrap' }}>
<span><Icon name="calendar" size={14} /> 加入于 {user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
<span><Icon name="file" size={14} /> 发了 {posts.length} 个帖子</span>
{user.role === 'admin' && <span style={{ color: 'var(--primary)', fontWeight: 600 }}><Icon name="shield" size={14} /> 管理员</span>}
</div>
</div>
)}
</div>
<h3 style={{ margin: '24px 0 16px', fontSize: 18 }}><Icon name="file" size={18} /> 发布的帖子</h3>
{posts.length === 0 ? (
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div>
) : (
posts.map((p) => {
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
return (
<div key={p.id} className="post-card" style={{ cursor: 'pointer' }} onClick={(e) => openPost(p.id, e.currentTarget, p)}>
<div className="post-title" style={{ fontSize: 16, fontWeight: 600 }}>{p.title || '无标题'}</div>
<div className="post-content" style={{ fontSize: 14, color: 'var(--text-secondary)' }}>
{(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''}
{user.id && (
<div className="user-profile-posts">
<h3 style={{ margin: '24px 0 16px', fontSize: 18 }}><Icon name="file" size={18} /> 发布的帖子</h3>
{posts.length === 0 ? (
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div>
) : (
posts.map((p) => {
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
return (
<div key={p.id} className="post-card" style={{ cursor: 'pointer' }} onClick={(e) => openPost(p.id, e.currentTarget, p)}>
<div className="post-title" style={{ fontSize: 16, fontWeight: 600 }}>{p.title || '无标题'}</div>
<div className="post-content" style={{ fontSize: 14, color: 'var(--text-secondary)' }}>
{(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''}
</div>
<div style={{ fontSize: 12, color: 'var(--text-light)', marginTop: 8 }}>{time}</div>
</div>
<div style={{ fontSize: 12, color: 'var(--text-light)', marginTop: 8 }}>{time}</div>
</div>
);
})
);
})
)}
</div>
)}
</div>
</div>