Spaces:
No application file
No application file
| 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> | |
| ); | |
| } | |