feat: auto scroll to bottom when MCP response

This commit is contained in:
Kadxy 2025-01-09 20:02:27 +08:00
parent 7d51bfd42e
commit b410ec399c

View File

@ -421,12 +421,11 @@ export function ChatAction(props: {
function useScrollToBottom( function useScrollToBottom(
scrollRef: RefObject<HTMLDivElement>, scrollRef: RefObject<HTMLDivElement>,
detach: boolean = false, detach: boolean = false,
messages: ChatMessage[],
) { ) {
// for auto-scroll // for auto-scroll
const [autoScroll, setAutoScroll] = useState(true); const [autoScroll, setAutoScroll] = useState(true);
const scrollDomToBottom = useCallback(() => {
function scrollDomToBottom() {
const dom = scrollRef.current; const dom = scrollRef.current;
if (dom) { if (dom) {
requestAnimationFrame(() => { requestAnimationFrame(() => {
@ -434,7 +433,7 @@ function useScrollToBottom(
dom.scrollTo(0, dom.scrollHeight); dom.scrollTo(0, dom.scrollHeight);
}); });
} }
} }, [scrollRef]);
// auto scroll // auto scroll
useEffect(() => { useEffect(() => {
@ -443,6 +442,15 @@ function useScrollToBottom(
} }
}); });
// auto scroll when messages length changes
const lastMessagesLength = useRef(messages.length);
useEffect(() => {
if (messages.length > lastMessagesLength.current && !detach) {
scrollDomToBottom();
}
lastMessagesLength.current = messages.length;
}, [messages.length, detach, scrollDomToBottom]);
return { return {
scrollRef, scrollRef,
autoScroll, autoScroll,
@ -978,6 +986,7 @@ function _Chat() {
const { setAutoScroll, scrollDomToBottom } = useScrollToBottom( const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
scrollRef, scrollRef,
(isScrolledToBottom || isAttachWithTop) && !isTyping, (isScrolledToBottom || isAttachWithTop) && !isTyping,
session.messages,
); );
const [hitBottom, setHitBottom] = useState(true); const [hitBottom, setHitBottom] = useState(true);
const isMobileScreen = useMobileScreen(); const isMobileScreen = useMobileScreen();