diff --git a/src/components/ChatBot.jsx b/src/components/ChatBot.jsx index 508248b..4460028 100644 --- a/src/components/ChatBot.jsx +++ b/src/components/ChatBot.jsx @@ -15,18 +15,64 @@ const ChatBot = () => { const buttonRef = useRef(null); const messagesEndRef = useRef(null); const inputRef = useRef(null); + const messagesContainerRef = useRef(null); - // Auto-scroll to bottom when new messages arrive - useEffect(() => { + // Scroll to bottom helper + const scrollToBottom = () => { if (messagesEndRef.current) { messagesEndRef.current.scrollIntoView({ behavior: "smooth" }); } + }; + + // Auto-scroll to bottom when new messages arrive + useEffect(() => { + scrollToBottom(); }, [messages]); - // Focus input when chat opens + // Scroll to bottom when chat opens + useEffect(() => { + if (isOpen) { + // Small delay to ensure DOM is rendered + setTimeout(scrollToBottom, 100); + if (inputRef.current) { + inputRef.current.focus(); + } + } + }, [isOpen]); + + // Prevent scroll propagation to body useEffect(() => { - if (isOpen && inputRef.current) { - inputRef.current.focus(); + const messagesContainer = messagesContainerRef.current; + + if (isOpen && messagesContainer) { + const preventScroll = (e) => { + e.stopPropagation(); + }; + + messagesContainer.addEventListener("wheel", preventScroll, { + passive: true, + }); + messagesContainer.addEventListener("touchmove", preventScroll, { + passive: true, + }); + + return () => { + messagesContainer.removeEventListener("wheel", preventScroll); + messagesContainer.removeEventListener("touchmove", preventScroll); + }; + } + }, [isOpen]); + + // Prevent body scroll when chat is open + useEffect(() => { + if (isOpen) { + // Store original overflow + const originalOverflow = document.body.style.overflow; + document.body.style.overflow = "hidden"; + + return () => { + document.body.style.overflow = originalOverflow; + }; } }, [isOpen]); @@ -100,181 +146,205 @@ const ChatBot = () => { }; return ( -
- {/* Chat Toggle Button */} - - - {/* Chat Window */} + <> + {/* Overlay for mobile */} {isOpen && (
setIsOpen(false)} + /> + )} + +
+ {/* Chat Toggle Button */} + + + {/* Chat Window */} + {isOpen && ( +
+ {/* Header Section */} +
+
+
+

+ SAST Bot +

+

+ Your astronomy & space guide +

+
+
-
-
- {/* Messages Area */} -
- {messages.length === 0 ? ( -
-

- Welcome! Ask me anything about astronomy, space technology, or - SAST. -

-
- ) : ( - messages.map((message, index) => ( -
+ {/* Messages Area */} +
+ {messages.length === 0 ? ( +
+

+ Welcome! Ask me anything about astronomy, space technology, + or SAST. +

+
+ ) : ( + messages.map((message, index) => (
-
- {formatMessage(message)} -
- {message.createdAt && ( -
- {new Date(message.createdAt).toLocaleTimeString([], { - hour: "2-digit", - minute: "2-digit", - })} + > +
+ {formatMessage(message)}
- )} + {message.createdAt && ( +
+ {new Date(message.createdAt).toLocaleTimeString([], { + hour: "2-digit", + minute: "2-digit", + })} +
+ )} +
-
- )) - )} + )) + )} - {/* Loading indicator */} - {isLoading && ( -
-
-
- - SAST Bot is thinking... + {/* Loading indicator */} + {isLoading && ( +
+
+
+ + + SAST Bot is thinking... + +
-
- )} + )} -
-
+
+
- {/* Input Area */} -
-
- setInputText(e.target.value)} - placeholder="Ask about astronomy, space, or SAST..." - disabled={isLoading} - className=" - flex-1 px-4 py-3 rounded-xl - bg-slate-800 border border-slate-600 - text-white placeholder-slate-400 - focus:outline-none focus:ring-2 focus:ring-blue-500 - disabled:opacity-50 disabled:cursor-not-allowed - text-sm - " - /> - -
+ {/* Input Area */} +
+
+ setInputText(e.target.value)} + placeholder="Ask about astronomy, space, or SAST..." + disabled={isLoading} + className=" + flex-1 px-3 md:px-4 py-2 md:py-3 rounded-xl + bg-slate-800 border border-slate-600 + text-white placeholder-slate-400 + focus:outline-none focus:ring-2 focus:ring-blue-500 + disabled:opacity-50 disabled:cursor-not-allowed + text-xs md:text-sm + " + /> + +
+
-
- )} -
+ )} +
+ ); };