fix: 滚动条不在底部时发送消息看不到 AI 输出

use-stick-to-bottom 库仅在 isAtBottom=true 时跟随新内容自动滚动。
用户滚动上去后发送消息,流式内容渲染在视口下方不可见。

新增 ScrollOnStream 组件,监听 isStreaming 从 false→true 的变化,
强制调用 scrollToBottom(),确保流式输出开始时自动跳到底部。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
7418andClaude Opus 4.6 committed 2026-03-01 22:49:35 +08:00
1 parent 6209c865fd
commit 7e45448860
1 file changed
+20
+20
View File
@@ -2,6 +2,7 @@
import { useRef, useEffect } from 'react';
import { useTranslation } from '@/hooks/useTranslation';
import { useStickToBottomContext } from 'use-stick-to-bottom';
import type { Message, PermissionRequestEvent } from '@/types';
import {
Conversation,
@@ -13,6 +14,24 @@ import { MessageItem } from './MessageItem';
import { StreamingMessage } from './StreamingMessage';
import { CodePilotLogo } from './CodePilotLogo';
/**
* Scrolls to bottom when streaming starts.
* Must be rendered inside <Conversation> (StickToBottom provider).
*/
function ScrollOnStream({ isStreaming }: { isStreaming: boolean }) {
const { scrollToBottom } = useStickToBottomContext();
const wasStreaming = useRef(false);
useEffect(() => {
if (isStreaming && !wasStreaming.current) {
scrollToBottom();
}
wasStreaming.current = isStreaming;
}, [isStreaming, scrollToBottom]);
return null;
}
interface ToolUseInfo {
id: string;
name: string;
@@ -97,6 +116,7 @@ export function MessageList({
return (
<Conversation>
<ScrollOnStream isStreaming={isStreaming} />
<ConversationContent className="mx-auto max-w-3xl px-4 py-6 gap-6">
{hasMore && (
<div className="flex justify-center">