Spaces:
Running
Running
Extract MessageInput, MessageBubble and MessagePair components
Browse filesmodified: src/components/ChatInterface.jsx
new file: src/components/MessageBubble.jsx
new file: src/components/MessageInput.jsx
new file: src/components/MessagePair.jsx
- src/components/ChatInterface.jsx +11 -47
- src/components/MessageBubble.jsx +23 -0
- src/components/MessageInput.jsx +26 -0
- src/components/MessagePair.jsx +10 -0
src/components/ChatInterface.jsx
CHANGED
|
@@ -1,17 +1,14 @@
|
|
| 1 |
import { useState } from "react";
|
|
|
|
|
|
|
| 2 |
|
| 3 |
export default function ChatInterface() {
|
| 4 |
-
const [input, setInput] = useState("");
|
| 5 |
const [chatHistory, setChatHistory] = useState([]);
|
| 6 |
|
| 7 |
-
const
|
| 8 |
-
if (!input.trim()) return;
|
| 9 |
-
|
| 10 |
const fakeResponse =
|
| 11 |
"This is a simulated response.\nIt can span multiple lines.";
|
| 12 |
-
const newPair = { user:
|
| 13 |
-
|
| 14 |
-
setInput("");
|
| 15 |
setChatHistory([newPair, ...chatHistory]);
|
| 16 |
};
|
| 17 |
|
|
@@ -27,51 +24,18 @@ export default function ChatInterface() {
|
|
| 27 |
}}
|
| 28 |
>
|
| 29 |
{/* Input area */}
|
| 30 |
-
<div
|
| 31 |
-
<
|
| 32 |
-
type="text"
|
| 33 |
-
value={input}
|
| 34 |
-
onChange={(e) => setInput(e.target.value)}
|
| 35 |
-
placeholder="Type your message..."
|
| 36 |
-
style={{ flex: 1, padding: "10px", fontSize: "16px" }}
|
| 37 |
-
/>
|
| 38 |
-
<button onClick={handleSend} style={{ padding: "10px 20px" }}>
|
| 39 |
-
Send
|
| 40 |
-
</button>
|
| 41 |
</div>
|
| 42 |
|
| 43 |
{/* Chat history */}
|
| 44 |
<div>
|
| 45 |
{chatHistory.map((pair, index) => (
|
| 46 |
-
<
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
padding: "10px",
|
| 52 |
-
borderRadius: "8px",
|
| 53 |
-
whiteSpace: "pre-wrap",
|
| 54 |
-
marginBottom: "4px",
|
| 55 |
-
}}
|
| 56 |
-
>
|
| 57 |
-
{pair.user}
|
| 58 |
-
</div>
|
| 59 |
-
|
| 60 |
-
<div style={{ fontWeight: "bold", margin: "8px 0 4px" }}>
|
| 61 |
-
Assistant:
|
| 62 |
-
</div>
|
| 63 |
-
<div
|
| 64 |
-
style={{
|
| 65 |
-
backgroundColor: "#f1f1f1",
|
| 66 |
-
padding: "10px",
|
| 67 |
-
borderRadius: "8px",
|
| 68 |
-
whiteSpace: "pre-wrap",
|
| 69 |
-
marginBottom: "4px",
|
| 70 |
-
}}
|
| 71 |
-
>
|
| 72 |
-
{pair.assistant}
|
| 73 |
-
</div>
|
| 74 |
-
</div>
|
| 75 |
))}
|
| 76 |
</div>
|
| 77 |
</div>
|
|
|
|
| 1 |
import { useState } from "react";
|
| 2 |
+
import MessageInput from "./MessageInput";
|
| 3 |
+
import MessagePair from "./MessagePair";
|
| 4 |
|
| 5 |
export default function ChatInterface() {
|
|
|
|
| 6 |
const [chatHistory, setChatHistory] = useState([]);
|
| 7 |
|
| 8 |
+
const handleMessageSend = (message) => {
|
|
|
|
|
|
|
| 9 |
const fakeResponse =
|
| 10 |
"This is a simulated response.\nIt can span multiple lines.";
|
| 11 |
+
const newPair = { user: message, assistant: fakeResponse };
|
|
|
|
|
|
|
| 12 |
setChatHistory([newPair, ...chatHistory]);
|
| 13 |
};
|
| 14 |
|
|
|
|
| 24 |
}}
|
| 25 |
>
|
| 26 |
{/* Input area */}
|
| 27 |
+
<div>
|
| 28 |
+
<MessageInput onSend={handleMessageSend} />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
</div>
|
| 30 |
|
| 31 |
{/* Chat history */}
|
| 32 |
<div>
|
| 33 |
{chatHistory.map((pair, index) => (
|
| 34 |
+
<MessagePair
|
| 35 |
+
key={index}
|
| 36 |
+
userMessage={pair.user}
|
| 37 |
+
assistantMessage={pair.assistant}
|
| 38 |
+
/>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
))}
|
| 40 |
</div>
|
| 41 |
</div>
|
src/components/MessageBubble.jsx
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export default function MessageBubble({ role, content }) {
|
| 2 |
+
const isUser = role === "user";
|
| 3 |
+
const bubbleStyle = {
|
| 4 |
+
backgroundColor: isUser ? "#e0f7fa" : "#f1f1f1",
|
| 5 |
+
color: "#333",
|
| 6 |
+
padding: "10px",
|
| 7 |
+
borderRadius: "8px",
|
| 8 |
+
whiteSpace: "pre-wrap",
|
| 9 |
+
marginBottom: "4px",
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
const labelStyle = {
|
| 13 |
+
fontWeight: "bold",
|
| 14 |
+
marginBottom: "4px",
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
return (
|
| 18 |
+
<div>
|
| 19 |
+
<div style={labelStyle}>{isUser ? "User:" : "Assistant:"}</div>
|
| 20 |
+
<div style={bubbleStyle}>{content}</div>
|
| 21 |
+
</div>
|
| 22 |
+
);
|
| 23 |
+
}
|
src/components/MessageInput.jsx
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { useState } from "react";
|
| 2 |
+
|
| 3 |
+
export default function MessageInput({ onSend }) {
|
| 4 |
+
const [input, setInput] = useState("");
|
| 5 |
+
|
| 6 |
+
const handleSend = () => {
|
| 7 |
+
if (!input.trim()) return;
|
| 8 |
+
onSend(input);
|
| 9 |
+
setInput("");
|
| 10 |
+
};
|
| 11 |
+
|
| 12 |
+
return (
|
| 13 |
+
<div style={{ display: "flex", gap: "10px" }}>
|
| 14 |
+
<input
|
| 15 |
+
type="text"
|
| 16 |
+
value={input}
|
| 17 |
+
onChange={(e) => setInput(e.target.value)}
|
| 18 |
+
placeholder="Type your message..."
|
| 19 |
+
style={{ flex: 1, padding: "10px", fontSize: "16px" }}
|
| 20 |
+
/>
|
| 21 |
+
<button onClick={handleSend} style={{ padding: "10px 20px" }}>
|
| 22 |
+
Send
|
| 23 |
+
</button>
|
| 24 |
+
</div>
|
| 25 |
+
);
|
| 26 |
+
}
|
src/components/MessagePair.jsx
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import MessageBubble from "./MessageBubble";
|
| 2 |
+
|
| 3 |
+
export default function MessagePair({ userMessage, assistantMessage }) {
|
| 4 |
+
return (
|
| 5 |
+
<div style={{ marginBottom: "16px" }}>
|
| 6 |
+
<MessageBubble role="user" content={userMessage} />
|
| 7 |
+
<MessageBubble role="assistant" content={assistantMessage} />
|
| 8 |
+
</div>
|
| 9 |
+
);
|
| 10 |
+
}
|