Skip to content

Commit 2365161

Browse files
committed
fix scroll to bottom
1 parent ffe148b commit 2365161

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

client/src/components/chat.tsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ChatInput } from "@/components/ui/chat/chat-input";
88
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
99
import { useTransition, animated, type AnimatedProps } from "@react-spring/web";
1010
import { Paperclip, Send, X } from "lucide-react";
11-
import { useEffect, useRef, useState, useCallback } from "react";
11+
import { useEffect, useRef, useState } from "react";
1212
import type { Content, UUID } from "@elizaos/core";
1313
import { useMutation, useQueryClient } from "@tanstack/react-query";
1414
import { apiClient } from "@/lib/api";
@@ -22,6 +22,7 @@ import AIWriter from "react-aiwriter";
2222
import type { IAttachment } from "@/types";
2323
import { AudioRecorder } from "./audio-recorder";
2424
import { Badge } from "./ui/badge";
25+
import { useAutoScroll } from "./ui/chat/hooks/useAutoScroll";
2526

2627
type ExtraContentFields = {
2728
user: string;
@@ -39,7 +40,6 @@ export default function Page({ agentId }: { agentId: UUID }) {
3940
const { toast } = useToast();
4041
const [selectedFile, setSelectedFile] = useState<File | null>(null);
4142
const [input, setInput] = useState("");
42-
const messagesContainerRef = useRef<HTMLDivElement>(null);
4343
const inputRef = useRef<HTMLTextAreaElement>(null);
4444
const fileInputRef = useRef<HTMLInputElement>(null);
4545
const formRef = useRef<HTMLFormElement>(null);
@@ -49,13 +49,10 @@ export default function Page({ agentId }: { agentId: UUID }) {
4949
const getMessageVariant = (role: string) =>
5050
role !== "user" ? "received" : "sent";
5151

52-
const scrollToBottom = useCallback(() => {
53-
if (messagesContainerRef.current) {
54-
messagesContainerRef.current.scrollTop =
55-
messagesContainerRef.current.scrollHeight;
56-
}
57-
}, []);
58-
52+
const { scrollRef, isAtBottom, scrollToBottom, disableAutoScroll } = useAutoScroll({
53+
smooth: true,
54+
});
55+
5956
useEffect(() => {
6057
scrollToBottom();
6158
}, [queryClient.getQueryData(["messages", agentId])]);
@@ -176,7 +173,12 @@ export default function Page({ agentId }: { agentId: UUID }) {
176173
return (
177174
<div className="flex flex-col w-full h-[calc(100dvh)] p-4">
178175
<div className="flex-1 overflow-y-auto">
179-
<ChatMessageList ref={messagesContainerRef}>
176+
<ChatMessageList
177+
scrollRef={scrollRef}
178+
isAtBottom={isAtBottom}
179+
scrollToBottom={scrollToBottom}
180+
disableAutoScroll={disableAutoScroll}
181+
>
180182
{transitions((style, message: ContentWithUser) => {
181183
const variant = getMessageVariant(message?.user);
182184
return (

client/src/components/ui/chat/chat-message-list.tsx

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import * as React from "react";
22
import { ArrowDown } from "lucide-react";
33
import { Button } from "@/components/ui/button";
4-
import { useAutoScroll } from "@/components/ui/chat/hooks/useAutoScroll";
54

65
interface ChatMessageListProps extends React.HTMLAttributes<HTMLDivElement> {
6+
scrollRef: React.RefObject<HTMLDivElement | null>;
7+
isAtBottom: boolean;
8+
scrollToBottom: () => void;
9+
disableAutoScroll: () => void;
710
smooth?: boolean;
811
}
912

1013
const ChatMessageList = React.forwardRef<HTMLDivElement, ChatMessageListProps>(
11-
({ className, children, smooth = false, ...props }, _ref) => {
12-
const { scrollRef, isAtBottom, scrollToBottom, disableAutoScroll } =
13-
useAutoScroll({
14-
smooth,
15-
content: children,
16-
});
17-
14+
({ className, children, scrollRef, isAtBottom, scrollToBottom, disableAutoScroll, ...props }) => {
1815
return (
1916
<div className="relative w-full h-full">
2017
<div

0 commit comments

Comments
 (0)