Spaces:
No application file
No application file
File size: 1,544 Bytes
2be9eb9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import { useState, useEffect, useRef } from 'react';
import styles from '../styles/index.module.css';
import ReactMarkdown from 'react-markdown';
interface ChatWindowProps {
messages: { sender: 'user' | 'bot'; text: string }[];
onSendMessage: (message: string) => void;
}
export default function ChatWindow({
messages,
onSendMessage,
}: ChatWindowProps) {
const [input, setInput] = useState('');
const messagesEndRef = useRef<HTMLDivElement>(null);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
onSendMessage(input);
setInput('');
};
useEffect(() => {
if (messagesEndRef.current) {
messagesEndRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, [messages]);
return (
<div className={styles.chatWindow}>
<div className={styles.messages}>
{messages.map((msg, index) => (
<div
key={index}
className={`${styles.message} ${msg.sender === 'bot' ? styles.bot : styles.user}`}
>
<ReactMarkdown>{msg.text}</ReactMarkdown>
</div>
))}
<div ref={messagesEndRef} />
</div>
<form onSubmit={handleSubmit} className={styles.inputForm}>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Type a message..."
className={styles.inputField}
/>
<button type="submit" className={styles.sendButton}>
Send
</button>
</form>
</div>
);
}
|