fix: auto-update, auto-scroll to the bottom

This commit is contained in:
Bingxi Zhao (Frank)
2026-04-25 17:47:56 +08:00
parent c89a3df8a5
commit fba51dc856
3 changed files with 24 additions and 12 deletions

View File

@@ -1644,7 +1644,6 @@ function KnowledgePageContent() {
ref={createFileRef}
type="file"
multiple
accept={uploadPolicy.accept || undefined}
className="hidden"
onChange={(event) => {
setNewKbFiles((prev) =>
@@ -1819,7 +1818,6 @@ function KnowledgePageContent() {
ref={uploadFileRef}
type="file"
multiple
accept={uploadPolicy.accept || undefined}
className="hidden"
onChange={(event) => {
setUploadFiles((prev) =>

View File

@@ -37,14 +37,17 @@ export default function BotChatPage() {
const scrollRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLTextAreaElement>(null);
const scrollToBottom = useCallback(() => {
requestAnimationFrame(() => {
scrollRef.current?.scrollTo({
top: scrollRef.current.scrollHeight,
behavior: "smooth",
const scrollToBottom = useCallback(
(behavior: ScrollBehavior = "smooth") => {
requestAnimationFrame(() => {
scrollRef.current?.scrollTo({
top: scrollRef.current.scrollHeight,
behavior,
});
});
});
}, []);
},
[],
);
useEffect(() => {
if (!botId) {
@@ -64,10 +67,17 @@ export default function BotChatPage() {
role: m.role as "user" | "assistant",
content: m.content,
}));
if (restored.length) setMessages(restored);
if (restored.length) {
setMessages(restored);
// Markdown/KaTeX inside AssistantResponse can grow the container after
// the first paint — re-snap a few times so we land at the bottom.
requestAnimationFrame(() => scrollToBottom("instant"));
window.setTimeout(() => scrollToBottom("instant"), 80);
window.setTimeout(() => scrollToBottom("instant"), 250);
}
})
.catch(() => {});
}, [botId]);
}, [botId, scrollToBottom]);
useEffect(() => {
if (!botId) {

View File

@@ -90,8 +90,12 @@ export function useChatAutoScroll({
// After streaming ends, dynamically-loaded components (e.g. MathAnimatorViewer
// via next/dynamic) may render and grow the content height. Detect that and
// scroll to bottom so the user can see the full result.
// hasMessages is in deps so the observer attaches once the messages
// container mounts on session reopen — without it, the container ref is null
// on initial mount and the observer is never set up.
useEffect(() => {
if (isStreaming) return;
if (!hasMessages) return;
const container = containerRef.current;
if (!container) return;
@@ -118,7 +122,7 @@ export function useChatAutoScroll({
mo.disconnect();
if (rafId) cancelAnimationFrame(rafId);
};
}, [isStreaming, scrollToBottom]);
}, [hasMessages, isStreaming, scrollToBottom]);
const handleScroll = useCallback(() => {
const container = containerRef.current;