1
1
"use client" ;
2
2
3
3
import { PlaceholdersAndVanishInput } from "./ui/placeholders-and-vanish-input" ;
4
+ import { useState , useEffect } from "react" ;
5
+ import axios from "axios" ;
4
6
5
7
export function PlaceholdersAndVanishInputDemo ( ) {
8
+ const [ messages , setMessages ] = useState < Array < { query : string ; response : string } > > ( [ ] ) ;
9
+ const [ isLoading , setIsLoading ] = useState ( false ) ;
10
+
11
+ const backendUrl = import . meta. env . VITE_BACKEND_URL || 'http://localhost:5000' ;
12
+
13
+
6
14
const placeholders = [
7
15
"What's the first rule of Fight Club?" ,
8
16
"Who is Tyler Durden?" ,
@@ -12,22 +20,58 @@ export function PlaceholdersAndVanishInputDemo() {
12
20
] ;
13
21
14
22
const handleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
15
- console . log ( e . target . value ) ;
23
+ // You can add any additional logic here if needed
16
24
} ;
17
- const onSubmit = ( e : React . FormEvent < HTMLFormElement > ) => {
25
+
26
+ const onSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
18
27
e . preventDefault ( ) ;
19
- console . log ( "submitted" ) ;
28
+ const query = e . currentTarget . querySelector ( 'input' ) ?. value ;
29
+ if ( ! query ) return ;
30
+
31
+ setIsLoading ( true ) ;
32
+ try {
33
+ const result = await axios . post ( `${ backendUrl } /chat` , { query } ) ;
34
+ setMessages ( prevMessages => [ ...prevMessages , { query, response : result . data . result } ] ) ;
35
+ } catch ( error ) {
36
+ console . error ( 'Error querying the model:' , error ) ;
37
+ setMessages ( prevMessages => [ ...prevMessages , { query, response : "An error occurred while processing your request." } ] ) ;
38
+ } finally {
39
+ setIsLoading ( false ) ;
40
+ }
20
41
} ;
42
+
21
43
return (
22
- < div className = "h-[40rem ] flex flex-col justify-center items-center px-4 " >
23
- < h2 className = "mb-10 sm:mb-20 text-xl text-center sm:text-5xl dark:text-white text-black" >
24
- Start Collabrating
44
+ < div className = "h-[50rem ] flex flex-col" >
45
+ < h2 className = "mb-4 text-xl text-center sm:text-5xl dark:text-white text-black" >
46
+ Start Collaborating
25
47
</ h2 >
26
- < PlaceholdersAndVanishInput
27
- placeholders = { placeholders }
28
- onChange = { handleChange }
29
- onSubmit = { onSubmit }
30
- />
48
+ < div className = "flex-grow overflow-y-auto px-4 flex flex-col items-center" >
49
+ { messages . map ( ( message , index ) => (
50
+ < div key = { index } className = "mb-4 w-full max-w-[80%]" >
51
+ < div className = "bg-zinc-100 dark:bg-zinc-800 p-2 rounded-lg mb-2" >
52
+ < p className = "font-bold" > You:</ p >
53
+ < p > { message . query } </ p >
54
+ </ div >
55
+ < div className = "bg-zinc-200 dark:bg-zinc-700 p-2 rounded-lg" >
56
+ < p className = "font-bold" > AI:</ p >
57
+ < p > { message . response } </ p >
58
+ </ div >
59
+ </ div >
60
+ ) ) }
61
+ </ div >
62
+ { isLoading && (
63
+ < div className = "flex justify-center items-center p-4" >
64
+ < p > Loading...</ p >
65
+ </ div >
66
+ ) }
67
+ < div className = "p-4 mt-4" >
68
+ < PlaceholdersAndVanishInput
69
+ placeholders = { placeholders }
70
+
71
+ onChange = { handleChange }
72
+ onSubmit = { onSubmit }
73
+ />
74
+ </ div >
31
75
</ div >
32
76
) ;
33
77
}
0 commit comments