import { useEffect, useRef, useState, useCallback } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { ArrowDown } from 'lucide-react'; import MessageBubble from './MessageBubble'; import WelcomeScreen from './WelcomeScreen'; import useChatStore from '../../store/useChatStore'; export default function ChatWindow({ onPrompt, onRegenerate }) { const chat = useChatStore(s => s.getActiveChat()); const bottomRef = useRef(null); const scrollRef = useRef(null); const [showScrollBtn, setShowScrollBtn] = useState(false); const messages = chat?.messages ?? []; // Auto-scroll to bottom on new messages / streaming useEffect(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages.length, messages[messages.length - 1]?.streaming]); // Show scroll-to-bottom button when user scrolls up const handleScroll = useCallback(() => { const el = scrollRef.current; if (!el) return; const distFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight; setShowScrollBtn(distFromBottom > 200); }, []); const scrollToBottom = useCallback(() => { bottomRef.current?.scrollIntoView({ behavior: 'smooth' }); }, []); return (
{messages.length === 0 ? ( ) : (
{messages.map((msg) => ( onRegenerate(msg.id)} /> ))}
)}
{/* Scroll-to-bottom FAB */} {showScrollBtn && messages.length > 0 && ( )}
); }