Spaces:
Paused

miracle / frontend /apps /customer /src /components /ChatWindow.tsx
dushyantsingh0
Beta 1.0: Pure Backend Release (Excluding Frontend)
5eb4857
Raw History Blame Contribute Delete
2.9 kB
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>
);
};