'use client'; // 帖子详情页 import { useEffect, useState, useCallback } from 'react'; import { API } from '@/lib/api'; import { useApp } from './AppProvider'; import ReplyList from './ReplyList'; import { Icon } from './Icons'; function avatar(username) { return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') + '&background=6366f1&color=fff&size=64'; } export default function PostDetail({ postId }) { const { currentUser, navigate, refreshKey } = useApp(); const [post, setPost] = useState(null); const [error, setError] = useState(null); const load = useCallback(async () => { setError(null); try { const data = await API.getPost(postId); setPost(data); } catch (err) { setError(err.message); } }, [postId, refreshKey]); useEffect(() => { load(); }, [load]); if (error) { return (
加载失败:{error}
); } if (!post) { return
加载中...
; } const time = post.created_at ? new Date(post.created_at).toLocaleString('zh-CN') : ''; const canDelete = currentUser && currentUser.id === post.user_id; const handleDelete = async () => { if (!confirm('确定要删除这条帖子吗?')) return; try { await API.deletePost(postId); alert('删除成功'); navigate('feed'); } catch (err) { alert('删除失败:' + err.message); } }; return (

{post.title || '帖子详情'}

{post.username || '匿名用户'} {time} {canDelete && ( )}
{post.title || '无标题'}
{(post.content || '').split('\n').map((l, i) => {l}
)}
{post.images && post.images.length > 0 && (
{post.images.map((img, i) => )}
)}
); }