diff --git a/app/globals.css b/app/globals.css index 785ac0e..35166a3 100644 --- a/app/globals.css +++ b/app/globals.css @@ -1513,6 +1513,43 @@ html.home-view-transition.returning-home::view-transition-old(post-edited-detail animation: editedDateCollapse 0.24s 0.04s cubic-bezier(0.4, 0, 1, 1) both; } +/* 回复仍在加载时返回:从页面中独立抽离,向上淡出后再消失。 */ +html.home-view-transition.returning-home #pagePost .reply-loading-state { + view-transition-name: reply-loading-return; +} + +html.home-view-transition.returning-home::view-transition-group(reply-loading-return) { + animation: none; + z-index: 14; +} + +html.home-view-transition.returning-home::view-transition-image-pair(reply-loading-return) { + overflow: visible; + mix-blend-mode: normal; +} + +html.home-view-transition.returning-home::view-transition-old(reply-loading-return) { + animation: replyLoadingReturnOut 0.24s cubic-bezier(0.4, 0, 1, 1) both; + mix-blend-mode: normal; + -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 40%, transparent 60%, transparent 100%); + mask-image: linear-gradient(to bottom, #000 0%, #000 40%, transparent 60%, transparent 100%); + -webkit-mask-size: 100% 250%; + mask-size: 100% 250%; + -webkit-mask-repeat: no-repeat; + mask-repeat: no-repeat; +} + +@keyframes replyLoadingReturnOut { + from { + -webkit-mask-position: 0 0; + mask-position: 0 0; + } + to { + -webkit-mask-position: 0 100%; + mask-position: 0 100%; + } +} + @keyframes editedDateExpand { from { opacity: 0; @@ -3390,6 +3427,45 @@ a.btn-secondary { to { transform: rotate(360deg); } } +/* 回复慢请求:帖子展开完成后才柔和出现,快速请求不会挂载此状态。 */ +.reply-loading-state { + min-height: 76px; + margin-top: 16px; + display: flex; + align-items: center; + justify-content: center; + gap: 10px; + color: var(--glass-text-light); + font-size: 13px; + animation: replyLoadingReveal 0.32s ease-out both; +} + +.reply-loading-spinner { + width: 16px; + height: 16px; + border: 2px solid var(--glass-border); + border-top-color: var(--primary); + border-radius: 50%; + animation: spin 0.8s linear infinite; +} + +html.post-view-transition #pagePost .reply-loading-state { + opacity: 0; + transform: translateY(5px); + animation: none; +} + +@keyframes replyLoadingReveal { + from { + opacity: 0; + transform: translateY(5px); + } + to { + opacity: 0.78; + transform: translateY(0); + } +} + /* ===== 评论抽屉(平滑展开/收起) ===== */ .replies-drawer { display: grid; diff --git a/components/ReplyList.js b/components/ReplyList.js index 5a7c3be..ff4ee24 100644 --- a/components/ReplyList.js +++ b/components/ReplyList.js @@ -8,6 +8,7 @@ import { Icon } from './Icons'; import { renderMarkdown } from '@/lib/markdown'; import CaptchaImage from './CaptchaImage'; import { useToast } from './Toast'; +import { useTranslation } from 'react-i18next'; function avatar(username) { return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || '匿名用户') + @@ -17,7 +18,10 @@ function avatar(username) { export default function ReplyList({ postId, onRefresh }) { const { currentUser, openUser } = useApp(); const { toast, confirmAction } = useToast(); - const [replies, setReplies] = useState([]); + const { t } = useTranslation(); + const [replies, setReplies] = useState(null); + const [loading, setLoading] = useState(true); + const [showLoading, setShowLoading] = useState(false); const [content, setContent] = useState(''); const [captcha, setCaptcha] = useState(null); const [captchaInput, setCaptchaInput] = useState(''); @@ -25,6 +29,7 @@ export default function ReplyList({ postId, onRefresh }) { const [replyTo, setReplyTo] = useState(null); const replyAreaRef = useRef(null); const replyInputRef = useRef(null); + const loadRequestRef = useRef(0); // 点击"回复":记录目标回复 ID,滚动到回复框并聚焦 const startReply = (replyId) => { @@ -35,14 +40,37 @@ export default function ReplyList({ postId, onRefresh }) { }, 120); }; - const load = useCallback(async () => { + const load = useCallback(async ({ reset = false } = {}) => { + const requestId = ++loadRequestRef.current; + if (reset) setReplies(null); + setLoading(true); try { const data = await API.getReplies(postId); + if (requestId !== loadRequestRef.current) return; setReplies(data || []); - } catch { setReplies([]); } + } catch { + if (requestId !== loadRequestRef.current) return; + setReplies([]); + } finally { + if (requestId === loadRequestRef.current) setLoading(false); + } }, [postId]); - useEffect(() => { load(); }, [load]); + useEffect(() => { + load({ reset: true }); + return () => { loadRequestRef.current += 1; }; + }, [load]); + + // 快请求不闪加载态;慢请求的加载动画由 CSS 等帖子 View Transition 结束后再淡入。 + useEffect(() => { + if (!loading) { + setShowLoading(false); + return; + } + const timer = setTimeout(() => setShowLoading(true), 180); + return () => clearTimeout(timer); + }, [loading]); + useEffect(() => { API.getCaptcha().then((c) => setCaptcha(c)).catch(() => {}); }, [postId]); const handleSubmit = async () => { @@ -82,33 +110,43 @@ export default function ReplyList({ postId, onRefresh }) { }; // 计算每条回复的嵌套深度(按 reply_to_id 精确追踪回复链) + const loadedReplies = replies || []; 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 parent = loadedReplies.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)); + loadedReplies.forEach((r) => getDepth(r)); return map; })(); // 正在回复的目标回复对象(用于显示 @用户名) - const replyToObj = replyTo ? replies.find((x) => x.id === replyTo) : null; + const replyToObj = replyTo ? loadedReplies.find((x) => x.id === replyTo) : null; return ( <> -