Skip to content

Commit 48dca07

Browse files
authored
Merge pull request #1538 from swizzmagik/feat/add-autoscroll-chat-client
2 parents 00b6095 + 3561ed8 commit 48dca07

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1224,4 +1224,4 @@
12241224

12251225

12261226

1227-
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
1227+
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*

client/src/Chat.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { useRef, useState } from "react";
2-
import { useParams } from "react-router-dom";
3-
import { useMutation } from "@tanstack/react-query";
41
import { Button } from "@/components/ui/button";
5-
import { ImageIcon } from "lucide-react";
62
import { Input } from "@/components/ui/input";
3+
import { useMutation } from "@tanstack/react-query";
4+
import { ImageIcon } from "lucide-react";
5+
import { useEffect, useRef, useState } from "react";
6+
import { useParams } from "react-router-dom";
77
import "./App.css";
8-
import path from "path";
98

109
type TextResponse = {
1110
text: string;
@@ -19,6 +18,15 @@ export default function Chat() {
1918
const [messages, setMessages] = useState<TextResponse[]>([]);
2019
const [selectedFile, setSelectedFile] = useState<File | null>(null);
2120
const fileInputRef = useRef<HTMLInputElement>(null);
21+
const messagesEndRef = useRef<HTMLDivElement>(null);
22+
23+
const scrollToBottom = () => {
24+
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
25+
};
26+
27+
useEffect(() => {
28+
scrollToBottom();
29+
}, [messages]);
2230

2331
const mutation = useMutation({
2432
mutationFn: async (text: string) => {
@@ -115,6 +123,7 @@ export default function Chat() {
115123
No messages yet. Start a conversation!
116124
</div>
117125
)}
126+
<div ref={messagesEndRef} />
118127
</div>
119128
</div>
120129

0 commit comments

Comments
 (0)