Files
forumlify/components/PostDetail.js
T
lezi-fun 70a198ae32 fix: restore original Feather icons across UI
- Add components/Icons.js with SVG paths extracted verbatim from the original index.html (14 unique icons)
- Restore icons on navbar menu, sidebar, feed actions, post/reply, modals, admin tabs and settings
- Replaces emoji placeholders from the initial migration
2026-08-09 18:12:46 +08:00

91 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'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 (
<div className="page-slide active" style={{ padding: '40px 0', textAlign: 'center', color: '#ef4444' }}>
加载失败:{error}
</div>
);
}
if (!post) {
return <div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}>加载中...</div>;
}
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 (
<div className="page-slide active">
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<h2>{post.title || '帖子详情'}</h2>
</div>
<div id="postDetailContent" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
<div className="post-detail-card">
<div className="post-header">
<img src={avatar(post.username)} className="post-avatar" alt="" />
<span className="post-username">{post.username || '匿名用户'}</span>
<span className="post-time">{time}</span>
{canDelete && (
<button className="btn-sm btn-danger" onClick={handleDelete}>
<Icon name="trash" size={14} /> 删除
</button>
)}
</div>
<div className="post-title" style={{ fontSize: 20, fontWeight: 700, margin: '8px 0 12px' }}>
{post.title || '无标题'}
</div>
<div className="post-content" style={{ fontSize: 16, lineHeight: 1.8 }}>
{(post.content || '').split('\n').map((l, i) => <span key={i}>{l}<br /></span>)}
</div>
{post.images && post.images.length > 0 && (
<div className="post-images">
{post.images.map((img, i) => <img key={i} src={img} className="post-image" alt="" />)}
</div>
)}
</div>
<ReplyList postId={postId} onRefresh={load} />
</div>
</div>
);
}