Spaces:
Paused
Paused
File size: 2,896 Bytes
5eb4857 | 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | import React, { useState, useRef, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Button, Input } from '@nexus/shared';
import styles from './ChatWindow.module.css';
interface Message {
role: 'user' | 'assistant';
content: string;
}
interface ChatWindowProps {
messages: Message[];
onSendMessage: (text: string) => void;
loading?: boolean;
}
export const ChatWindow: React.FC<ChatWindowProps> = ({ messages, onSendMessage, loading }) => {
const [input, setInput] = useState('');
const scrollRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (scrollRef.current) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}, [messages, loading]);
const handleSend = () => {
if (!input.trim()) return;
onSendMessage(input);
setInput('');
};
return (
<div className={styles.container}>
<div className={styles.messages} ref={scrollRef}>
<AnimatePresence mode="popLayout">
{messages.length === 0 ? (
<motion.div
key="welcome"
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.9 }}
className={styles.welcome}
>
<h2>Nexus is Ready</h2>
<p>How can I assist you with your workspace today?</p>
</motion.div>
) : (
messages.map((msg, idx) => (
<motion.div
key={idx}
initial={{ opacity: 0, y: 10, scale: 0.95 }}
animate={{ opacity: 1, y: 0, scale: 1 }}
transition={{ type: 'spring', damping: 20, stiffness: 300 }}
className={`${styles.bubble} ${styles[msg.role]}`}
>
<p className={styles.content}>{msg.content}</p>
</motion.div>
))
)}
{loading && (
<motion.div
key="loading"
initial={{ opacity: 0, y: 5 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0 }}
className={`${styles.bubble} ${styles.assistant} ${styles.loading}`}
>
<div className={styles.dot}></div>
<div className={styles.dot}></div>
<div className={styles.dot}></div>
</motion.div>
)}
</AnimatePresence>
</div>
<div className={styles.footer}>
<Input
placeholder="Ask Nexus anything..."
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && handleSend()}
className={styles.input}
/>
<Button onClick={handleSend} loading={loading} className={styles.sendBtn}>
Send
</Button>
</div>
</div>
);
};
|