From 670be3cbbbf47202389bdcf448826094dd404f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=97=E6=9E=B3?= <7854742+wang_rumeng@user.noreply.gitee.com> Date: Sun, 24 Aug 2025 15:49:39 +0800 Subject: [PATCH] =?UTF-8?q?chatbox=20=E8=B0=83=E6=95=B4=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E5=88=B0=E5=BA=95=E9=83=A8=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/SmartChatBox/SmartChatBox.tsx | 35 ++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/components/SmartChatBox/SmartChatBox.tsx b/components/SmartChatBox/SmartChatBox.tsx index cb0cfd4..e486930 100644 --- a/components/SmartChatBox/SmartChatBox.tsx +++ b/components/SmartChatBox/SmartChatBox.tsx @@ -1,4 +1,4 @@ -import React, { useRef, useCallback } from "react"; +import React, { useRef, useCallback, useState, useEffect } from "react"; import { ArrowRightFromLine, ChevronDown } from 'lucide-react'; import { Switch } from 'antd'; import { MessageRenderer } from "./MessageRenderer"; @@ -35,13 +35,42 @@ function BackToLatestButton({ onClick }: { onClick: () => void }) { export default function SmartChatBox({ isSmartChatBoxOpen, setIsSmartChatBoxOpen, projectId, userId }: SmartChatBoxProps) { // 消息列表引用 const listRef = useRef(null); + const [isAtBottom, setIsAtBottom] = useState(true); + + // 检查是否滚动到底部 + const checkIfAtBottom = useCallback(() => { + if (listRef.current) { + const { scrollHeight, scrollTop, clientHeight } = listRef.current; + // 考虑一个小的误差范围(10px) + const isBottom = scrollHeight - scrollTop - clientHeight <= 10; + setIsAtBottom(isBottom); + } + }, []); + + // 处理滚动事件 + const handleScroll = useCallback(() => { + checkIfAtBottom(); + }, [checkIfAtBottom]); + + // 监听滚动事件 + useEffect(() => { + const listElement = listRef.current; + if (listElement) { + listElement.addEventListener('scroll', handleScroll); + return () => { + listElement.removeEventListener('scroll', handleScroll); + }; + } + }, [handleScroll]); // 处理消息更新时的滚动 const handleMessagesUpdate = useCallback((shouldScroll: boolean) => { if (shouldScroll && listRef.current) { listRef.current.scrollTo({ top: listRef.current.scrollHeight, behavior: "smooth" }); } - }, []); + // 更新底部状态 + checkIfAtBottom(); + }, [checkIfAtBottom]); // 使用消息管理 hook const [ @@ -135,7 +164,7 @@ export default function SmartChatBox({ isSmartChatBoxOpen, setIsSmartChatBoxOpen )} {/* Back to latest button */} - {isViewingHistory && ( + {isViewingHistory && !isAtBottom && ( )}