@@ -5,10 +5,11 @@ import ChatHeader from "./components/ChatHeader";
55import ChatBox from "./components/ChatBox" ;
66import ChatInput from "./components/ChatInput" ;
77import "./index.css" ;
8+ import { API_URL } from "./config" ;
89
910export default function App ( ) {
1011 const savedTheme = localStorage . getItem ( "theme" ) ;
11- const [ theme , setTheme ] = useState ( savedTheme ) ;
12+ const [ theme , setTheme ] = useState ( savedTheme || "dark" ) ;
1213 const [ sidebarOpen , setSidebarOpen ] = useState ( true ) ;
1314 const [ messages , setMessages ] = useState ( [
1415 {
@@ -27,17 +28,62 @@ export default function App() {
2728 localStorage . setItem ( "theme" , theme ) ;
2829 } , [ theme ] ) ;
2930
30- const handleSend = ( inputText ) => {
31- const newMessages = [
32- ...messages ,
33- { type : "user" , text : inputText , icon : "🐻" } ,
34- {
35- type : "bot" ,
36- text : `Sorry, I don't understand yet. Please try again later.` ,
37- icon : "🐶" ,
38- } ,
39- ] ;
40- setMessages ( newMessages ) ;
31+ // const handleSend = (inputText) => {
32+ // const newMessages = [
33+ // ...messages,
34+ // { type: "user", text: inputText, icon: "🐻" },
35+ // {
36+ // type: "bot",
37+ // text: `Sorry, I don't understand yet. Please try again later.`,
38+ // icon: "🐶",
39+ // },
40+ // ];
41+ // setMessages(newMessages);
42+ // };
43+
44+ const handleSend = async ( inputText ) => {
45+ const userMessage = { type : "user" , text : inputText , icon : "🐻" } ;
46+ setMessages ( ( prev ) => [ ...prev , userMessage ] ) ;
47+
48+ // Add loading state
49+ const loadingId = setTimeout ( ( ) => {
50+ setMessages ( ( prev ) => [
51+ ...prev ,
52+ { type : "bot" , text : "Thinking..." , icon : "🐶" , isLoading : true }
53+ ] ) ;
54+ } , 1000 ) ;
55+
56+ try {
57+ const res = await fetch ( `${ API_URL } /chat` , { // Change this to your deployed API
58+ method : "POST" ,
59+ headers : { "Content-Type" : "application/json" } ,
60+ body : JSON . stringify ( { question : inputText } ) ,
61+ } ) ;
62+
63+ clearTimeout ( loadingId ) ;
64+
65+ // Remove loading message if it was added
66+ setMessages ( ( prev ) => prev . filter ( msg => ! msg . isLoading ) ) ;
67+
68+ if ( ! res . ok ) {
69+ throw new Error ( `Server responded with status: ${ res . status } ` ) ;
70+ }
71+
72+ const data = await res . json ( ) ;
73+ const botReply = { type : "bot" , text : data . response || "Sorry, I couldn't generate a response." , icon : "🐶" } ;
74+ setMessages ( ( prev ) => [ ...prev , botReply ] ) ;
75+ } catch ( error ) {
76+ clearTimeout ( loadingId ) ;
77+
78+ // Remove loading message if it was added
79+ setMessages ( ( prev ) => prev . filter ( msg => ! msg . isLoading ) ) ;
80+
81+ console . error ( "Chat error:" , error ) ;
82+ setMessages ( ( prev ) => [
83+ ...prev ,
84+ { type : "bot" , text : "An error occurred. Please try again later." , icon : "🐶" } ,
85+ ] ) ;
86+ }
4187 } ;
4288
4389 return (
0 commit comments