Skip to content

Commit 555995f

Browse files
committed
feat: added chat feature in UI
1 parent f4ce775 commit 555995f

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed

client/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const App: React.FC = () => {
5252
<Routes>
5353
<Route path="/" element={authenticated ? <Navigate to="/home" /> : <Landing />} />
5454
<Route path="/signup" element={<Signup />} />
55-
<Route path="/login" element={<Login onLoginSuccess={(username) => { setAuthenticated(true); setUsername(username); }} />} />
55+
<Route path="/login" element={<Login onLoginSuccess={(username: string) => { setAuthenticated(true); setUsername(username); }} />} />
5656
<Route path="/home" element={authenticated ? <Home onLogout={handleLogout} username={username || ''} /> : <Navigate to="/" />} />
5757
<Route path="/u/:username" element={<Profile onLogout={handleLogout} username={username || ''} />} />
5858
<Route path="*" element={<Navigate to="/" />} />

client/src/components/chat.tsx

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
"use client";
22

33
import { PlaceholdersAndVanishInput } from "./ui/placeholders-and-vanish-input";
4+
import { useState, useEffect } from "react";
5+
import axios from "axios";
46

57
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+
614
const placeholders = [
715
"What's the first rule of Fight Club?",
816
"Who is Tyler Durden?",
@@ -12,22 +20,58 @@ export function PlaceholdersAndVanishInputDemo() {
1220
];
1321

1422
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
15-
console.log(e.target.value);
23+
// You can add any additional logic here if needed
1624
};
17-
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
25+
26+
const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
1827
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+
}
2041
};
42+
2143
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
2547
</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>
3175
</div>
3276
);
3377
}

client/src/pages/Login.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { buttonVariants } from "@/components/ui/button";
55
import { UserAuthForm } from "@/components/Auth/user-auth-form-login";
66

77
interface LoginProps {
8-
onLoginSuccess: () => void;
8+
onLoginSuccess: (username: string) => void;
99
}
1010

1111
const Login: React.FC<LoginProps> = ({ onLoginSuccess }) => {
@@ -62,7 +62,7 @@ const Login: React.FC<LoginProps> = ({ onLoginSuccess }) => {
6262
Enter your details below to Login to your account
6363
</p>
6464
</div>
65-
<UserAuthForm onLoginSuccess={onLoginSuccess} />
65+
<UserAuthForm onLoginSuccess={(username: string) => onLoginSuccess(username)} />
6666
<p className="px-8 text-center text-sm text-muted-foreground">
6767
By clicking continue, you agree to our{" "}
6868
<Link

0 commit comments

Comments
 (0)