Spaces:
Sleeping
Sleeping
Create pages/index.js
Browse files- pages/index.js +105 -0
pages/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState } from "react";
|
| 2 |
+
|
| 3 |
+
export default function Home() {
|
| 4 |
+
const [projectName, setProjectName] = useState("");
|
| 5 |
+
const [projectDescription, setProjectDescription] = useState("");
|
| 6 |
+
const [projects, setProjects] = useState([]);
|
| 7 |
+
const [messages, setMessages] = useState([]);
|
| 8 |
+
const [input, setInput] = useState("");
|
| 9 |
+
|
| 10 |
+
const handleAddProject = () => {
|
| 11 |
+
if (!projectName) return;
|
| 12 |
+
setProjects([...projects, { name: projectName, description: projectDescription }]);
|
| 13 |
+
setProjectName("");
|
| 14 |
+
setProjectDescription("");
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
const handleSendMessage = async () => {
|
| 18 |
+
if (!input) return;
|
| 19 |
+
const newMessage = { role: "user", content: input };
|
| 20 |
+
setMessages((prev) => [...prev, newMessage]);
|
| 21 |
+
|
| 22 |
+
try {
|
| 23 |
+
const res = await fetch("/api/chat", {
|
| 24 |
+
method: "POST",
|
| 25 |
+
headers: { "Content-Type": "application/json" },
|
| 26 |
+
body: JSON.stringify({ messages: [...messages, newMessage] }),
|
| 27 |
+
});
|
| 28 |
+
const data = await res.json();
|
| 29 |
+
setMessages((prev) => [...prev, { role: "assistant", content: data.reply }]);
|
| 30 |
+
} catch (err) {
|
| 31 |
+
setMessages((prev) => [...prev, { role: "assistant", content: "⚠️ Error fetching response" }]);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
setInput("");
|
| 35 |
+
};
|
| 36 |
+
|
| 37 |
+
return (
|
| 38 |
+
<div className="min-h-screen flex flex-col items-center p-6">
|
| 39 |
+
<h1 className="text-3xl font-bold mb-6">🧪 Bio Lab AI</h1>
|
| 40 |
+
|
| 41 |
+
{/* Projects */}
|
| 42 |
+
<div className="w-full max-w-2xl mb-6">
|
| 43 |
+
<h2 className="text-xl font-semibold mb-2">Projects</h2>
|
| 44 |
+
<div className="flex gap-2 mb-3">
|
| 45 |
+
<input
|
| 46 |
+
className="flex-1 p-2 border rounded-lg"
|
| 47 |
+
placeholder="Project name"
|
| 48 |
+
value={projectName}
|
| 49 |
+
onChange={(e) => setProjectName(e.target.value)}
|
| 50 |
+
/>
|
| 51 |
+
<input
|
| 52 |
+
className="flex-1 p-2 border rounded-lg"
|
| 53 |
+
placeholder="Project description"
|
| 54 |
+
value={projectDescription}
|
| 55 |
+
onChange={(e) => setProjectDescription(e.target.value)}
|
| 56 |
+
/>
|
| 57 |
+
<button
|
| 58 |
+
onClick={handleAddProject}
|
| 59 |
+
className="px-4 py-2 bg-blue-600 text-white rounded-lg"
|
| 60 |
+
>
|
| 61 |
+
Add
|
| 62 |
+
</button>
|
| 63 |
+
</div>
|
| 64 |
+
<ul className="space-y-1">
|
| 65 |
+
{projects.map((p, i) => (
|
| 66 |
+
<li key={i} className="p-2 bg-white rounded shadow">
|
| 67 |
+
<strong>{p.name}</strong> — {p.description}
|
| 68 |
+
</li>
|
| 69 |
+
))}
|
| 70 |
+
</ul>
|
| 71 |
+
</div>
|
| 72 |
+
|
| 73 |
+
{/* Chat */}
|
| 74 |
+
<div className="w-full max-w-2xl flex-1 flex flex-col border rounded-lg bg-white shadow p-4">
|
| 75 |
+
<div className="flex-1 overflow-y-auto space-y-2 mb-3">
|
| 76 |
+
{messages.map((m, i) => (
|
| 77 |
+
<div
|
| 78 |
+
key={i}
|
| 79 |
+
className={`p-2 rounded-lg max-w-xs ${
|
| 80 |
+
m.role === "user" ? "bg-blue-100 self-end" : "bg-gray-100 self-start"
|
| 81 |
+
}`}
|
| 82 |
+
>
|
| 83 |
+
<span>{m.content}</span>
|
| 84 |
+
</div>
|
| 85 |
+
))}
|
| 86 |
+
</div>
|
| 87 |
+
<div className="flex gap-2">
|
| 88 |
+
<input
|
| 89 |
+
className="flex-1 p-2 border rounded-lg"
|
| 90 |
+
placeholder="Type a message..."
|
| 91 |
+
value={input}
|
| 92 |
+
onChange={(e) => setInput(e.target.value)}
|
| 93 |
+
onKeyDown={(e) => e.key === "Enter" && handleSendMessage()}
|
| 94 |
+
/>
|
| 95 |
+
<button
|
| 96 |
+
onClick={handleSendMessage}
|
| 97 |
+
className="px-4 py-2 bg-green-600 text-white rounded-lg"
|
| 98 |
+
>
|
| 99 |
+
Send
|
| 100 |
+
</button>
|
| 101 |
+
</div>
|
| 102 |
+
</div>
|
| 103 |
+
</div>
|
| 104 |
+
);
|
| 105 |
+
}
|