From 0aac277aea5cdb2c6a8ae40de090c58b09631e3d Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Tue, 11 Aug 2026 22:22:18 +0800 Subject: [PATCH] Revert "feat: reply chain view (sub-thread folding)" This reverts commit 4c0f146338dec191acebb6257a459cbe440e77f4. --- app/globals.css | 10 --- components/Icons.js | 5 -- components/PostDetail.js | 3 +- components/ReplyList.js | 169 +++++++++++++++------------------------ 4 files changed, 64 insertions(+), 123 deletions(-) diff --git a/app/globals.css b/app/globals.css index f646356..13e0002 100644 --- a/app/globals.css +++ b/app/globals.css @@ -3166,13 +3166,3 @@ a.btn-secondary { color: #fff; border-color: var(--primary); } - -/* ===== 回复链视图头部 ===== */ -.reply-chain-header { - display: flex; - align-items: center; - gap: 12px; - margin-bottom: 8px; - padding-bottom: 8px; - border-bottom: 1px solid var(--border); -} diff --git a/components/Icons.js b/components/Icons.js index 442ff3f..ae34f83 100644 --- a/components/Icons.js +++ b/components/Icons.js @@ -27,11 +27,6 @@ const PATHS = { ), - arrowLeft: ( - <> - - - ), settings: ( <> diff --git a/components/PostDetail.js b/components/PostDetail.js index 4266ba9..ac19db6 100644 --- a/components/PostDetail.js +++ b/components/PostDetail.js @@ -30,7 +30,6 @@ export default function PostDetail({ postId, initialPost = null }) { const editImageInputRef = useRef(null); // 评论抽屉:帖子加载完成后自动展开(动画完整播放) const [repliesOpen, setRepliesOpen] = useState(false); - const [chainParentId, setChainParentId] = useState(null); useEffect(() => { if (post && !repliesOpen) { // 延迟到帖子内容渲染后,让抽屉动画平滑播放 @@ -197,7 +196,7 @@ export default function PostDetail({ postId, initialPost = null }) { {/* 评论抽屉:帖子加载完成后自动展开 */}
- +
diff --git a/components/ReplyList.js b/components/ReplyList.js index fa8a549..6b5a704 100644 --- a/components/ReplyList.js +++ b/components/ReplyList.js @@ -1,7 +1,6 @@ 'use client'; // 回复列表 + 发表回复表单 -// 支持树形子回复链折叠:每条回复的子回复 >3 条时折叠,点击"继续此消息串"进入独立链视图 import { useEffect, useState, useCallback, useRef } from 'react'; import { API } from '@/lib/api'; import { useApp } from './AppProvider'; @@ -15,68 +14,19 @@ function avatar(username) { '&background=6366f1&color=fff&size=64'; } -// 渲染单条回复(树形,带子回复折叠) -function ReplyItem({ r, children, onStartReply, onDelete, currentUser, openUser, onShowChain, chainParentId }) { - const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : ''; - const childList = children[r.id] || []; - // 子回复折叠:最多显示 3 条,超出的折叠显示"继续此消息串" - const [showAll, setShowAll] = useState(false); - const visibleChildren = chainParentId || showAll ? childList : childList.slice(0, 3); - const hiddenCount = childList.length - visibleChildren.length; - - return ( -
-
- - openUser(r.username)}> - {r.username || '匿名用户'} - - {rTime} - - {currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') && ( - - )} -
-
- {/* 子回复列表 */} - {visibleChildren.map((child) => ( - - ))} - {/* 子回复折叠按钮 */} - {hiddenCount > 0 && !chainParentId && ( - - )} -
- ); -} - -export default function ReplyList({ postId, onRefresh, chainParentId, onBack }) { +export default function ReplyList({ postId, onRefresh }) { const { currentUser, openUser } = useApp(); const { toast, confirmAction } = useToast(); const [replies, setReplies] = useState([]); const [content, setContent] = useState(''); const [captcha, setCaptcha] = useState(null); const [captchaInput, setCaptchaInput] = useState(''); + // 正在回复的用户(hover 回复按钮 → 回填到回复框) const [replyTo, setReplyTo] = useState(null); const replyAreaRef = useRef(null); const replyInputRef = useRef(null); + // 点击"回复":记录目标回复 ID,滚动到回复框并聚焦 const startReply = (replyId) => { setReplyTo(replyId); setTimeout(() => { @@ -100,6 +50,7 @@ export default function ReplyList({ postId, onRefresh, chainParentId, onBack }) if (!content.trim()) { toast('请填写回复内容', 'warning'); return; } if (!captcha || !captchaInput.trim()) { toast('请填写验证码', 'warning'); return; } try { + // 答案由服务端 HMAC 校验 await API.createReply(postId, content.trim(), { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig }, replyTo); API.logEvent('create_reply').catch(() => {}); setContent(''); @@ -125,69 +76,75 @@ export default function ReplyList({ postId, onRefresh, chainParentId, onBack }) } }; - // 构建回复树(reply_id → children) - const children = {}; - replies.forEach((r) => { - const pid = r.reply_to_id || '__root__'; - if (!children[pid]) children[pid] = []; - children[pid].push(r); - }); - // 根回复(无 reply_to 或回复目标不在当前回复列表) - const rootReplies = (children['__root__'] || []).concat( - replies.filter((r) => r.reply_to_id && !replies.find((x) => x.id === r.reply_to_id)) - ); + // 计算每条回复的嵌套深度(按 reply_to_id 精确追踪回复链) + const replyDepths = (() => { + const map = {}; + const getDepth = (r) => { + if (map[r.id] !== undefined) return map[r.id]; + if (!r.reply_to_id) { map[r.id] = 0; return 0; } + const parent = replies.find((x) => x.id === r.reply_to_id); + const d = parent ? getDepth(parent) + 1 : 0; + map[r.id] = d; + return d; + }; + replies.forEach((r) => getDepth(r)); + return map; + })(); + // 正在回复的目标回复对象(用于显示 @用户名) const replyToObj = replyTo ? replies.find((x) => x.id === replyTo) : null; + // 回复折叠:根据视口高度动态调整可见条数(至少 3 条) + const [showCount, setShowCount] = useState(3); + const [showAllReplies, setShowAllReplies] = useState(false); + useEffect(() => { + setShowCount(Math.max(3, Math.floor(window.innerHeight / 200))); + }, []); + const visibleReplies = showAllReplies ? replies : replies.slice(-showCount); + const hiddenCount = replies.length - visibleReplies.length; + return ( <> - {chainParentId && onBack && ( -
- - 回复链 -
- )} -
+
{replies.length} 条回复
{replies.length === 0 ? (
还没有回复,快来发表第一条回复吧
- ) : chainParentId ? ( - // 链视图:显示指定回复 + 所有子回复(递归展开) - (() => { - const chainRoot = replies.find((r) => r.id === chainParentId); - if (!chainRoot) return null; - return ( - - ); - })() ) : ( - // 普通视图:显示根回复,每条回复的子回复最多 3 条 - rootReplies.map((r) => ( - - )) + visibleReplies.map((r) => { + const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : ''; + const nestDepth = Math.min(replyDepths[r.id] || 0, 5); + return ( +
+
+ + openUser(r.username)} + > + {r.username || '匿名用户'} + + {rTime} + + {currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') && ( + + )} +
+
+
+ ); + }) + )} + {hiddenCount > 0 && ( + )}
@@ -228,4 +185,4 @@ export default function ReplyList({ postId, onRefresh, chainParentId, onBack })
); -} \ No newline at end of file +}