Skip to content

Commit 83dae1f

Browse files
committed
chat scrolls top
1 parent 42ef82d commit 83dae1f

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

components/chatbot/ChatBot.tsx

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from "react";
1+
import { useEffect, useState, useRef } from "react";
22
import { Button } from "@/components/ui/button";
33
import { Card, CardHeader, CardContent, CardFooter } from "@/components/ui/card";
44
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
@@ -54,8 +54,6 @@ export default function Chatbot() {
5454
Here is the user prompt: ${userInput}.`),
5555
});
5656

57-
58-
5957
const data = await response.json();
6058

6159
let aiAnswer;
@@ -74,23 +72,22 @@ export default function Chatbot() {
7472
id: uuidv4().slice(0, 4),
7573
};
7674

77-
if(aiAnswer === undefined){
78-
assistantMessage.content = "I'm sorry. Please try again."
75+
if (aiAnswer === undefined) {
76+
assistantMessage.content = "I'm sorry. Please try again.";
7977
setMessages([...messages, userMessage, assistantMessage]);
8078
} else {
8179
setMessages([...messages, userMessage, assistantMessage]);
8280
}
8381

84-
8582
setIsLoading(false);
8683
}
8784

8885
useEffect(() => {
89-
console.log("aiChatBot",flags["ai-chatbot"])
86+
console.log("aiChatBot", flags["ai-chatbot"]);
9087
console.log(messages);
9188
}, [messages]);
9289

93-
const surveyResponseNotification = (surveyResponse:string) => {
90+
const surveyResponseNotification = (surveyResponse: string) => {
9491
client?.track(surveyResponse, client.getContext());
9592
client?.flush();
9693
toast({
@@ -99,6 +96,15 @@ export default function Chatbot() {
9996
});
10097
};
10198

99+
const chatContentRef = useRef(null);
100+
101+
useEffect(() => {
102+
if (chatContentRef.current) {
103+
console.log(chatContentRef)
104+
chatContentRef.current.scrollTop = chatContentRef.current.scrollHeight;
105+
}
106+
}, [messages]);
107+
102108
return (
103109
<>
104110
<div className="fixed bottom-4 right-4 z-50">
@@ -176,7 +182,7 @@ export default function Chatbot() {
176182
</Button>
177183
</div>
178184
</CardHeader>
179-
<CardContent className="h-[400px] overflow-y-auto">
185+
<CardContent className="h-[400px] overflow-y-auto" ref={chatContentRef}>
180186
<div className="space-y-4">
181187
<div className="flex w-max max-w-[75%] flex-col gap-2 rounded-lg px-3 py-2 text-sm bg-gray-100 dark:bg-gray-800">
182188
Hello! How can I assist you today?

0 commit comments

Comments
 (0)