fix: polish forum navigation and interface states
This commit is contained in:
1 parent
0933220c70
commit
6947cdaf36
33 files changed
+1615
-360
No files matched your search
+73
-64
@@ -5,18 +5,21 @@ import { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { API, uploadImage } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
import ReplyList from './ReplyList';
|
||||
import Sidebar from './Sidebar';
|
||||
import { Icon } from './Icons';
|
||||
import ImageViewer from './ImageViewer';
|
||||
import { renderMarkdown } from '@/lib/markdown';
|
||||
import { useToast } from './Toast';
|
||||
|
||||
function avatar(username) {
|
||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
||||
'&background=6366f1&color=fff&size=64';
|
||||
}
|
||||
|
||||
export default function PostDetail({ postId }) {
|
||||
export default function PostDetail({ postId, initialPost = null }) {
|
||||
const { currentUser, navigate, openUser, refreshKey } = useApp();
|
||||
const [post, setPost] = useState(null);
|
||||
const { toast, confirmAction } = useToast();
|
||||
const [post, setPost] = useState(initialPost);
|
||||
const [error, setError] = useState(null);
|
||||
const [viewerSrc, setViewerSrc] = useState(null);
|
||||
const [editing, setEditing] = useState(false);
|
||||
@@ -46,7 +49,7 @@ export default function PostDetail({ postId }) {
|
||||
);
|
||||
}
|
||||
if (!post) {
|
||||
return <div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}>加载中...</div>;
|
||||
return <div id="pagePost" className="page-slide active" style={{ textAlign: 'center', color: '#94a3b8' }}><span className="spinner-sm" />加载中...</div>;
|
||||
}
|
||||
|
||||
const time = post.created_at ? new Date(post.created_at).toLocaleString('zh-CN') : '';
|
||||
@@ -55,23 +58,23 @@ export default function PostDetail({ postId }) {
|
||||
const canEdit = currentUser && currentUser.id === post.user_id;
|
||||
|
||||
const handleDelete = async () => {
|
||||
if (!confirm('确定要删除这条帖子吗?')) return;
|
||||
if (!await confirmAction('确定要删除这条帖子吗?')) return;
|
||||
try {
|
||||
await API.deletePost(postId);
|
||||
alert('删除成功');
|
||||
toast('删除成功', 'success');
|
||||
navigate('feed');
|
||||
} catch (err) {
|
||||
alert('删除失败:' + err.message);
|
||||
toast('删除失败:' + err.message, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const handleTogglePin = async () => {
|
||||
if (!confirm('确定要' + (post.is_pinned ? '取消' : '') + '置顶吗?')) return;
|
||||
if (!await confirmAction('确定要' + (post.is_pinned ? '取消' : '') + '置顶吗?', { danger: false })) return;
|
||||
try {
|
||||
await API.togglePinPost(postId);
|
||||
load();
|
||||
} catch (err) {
|
||||
alert('操作失败:' + err.message);
|
||||
toast('操作失败:' + err.message, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -83,27 +86,27 @@ export default function PostDetail({ postId }) {
|
||||
};
|
||||
|
||||
const handleSaveEdit = async () => {
|
||||
if (!editContent.trim()) { alert('请填写内容'); return; }
|
||||
if (!editContent.trim()) { toast('请填写内容', 'warning'); return; }
|
||||
try {
|
||||
await API.updatePost(postId, editTitle.trim() || '无标题', editContent.trim(), editImages);
|
||||
setEditing(false);
|
||||
load();
|
||||
} catch (err) {
|
||||
alert('编辑失败:' + err.message);
|
||||
toast('编辑失败:' + err.message, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
// 编辑时上传新图片
|
||||
const handleEditUpload = async (file) => {
|
||||
if (!file) return;
|
||||
if (!file.type.startsWith('image/')) { alert('请选择图片文件'); return; }
|
||||
if (file.size > 5 * 1024 * 1024) { alert('图片不能超过 5MB'); return; }
|
||||
if (!file.type.startsWith('image/')) { toast('请选择图片文件', 'warning'); return; }
|
||||
if (file.size > 5 * 1024 * 1024) { toast('图片不能超过 5MB', 'warning'); return; }
|
||||
setEditUploading(true);
|
||||
try {
|
||||
const url = await uploadImage(file);
|
||||
setEditImages((prev) => [...prev, url]);
|
||||
} catch (err) {
|
||||
alert('上传失败:' + err.message);
|
||||
toast('上传失败:' + err.message, 'error');
|
||||
} finally {
|
||||
setEditUploading(false);
|
||||
}
|
||||
@@ -116,68 +119,74 @@ export default function PostDetail({ postId }) {
|
||||
|
||||
return (
|
||||
<div id="pagePost" className="page-slide active">
|
||||
<div className="page-header" style={{ maxWidth: 700, margin: '0 auto', width: '100%' }}>
|
||||
<div className="page-header post-page-header">
|
||||
<button className="back-btn" onClick={() => navigate('feed')}>
|
||||
<Icon name="back" size={16} /> 返回
|
||||
</button>
|
||||
<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={post.avatar_url || avatar(post.username)} className="post-avatar" alt="" />
|
||||
<span
|
||||
className="post-username"
|
||||
style={{ cursor: 'pointer', color: 'var(--primary)' }}
|
||||
onClick={() => openUser(post.username)}
|
||||
>
|
||||
{post.username || '匿名用户'}
|
||||
</span>
|
||||
<span className="post-time">{time}</span>
|
||||
{post.edited_at && (
|
||||
<span style={{ fontSize: 12, color: 'var(--text-light)', marginLeft: 8 }}>
|
||||
(已编辑 {new Date(post.edited_at).toLocaleString('zh-CN')})
|
||||
<div className="post-page-layout">
|
||||
<Sidebar id="postSidebar" />
|
||||
<div id="postDetailContent" className="post-page-main">
|
||||
<div className="post-detail-card">
|
||||
<div className="post-header">
|
||||
<img src={post.avatar_url || avatar(post.username)} className="post-avatar" alt="" />
|
||||
<span
|
||||
className="post-username"
|
||||
style={{ cursor: 'pointer', color: 'var(--primary)' }}
|
||||
onClick={() => openUser(post.username)}
|
||||
>
|
||||
{post.username || '匿名用户'}
|
||||
</span>
|
||||
)}
|
||||
{post.is_pinned && (
|
||||
<span style={{ fontSize: 12, color: 'var(--primary)', marginLeft: 8 }}>📌 置顶</span>
|
||||
)}
|
||||
<div style={{ display: 'flex', gap: 4, marginLeft: 'auto' }}>
|
||||
{currentUser && currentUser.role === 'admin' && (
|
||||
<button className="btn-sm btn-secondary" style={{ padding: '4px 10px' }} onClick={handleTogglePin}>
|
||||
{post.is_pinned ? '📌 取消置顶' : '📌 置顶'}
|
||||
</button>
|
||||
<span className="post-time">{time}</span>
|
||||
{post.edited_at && (
|
||||
<span style={{ fontSize: 12, color: 'var(--text-light)', marginLeft: 8 }}>
|
||||
(已编辑 {new Date(post.edited_at).toLocaleString('zh-CN')})
|
||||
</span>
|
||||
)}
|
||||
{canDelete && (
|
||||
<button className="btn-sm btn-secondary" style={{ padding: '4px 10px' }} onClick={handleEdit}>
|
||||
✏️ 编辑
|
||||
</button>
|
||||
)}
|
||||
{canDelete && (
|
||||
<button className="btn-sm btn-danger" style={{ padding: '4px 10px' }} onClick={handleDelete}>
|
||||
<Icon name="trash" size={14} /> 删除
|
||||
</button>
|
||||
{post.is_pinned && (
|
||||
<span style={{ fontSize: 12, color: 'var(--primary)', marginLeft: 8, display: 'inline-flex', alignItems: 'center', gap: 3 }}><Icon name="pin" size={12} /> 置顶</span>
|
||||
)}
|
||||
<div style={{ display: 'flex', gap: 4, marginLeft: 'auto' }}>
|
||||
{currentUser && currentUser.role === 'admin' && (
|
||||
<button className="btn-sm btn-secondary" style={{ padding: '4px 10px' }} onClick={handleTogglePin}>
|
||||
<Icon name="pin" size={12} /> {post.is_pinned ? '取消置顶' : '置顶'}
|
||||
</button>
|
||||
)}
|
||||
{canEdit && (
|
||||
<button className="btn-sm btn-secondary" style={{ padding: '4px 10px' }} onClick={handleEdit}>
|
||||
<Icon name="edit" size={12} /> 编辑
|
||||
</button>
|
||||
)}
|
||||
{canDelete && (
|
||||
<button className="btn-sm btn-danger" style={{ padding: '4px 10px' }} onClick={handleDelete}>
|
||||
<Icon name="trash" size={14} /> 删除
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="post-content" style={{ fontSize: 16, lineHeight: 1.8 }} dangerouslySetInnerHTML={{ __html: renderMarkdown(post.content) }} />
|
||||
{post.images && post.images.length > 0 && (
|
||||
<div className="post-images">
|
||||
{post.images.map((img, i) => (
|
||||
<img key={i} src={img} className="post-image" alt="" style={{ cursor: 'pointer' }} onClick={() => setViewerSrc(img)} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{post.signature && (
|
||||
<div style={{ marginTop: 16, paddingTop: 12, borderTop: '1px solid var(--border-light)', fontSize: 12, color: 'var(--text-secondary)' }} dangerouslySetInnerHTML={{ __html: renderMarkdown(post.signature) }} />
|
||||
)}
|
||||
</div>
|
||||
<div className="post-content" style={{ fontSize: 16, lineHeight: 1.8 }} dangerouslySetInnerHTML={{ __html: renderMarkdown(post.content) }} />
|
||||
{post.images && post.images.length > 0 && (
|
||||
<div className="post-images">
|
||||
{post.images.map((img, i) => (
|
||||
<img key={i} src={img} className="post-image" alt="" style={{ cursor: 'pointer' }} onClick={() => setViewerSrc(img)} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{post.signature && (
|
||||
<div style={{ marginTop: 16, paddingTop: 12, borderTop: '1px solid var(--border-light)', fontSize: 12, color: 'var(--text-secondary)' }} dangerouslySetInnerHTML={{ __html: renderMarkdown(post.signature) }} />
|
||||
)}
|
||||
<ReplyList postId={postId} onRefresh={load} />
|
||||
</div>
|
||||
<ReplyList postId={postId} onRefresh={load} />
|
||||
</div>
|
||||
|
||||
{/* 编辑帖子 modal */}
|
||||
{editing && (
|
||||
<div className="modal active" style={{ display: 'flex' }} onClick={(e) => { if (e.target === e.currentTarget) setEditing(false); }}>
|
||||
<div className="modal-content" style={{ maxWidth: 600, position: 'relative' }}>
|
||||
<span className="close" style={{ position: 'absolute', top: 12, right: 16, fontSize: 24, cursor: 'pointer', color: 'var(--text-light)' }} onClick={() => setEditing(false)}>×</span>
|
||||
<h2 style={{ marginBottom: 16 }}>✏️ 编辑帖子</h2>
|
||||
<span className="close" style={{ position: 'absolute', top: 12, right: 16, cursor: 'pointer', color: 'var(--text-light)' }} onClick={() => setEditing(false)}><Icon name="close" size={20} /></span>
|
||||
<h2 style={{ marginBottom: 16 }}><Icon name="edit" size={20} /> 编辑帖子</h2>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="标题"
|
||||
@@ -202,15 +211,15 @@ export default function PostDetail({ postId }) {
|
||||
type="button"
|
||||
title="删除图片"
|
||||
onClick={() => handleEditRemoveImage(img)}
|
||||
style={{ position: 'absolute', top: -6, right: -6, width: 20, height: 20, borderRadius: '50%', background: '#ef4444', color: '#fff', border: 'none', cursor: 'pointer', fontSize: 12, lineHeight: 1, display: 'flex', alignItems: 'center', justifyContent: 'center' }}
|
||||
>×</button>
|
||||
style={{ position: 'absolute', top: -6, right: -6, width: 20, height: 20, borderRadius: '50%', background: '#ef4444', color: '#fff', border: 'none', cursor: 'pointer', lineHeight: 1, display: 'flex', alignItems: 'center', justifyContent: 'center' }}
|
||||
><Icon name="close" size={12} /></button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div style={{ display: 'flex', gap: 8, marginTop: 12 }}>
|
||||
<button type="button" className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={() => editImageInputRef.current?.click()}>
|
||||
{editUploading ? '上传中...' : '📷 上传图片'}
|
||||
{editUploading ? '上传中...' : <><Icon name="camera" size={14} /> 上传图片</>}
|
||||
</button>
|
||||
<input type="file" ref={editImageInputRef} accept="image/*" multiple style={{ display: 'none' }} onChange={(e) => { const files = e.target.files; if (files && files.length > 0) { [...files].forEach((f) => handleEditUpload(f)); e.target.value = ''; } }} />
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user