fix: post detail client error (hooks order), image viewer, messages vs DM split
- Fix critical bug: useState declared after conditional return in PostDetail
caused 'Rendered more hooks than during previous render' client error
- Add image viewer (click post image to zoom, matching upstream openImageViewer)
- CSS upload warning confirm modal (matching upstream)
- Fix: navbar 消息 menu item now opens notifications page (messages), not DM
list — DM stays on the ✉️ nav button (matches upstream structure)
This commit is contained in:
1 parent
4ee38f6486
commit
2a508d1b21
5 files changed
+83
-10
No files matched your search
+7
-1
@@ -5,6 +5,7 @@ import { useEffect, useState, useCallback } from 'react';
|
|||||||
import { API } from '@/lib/api';
|
import { API } from '@/lib/api';
|
||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
|
import ImageViewer from './ImageViewer';
|
||||||
|
|
||||||
function avatar(username) {
|
function avatar(username) {
|
||||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
||||||
@@ -15,8 +16,10 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
const { currentUser, sort, setSort, openPost, openUser, navigate, refreshKey } = useApp();
|
const { currentUser, sort, setSort, openPost, openUser, navigate, refreshKey } = useApp();
|
||||||
const [posts, setPosts] = useState(null);
|
const [posts, setPosts] = useState(null);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
const [totalPages, setTotalPages] = useState(1);
|
const [totalPages, setTotalPages] = useState(1);
|
||||||
|
const [viewerSrc, setViewerSrc] = useState(null);
|
||||||
const PAGE_SIZE = 20;
|
const PAGE_SIZE = 20;
|
||||||
|
|
||||||
const load = useCallback(async (targetPage) => {
|
const load = useCallback(async (targetPage) => {
|
||||||
@@ -99,7 +102,9 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
<div className="post-content">{(p.content || '').split('\n').map((l, i) => <span key={i}>{l}<br /></span>)}</div>
|
<div className="post-content">{(p.content || '').split('\n').map((l, i) => <span key={i}>{l}<br /></span>)}</div>
|
||||||
{p.images && p.images.length > 0 && (
|
{p.images && p.images.length > 0 && (
|
||||||
<div className="post-images">
|
<div className="post-images">
|
||||||
{p.images.map((img, i) => <img key={i} src={img} className="post-image" alt="" />)}
|
{p.images.map((img, i) => (
|
||||||
|
<img key={i} src={img} className="post-image" alt="" style={{ cursor: 'pointer' }} onClick={(e) => { e.stopPropagation(); setViewerSrc(img); }} />
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div className="post-actions">
|
<div className="post-actions">
|
||||||
@@ -164,6 +169,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{viewerSrc && <ImageViewer src={viewerSrc} onClose={() => setViewerSrc(null)} />}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
// 图片查看器(点击帖子图片放大)
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
export default function ImageViewer({ src, onClose }) {
|
||||||
|
useEffect(() => {
|
||||||
|
const onKey = (e) => { if (e.key === 'Escape') onClose(); };
|
||||||
|
document.addEventListener('keydown', onKey);
|
||||||
|
return () => document.removeEventListener('keydown', onKey);
|
||||||
|
}, [onClose]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className="modal active"
|
||||||
|
style={{
|
||||||
|
position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.85)', zIndex: 9999,
|
||||||
|
display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||||
|
cursor: 'pointer', animation: 'fadeIn 0.2s ease-out',
|
||||||
|
}}
|
||||||
|
onClick={onClose}
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={src}
|
||||||
|
alt=""
|
||||||
|
style={{
|
||||||
|
maxWidth: '90vw', maxHeight: '90vh', objectFit: 'contain',
|
||||||
|
borderRadius: 8, boxShadow: '0 8px 32px rgba(0,0,0,0.5)',
|
||||||
|
cursor: 'default', userSelect: 'none',
|
||||||
|
}}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -39,8 +39,8 @@ export default function Navbar({ onOpenModal }) {
|
|||||||
const openMessages = (e) => {
|
const openMessages = (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setMenuOpen(false);
|
setMenuOpen(false);
|
||||||
// 打开私信列表(ChatManager 监听)
|
// 消息页 = 通知列表(与原版一致),私信走导航栏 ✉️ 按钮
|
||||||
window.dispatchEvent(new CustomEvent('forumlify-open-messages'));
|
navigate('messages');
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { API } from '@/lib/api';
|
|||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import ReplyList from './ReplyList';
|
import ReplyList from './ReplyList';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
|
import ImageViewer from './ImageViewer';
|
||||||
|
|
||||||
function avatar(username) {
|
function avatar(username) {
|
||||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') +
|
||||||
@@ -16,6 +17,10 @@ export default function PostDetail({ postId }) {
|
|||||||
const { currentUser, navigate, openUser, refreshKey } = useApp();
|
const { currentUser, navigate, openUser, refreshKey } = useApp();
|
||||||
const [post, setPost] = useState(null);
|
const [post, setPost] = useState(null);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
const [viewerSrc, setViewerSrc] = useState(null);
|
||||||
|
const [editing, setEditing] = useState(false);
|
||||||
|
const [editTitle, setEditTitle] = useState('');
|
||||||
|
const [editContent, setEditContent] = useState('');
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
const load = useCallback(async () => {
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -65,10 +70,6 @@ export default function PostDetail({ postId }) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const [editing, setEditing] = useState(false);
|
|
||||||
const [editTitle, setEditTitle] = useState('');
|
|
||||||
const [editContent, setEditContent] = useState('');
|
|
||||||
|
|
||||||
const handleEdit = () => {
|
const handleEdit = () => {
|
||||||
setEditTitle(post.title || '');
|
setEditTitle(post.title || '');
|
||||||
setEditContent(post.content || '');
|
setEditContent(post.content || '');
|
||||||
@@ -134,7 +135,9 @@ export default function PostDetail({ postId }) {
|
|||||||
</div>
|
</div>
|
||||||
{post.images && post.images.length > 0 && (
|
{post.images && post.images.length > 0 && (
|
||||||
<div className="post-images">
|
<div className="post-images">
|
||||||
{post.images.map((img, i) => <img key={i} src={img} className="post-image" alt="" />)}
|
{post.images.map((img, i) => (
|
||||||
|
<img key={i} src={img} className="post-image" alt="" style={{ cursor: 'pointer' }} onClick={() => setViewerSrc(img)} />
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@@ -165,6 +168,9 @@ export default function PostDetail({ postId }) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{/* 图片查看器 */}
|
||||||
|
{viewerSrc && <ImageViewer src={viewerSrc} onClose={() => setViewerSrc(null)} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,7 @@ import { API } from '@/lib/api';
|
|||||||
export default function AdminCustomCss() {
|
export default function AdminCustomCss() {
|
||||||
const [status, setStatus] = useState(null);
|
const [status, setStatus] = useState(null);
|
||||||
const [dragOver, setDragOver] = useState(false);
|
const [dragOver, setDragOver] = useState(false);
|
||||||
|
const [pendingFile, setPendingFile] = useState(null); // 待确认的 CSS 文件
|
||||||
const fileInputRef = useRef(null);
|
const fileInputRef = useRef(null);
|
||||||
|
|
||||||
const handleFile = async (file) => {
|
const handleFile = async (file) => {
|
||||||
@@ -19,14 +20,20 @@ export default function AdminCustomCss() {
|
|||||||
setStatus({ ok: false, msg: '❌ CSS 文件不能超过 1MB' });
|
setStatus({ ok: false, msg: '❌ CSS 文件不能超过 1MB' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 上传前警告确认(与原版一致)
|
||||||
|
setPendingFile(file);
|
||||||
|
};
|
||||||
|
|
||||||
|
const doUpload = async () => {
|
||||||
try {
|
try {
|
||||||
await API.uploadCustomCss(file);
|
await API.uploadCustomCss(pendingFile);
|
||||||
setStatus({ ok: true, msg: '✅ 自定义 CSS 已生效(刷新页面后应用)' });
|
setStatus({ ok: true, msg: '✅ 自定义 CSS 已生效(刷新页面后应用)' });
|
||||||
// 强制刷新样式
|
|
||||||
const link = document.getElementById('custom-css-link');
|
const link = document.getElementById('custom-css-link');
|
||||||
if (link) link.href = '/api/custom-css?t=' + Date.now();
|
if (link) link.href = '/api/custom-css?t=' + Date.now();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setStatus({ ok: false, msg: '❌ ' + err.message });
|
setStatus({ ok: false, msg: '❌ ' + err.message });
|
||||||
|
} finally {
|
||||||
|
setPendingFile(null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -77,6 +84,25 @@ export default function AdminCustomCss() {
|
|||||||
<button className="btn-primary" style={{ padding: '8px 20px' }} onClick={() => fileInputRef.current?.click()}>💾 保存 CSS</button>
|
<button className="btn-primary" style={{ padding: '8px 20px' }} onClick={() => fileInputRef.current?.click()}>💾 保存 CSS</button>
|
||||||
<button className="btn-secondary" style={{ padding: '8px 20px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={handleDelete}>🗑️ 删除自定义 CSS</button>
|
<button className="btn-secondary" style={{ padding: '8px 20px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={handleDelete}>🗑️ 删除自定义 CSS</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* 上传前警告确认 */}
|
||||||
|
{pendingFile && (
|
||||||
|
<div className="modal active" style={{ display: 'flex' }} onClick={() => setPendingFile(null)}>
|
||||||
|
<div className="modal-content" style={{ maxWidth: 420 }} onClick={(e) => e.stopPropagation()}>
|
||||||
|
<h2 style={{ marginBottom: 12 }}>⚠️ 警告</h2>
|
||||||
|
<p style={{ fontSize: 14, color: 'var(--text-secondary)', marginBottom: 16 }}>
|
||||||
|
若上传的 <strong>style.css</strong> 存在问题,将导致整个论坛界面样式错乱,甚至无法正常使用。
|
||||||
|
</p>
|
||||||
|
<p style={{ fontSize: 13, color: 'var(--text-light)', marginBottom: 20 }}>
|
||||||
|
请确保 CSS 文件是完整的、经过测试的版本。如出现问题,可点击「删除自定义 CSS」恢复默认样式。
|
||||||
|
</p>
|
||||||
|
<div style={{ display: 'flex', gap: 8 }}>
|
||||||
|
<button className="btn-primary" style={{ padding: '8px 24px' }} onClick={doUpload}>我了解,继续上传</button>
|
||||||
|
<button className="btn-secondary" style={{ padding: '8px 24px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={() => setPendingFile(null)}>取消</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user