|
1 | | -import React, { useRef } from "react"; |
2 | | -import autoAnimate from "@formkit/auto-animate"; |
3 | | -import { useEffect } from "react"; |
4 | | - |
| 1 | +import autoAnimate from '@formkit/auto-animate'; |
| 2 | +import React, { useEffect, useRef } from 'react'; |
5 | 3 |
|
6 | 4 | const ChatBody = ({ chat }) => { |
7 | | - const aiStyle = |
8 | | - "bg-white bg-opacity-40 backdrop-blur-lg dropshadow-md mr-auto"; |
9 | | - |
10 | | - const parent = useRef(null); |
11 | | - const bottomRef = useRef(null); |
12 | | - |
13 | | - // only for aut animations |
14 | | - useEffect(()=>{ |
15 | | - parent.current && autoAnimate(parent.current); |
16 | | - }, [parent]) |
17 | | - |
18 | | - //for scrolling bottom |
19 | | - useEffect(()=>{ |
20 | | - bottomRef.current?.scrollIntoView({behavior: "smooth"}) |
21 | | - }, [chat]) |
22 | | - |
23 | | - return ( |
24 | | - <div className="flex flex-col gap-4" ref={parent}> |
25 | | - {chat.map((message, i) => { |
26 | | - return ( |
27 | | - <div |
28 | | - key={i} |
29 | | - className={`border-[#999999] break-words border-2 rounded-xl self-end px-3 py-3 max-w-[80%] ${ |
30 | | - message.sender === "ai" && aiStyle |
31 | | - }`} |
32 | | - > |
33 | | - <pre className="whitespace-pre-wrap"> |
34 | | - <span>{message.message}</span> |
35 | | - </pre> |
36 | | - </div> |
37 | | - ); |
38 | | - })} |
39 | | - |
40 | | - <div ref={bottomRef} className="h-3"></div> |
41 | | - </div> |
42 | | - ); |
| 5 | + const aiStyle = 'bg-white bg-opacity-40 backdrop-blur-lg dropshadow-md mr-auto'; |
| 6 | + |
| 7 | + const parent = useRef(null); |
| 8 | + const bottomRef = useRef(null); |
| 9 | + |
| 10 | + // only for aut animations |
| 11 | + useEffect(() => { |
| 12 | + parent.current && autoAnimate(parent.current); |
| 13 | + }, [parent]); |
| 14 | + |
| 15 | + //for scrolling bottom |
| 16 | + useEffect(() => { |
| 17 | + bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); |
| 18 | + }, [chat]); |
| 19 | + |
| 20 | + return ( |
| 21 | + <div |
| 22 | + className="flex flex-col gap-4" |
| 23 | + ref={parent} |
| 24 | + > |
| 25 | + {chat.map((message, i) => { |
| 26 | + return ( |
| 27 | + <div |
| 28 | + key={i} |
| 29 | + className={`border-[#999999] break-words border-2 rounded-xl self-end px-3 py-3 max-w-[80%] ${message.sender === 'ai' && aiStyle}`} |
| 30 | + > |
| 31 | + <pre className="whitespace-pre-wrap"> |
| 32 | + <span>{message.message}</span> |
| 33 | + </pre> |
| 34 | + </div> |
| 35 | + ); |
| 36 | + })} |
| 37 | + |
| 38 | + <div |
| 39 | + ref={bottomRef} |
| 40 | + className="h-3" |
| 41 | + ></div> |
| 42 | + </div> |
| 43 | + ); |
43 | 44 | }; |
44 | 45 |
|
45 | 46 | export default ChatBody; |
0 commit comments