1
- import { useEffect , useState } from "react" ;
1
+ import { useEffect , useState , useRef } from "react" ;
2
2
import { Button } from "@/components/ui/button" ;
3
3
import { Card , CardHeader , CardContent , CardFooter } from "@/components/ui/card" ;
4
4
import { Avatar , AvatarImage , AvatarFallback } from "@/components/ui/avatar" ;
@@ -54,8 +54,6 @@ export default function Chatbot() {
54
54
Here is the user prompt: ${ userInput } .` ) ,
55
55
} ) ;
56
56
57
-
58
-
59
57
const data = await response . json ( ) ;
60
58
61
59
let aiAnswer ;
@@ -74,23 +72,22 @@ export default function Chatbot() {
74
72
id : uuidv4 ( ) . slice ( 0 , 4 ) ,
75
73
} ;
76
74
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." ;
79
77
setMessages ( [ ...messages , userMessage , assistantMessage ] ) ;
80
78
} else {
81
79
setMessages ( [ ...messages , userMessage , assistantMessage ] ) ;
82
80
}
83
81
84
-
85
82
setIsLoading ( false ) ;
86
83
}
87
84
88
85
useEffect ( ( ) => {
89
- console . log ( "aiChatBot" , flags [ "ai-chatbot" ] )
86
+ console . log ( "aiChatBot" , flags [ "ai-chatbot" ] ) ;
90
87
console . log ( messages ) ;
91
88
} , [ messages ] ) ;
92
89
93
- const surveyResponseNotification = ( surveyResponse :string ) => {
90
+ const surveyResponseNotification = ( surveyResponse : string ) => {
94
91
client ?. track ( surveyResponse , client . getContext ( ) ) ;
95
92
client ?. flush ( ) ;
96
93
toast ( {
@@ -99,6 +96,15 @@ export default function Chatbot() {
99
96
} ) ;
100
97
} ;
101
98
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
+
102
108
return (
103
109
< >
104
110
< div className = "fixed bottom-4 right-4 z-50" >
@@ -176,7 +182,7 @@ export default function Chatbot() {
176
182
</ Button >
177
183
</ div >
178
184
</ CardHeader >
179
- < CardContent className = "h-[400px] overflow-y-auto" >
185
+ < CardContent className = "h-[400px] overflow-y-auto" ref = { chatContentRef } >
180
186
< div className = "space-y-4" >
181
187
< 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" >
182
188
Hello! How can I assist you today?
0 commit comments