Spaces:
Sleeping
Sleeping
Create static/app.jsx
Browse files- static/app.jsx +57 -0
static/app.jsx
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState } from "https://esm.sh/react";
|
| 2 |
+
import ReactDOM from "https://esm.sh/react-dom";
|
| 3 |
+
|
| 4 |
+
function App() {
|
| 5 |
+
const [messages, setMessages] = useState([]);
|
| 6 |
+
const [input, setInput] = useState("");
|
| 7 |
+
|
| 8 |
+
async function sendMessage() {
|
| 9 |
+
const userMsg = { role: "user", content: input };
|
| 10 |
+
setMessages([...messages, userMsg]);
|
| 11 |
+
setInput("");
|
| 12 |
+
|
| 13 |
+
const res = await fetch("/chat", {
|
| 14 |
+
method: "POST",
|
| 15 |
+
headers: { "Content-Type": "application/json" },
|
| 16 |
+
body: JSON.stringify({ messages: [...messages, userMsg] })
|
| 17 |
+
});
|
| 18 |
+
|
| 19 |
+
const data = await res.json();
|
| 20 |
+
setMessages(m => [...m, data]);
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
return (
|
| 24 |
+
<div className="flex h-screen">
|
| 25 |
+
{/* Sidebar */}
|
| 26 |
+
<div className="w-64 bg-zinc-800 p-4">
|
| 27 |
+
<button className="w-full bg-zinc-700 p-2 rounded">+ New Chat</button>
|
| 28 |
+
</div>
|
| 29 |
+
|
| 30 |
+
{/* Chat */}
|
| 31 |
+
<div className="flex-1 flex flex-col">
|
| 32 |
+
<div className="flex-1 p-6 space-y-4 overflow-y-auto">
|
| 33 |
+
{messages.map((m, i) => (
|
| 34 |
+
<div key={i} className={m.role === "user" ? "text-right" : "text-left"}>
|
| 35 |
+
<div className={`inline-block p-4 rounded-xl max-w-xl ${m.role === "user" ? "bg-blue-600" : "bg-zinc-700"}`}>
|
| 36 |
+
{m.content}
|
| 37 |
+
</div>
|
| 38 |
+
</div>
|
| 39 |
+
))}
|
| 40 |
+
</div>
|
| 41 |
+
|
| 42 |
+
{/* Input */}
|
| 43 |
+
<div className="p-4 border-t border-zinc-700">
|
| 44 |
+
<input
|
| 45 |
+
value={input}
|
| 46 |
+
onChange={e => setInput(e.target.value)}
|
| 47 |
+
onKeyDown={e => e.key === "Enter" && sendMessage()}
|
| 48 |
+
className="w-full bg-zinc-800 p-3 rounded"
|
| 49 |
+
placeholder="Message ChatAI..."
|
| 50 |
+
/>
|
| 51 |
+
</div>
|
| 52 |
+
</div>
|
| 53 |
+
</div>
|
| 54 |
+
);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
|