11'use client' ;
2- import React , { useState } from 'react' ;
2+ import React , { useState , useEffect } from 'react' ;
3+ import PulseLoader from './components/ui/pulseLoader' ;
34import api from './lib/api' ;
45import { useWallet } from '@suiet/wallet-kit' ;
56import JSONFormatter from './utils/JSONFormatter' ;
67
7- import { keywords } from './data' ;
8-
98import Messages from './components/sections/Messages' ;
109import SampleQuestions from './components/sections/SampleQuestions' ;
10+ import LoadingPage from './components/ui/loadingPage' ;
1111
1212export default function Home ( ) {
1313 const [ messages , setMessages ] = useState <
1414 { text : string ; sender : 'user' | 'llm' ; isHTML ?: boolean } [ ]
1515 > ( [ ] ) ;
1616 const [ inputValue , setInputValue ] = useState ( '' ) ;
1717 const [ isThinking , setIsThinking ] = useState ( false ) ;
18- const { address } = useWallet ( ) ;
18+ const { address, connected } = useWallet ( ) ;
19+ const [ isLoading , setIsLoading ] = useState ( false ) ;
20+
21+ // Load chat history when wallet connects
22+ useEffect ( ( ) => {
23+ if ( address && connected ) {
24+ loadChatHistory ( ) ;
25+ // // Send initial wallet connection message
26+ // handleSend(`Connected wallet: ${address}`);
27+ }
28+ } , [ address , connected ] ) ;
29+
30+ const loadChatHistory = async ( ) => {
31+ try {
32+ setIsLoading ( true ) ;
33+ const response = await api . get ( `/query/history/${ address } ` ) ;
34+ setMessages ( response . data ) ;
35+ } catch ( error ) {
36+ console . error ( 'Error loading chat history:' , error ) ;
37+ } finally {
38+ setIsLoading ( false ) ;
39+ }
40+ } ;
1941
2042 const handleSend = async ( message ?: string ) => {
2143 const userMessage = message || inputValue . trim ( ) ;
2244
2345 if ( userMessage ) {
2446 setMessages ( ( prev ) => [ ...prev , { text : userMessage , sender : 'user' } ] ) ;
25- setIsThinking ( true ) ;
2647 setInputValue ( '' ) ;
48+ setIsThinking ( true ) ;
2749
2850 try {
29- let modifiedMessage = userMessage ;
30-
31- const containsKeywords = keywords . some ( ( keyword ) =>
32- userMessage . toLowerCase ( ) . includes ( keyword )
33- ) ;
51+ // Always send the wallet address with the query
52+ const response = await api . post ( '/query' , {
53+ query : userMessage ,
54+ walletAddress : address
55+ } ) ;
3456
35- if ( containsKeywords ) {
36- modifiedMessage = `${ userMessage } . My wallet address is ${ address } .` ;
37- }
38-
39- console . log ( modifiedMessage , 'modified' ) ;
40- const response = await api . post ( '/query' , { query : modifiedMessage } ) ;
41- console . log ( response ) ;
4257 const res = response . data [ 0 ] ;
43- console . log ( res ) ;
4458 let llmResponse = '' ;
4559
4660 if ( typeof res . response === 'string' ) {
@@ -65,7 +79,7 @@ export default function Home() {
6579 }
6680 }
6781 } ;
68-
82+ if ( isLoading ) return < LoadingPage /> ;
6983 return (
7084 < div className = "h-[90dvh] w-[90dvw] flex justify-center relative items-center flex-col bg-gradient-to-b from-white to-gray-100" >
7185 { /* Chat messages */ }
@@ -81,7 +95,8 @@ export default function Home() {
8195
8296 { isThinking && (
8397 < div className = "relative mb-3 p-3 rounded-md w-fit max-w-[70%] bg-gray-300 text-black self-start mr-auto text-left" >
84- Please wait...
98+ { /* Please wait... */ }
99+ < PulseLoader />
85100 </ div >
86101 ) }
87102 </ div >
0 commit comments