From 555995fbe24eb0d5e1aa05c376982b87ada03b81 Mon Sep 17 00:00:00 2001 From: Deepraj Date: Sun, 22 Sep 2024 03:44:35 +0530 Subject: [PATCH] feat: added chat feature in UI --- client/src/App.tsx | 2 +- client/src/components/chat.tsx | 66 ++++++++++++++++++++++++++++------ client/src/pages/Login.tsx | 4 +-- 3 files changed, 58 insertions(+), 14 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 226da09..5683f61 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -52,7 +52,7 @@ const App: React.FC = () => { : } /> } /> - { setAuthenticated(true); setUsername(username); }} />} /> + { setAuthenticated(true); setUsername(username); }} />} /> : } /> } /> } /> diff --git a/client/src/components/chat.tsx b/client/src/components/chat.tsx index a2802e7..f816048 100644 --- a/client/src/components/chat.tsx +++ b/client/src/components/chat.tsx @@ -1,8 +1,16 @@ "use client"; import { PlaceholdersAndVanishInput } from "./ui/placeholders-and-vanish-input"; +import { useState, useEffect } from "react"; +import axios from "axios"; export function PlaceholdersAndVanishInputDemo() { + const [messages, setMessages] = useState>([]); + const [isLoading, setIsLoading] = useState(false); + + const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:5000'; + + const placeholders = [ "What's the first rule of Fight Club?", "Who is Tyler Durden?", @@ -12,22 +20,58 @@ export function PlaceholdersAndVanishInputDemo() { ]; const handleChange = (e: React.ChangeEvent) => { - console.log(e.target.value); + // You can add any additional logic here if needed }; - const onSubmit = (e: React.FormEvent) => { + + const onSubmit = async (e: React.FormEvent) => { e.preventDefault(); - console.log("submitted"); + const query = e.currentTarget.querySelector('input')?.value; + if (!query) return; + + setIsLoading(true); + try { + const result = await axios.post(`${backendUrl}/chat`, { query }); + setMessages(prevMessages => [...prevMessages, { query, response: result.data.result }]); + } catch (error) { + console.error('Error querying the model:', error); + setMessages(prevMessages => [...prevMessages, { query, response: "An error occurred while processing your request." }]); + } finally { + setIsLoading(false); + } }; + return ( -
-

- Start Collabrating +
+

+ Start Collaborating

- +
+ {messages.map((message, index) => ( +
+
+

You:

+

{message.query}

+
+
+

AI:

+

{message.response}

+
+
+ ))} +
+ {isLoading && ( +
+

Loading...

+
+ )} +
+ +
); } diff --git a/client/src/pages/Login.tsx b/client/src/pages/Login.tsx index c6b06dc..1d93df3 100644 --- a/client/src/pages/Login.tsx +++ b/client/src/pages/Login.tsx @@ -5,7 +5,7 @@ import { buttonVariants } from "@/components/ui/button"; import { UserAuthForm } from "@/components/Auth/user-auth-form-login"; interface LoginProps { - onLoginSuccess: () => void; + onLoginSuccess: (username: string) => void; } const Login: React.FC = ({ onLoginSuccess }) => { @@ -62,7 +62,7 @@ const Login: React.FC = ({ onLoginSuccess }) => { Enter your details below to Login to your account

- + onLoginSuccess(username)} />

By clicking continue, you agree to our{" "}