From 4c0f146338dec191acebb6257a459cbe440e77f4 Mon Sep 17 00:00:00 2001
From: lezi-fun <177434121+lezi-fun@users.noreply.github.com>
Date: Tue, 11 Aug 2026 22:21:09 +0800
Subject: [PATCH] feat: reply chain view (sub-thread folding)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Build reply tree, show up to 3 children per reply. '继续此消息串' opens a dedicated chain view (only that reply + all descendants). '返回所有回复' goes back.
---
app/globals.css | 10 +++
components/Icons.js | 5 ++
components/PostDetail.js | 3 +-
components/ReplyList.js | 167 ++++++++++++++++++++++++---------------
4 files changed, 122 insertions(+), 63 deletions(-)
diff --git a/app/globals.css b/app/globals.css
index 13e0002..f646356 100644
--- a/app/globals.css
+++ b/app/globals.css
@@ -3166,3 +3166,13 @@ 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 ae34f83..442ff3f 100644
--- a/components/Icons.js
+++ b/components/Icons.js
@@ -27,6 +27,11 @@ const PATHS = {
>
),
+ arrowLeft: (
+ <>
+
+ >
+ ),
settings: (
<>
diff --git a/components/PostDetail.js b/components/PostDetail.js
index ac19db6..4266ba9 100644
--- a/components/PostDetail.js
+++ b/components/PostDetail.js
@@ -30,6 +30,7 @@ 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) {
// 延迟到帖子内容渲染后,让抽屉动画平滑播放
@@ -196,7 +197,7 @@ export default function PostDetail({ postId, initialPost = null }) {
{/* 评论抽屉:帖子加载完成后自动展开 */}
diff --git a/components/ReplyList.js b/components/ReplyList.js
index 6b5a704..fa8a549 100644
--- a/components/ReplyList.js
+++ b/components/ReplyList.js
@@ -1,6 +1,7 @@
'use client';
// 回复列表 + 发表回复表单
+// 支持树形子回复链折叠:每条回复的子回复 >3 条时折叠,点击"继续此消息串"进入独立链视图
import { useEffect, useState, useCallback, useRef } from 'react';
import { API } from '@/lib/api';
import { useApp } from './AppProvider';
@@ -14,19 +15,68 @@ function avatar(username) {
'&background=6366f1&color=fff&size=64';
}
-export default function ReplyList({ postId, onRefresh }) {
+// 渲染单条回复(树形,带子回复折叠)
+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 }) {
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(() => {
@@ -50,7 +100,6 @@ export default function ReplyList({ postId, onRefresh }) {
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('');
@@ -76,75 +125,69 @@ export default function ReplyList({ postId, onRefresh }) {
}
};
- // 计算每条回复的嵌套深度(按 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;
- })();
+ // 构建回复树(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))
+ );
- // 正在回复的目标回复对象(用于显示 @用户名)
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 ? (
还没有回复,快来发表第一条回复吧
- ) : (
- 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);
+ ) : chainParentId ? (
+ // 链视图:显示指定回复 + 所有子回复(递归展开)
+ (() => {
+ const chainRoot = replies.find((r) => r.id === chainParentId);
+ if (!chainRoot) return null;
return (
-
-
-

-
openUser(r.username)}
- >
- {r.username || '匿名用户'}
-
-
{rTime}
-
- {currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') && (
-
- )}
-
-
-
+
);
- })
- )}
- {hiddenCount > 0 && (
-
+ })()
+ ) : (
+ // 普通视图:显示根回复,每条回复的子回复最多 3 条
+ rootReplies.map((r) => (
+
+ ))
)}
@@ -185,4 +228,4 @@ export default function ReplyList({ postId, onRefresh }) {
>
);
-}
+}
\ No newline at end of file