diff --git a/src/pages/dashboard/ask.tsx b/src/pages/dashboard/ask.tsx index 9db16fa..b02a9fc 100644 --- a/src/pages/dashboard/ask.tsx +++ b/src/pages/dashboard/ask.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect, useRef } from 'react'; -import { Send, Loader2, X, History, MessageSquarePlus, AlertCircle } from 'lucide-react'; +import { Send, Loader2, X, History, MessageSquarePlus, AlertCircle, ArrowDown } from 'lucide-react'; import { Link, useParams, useNavigate } from 'react-router-dom'; import { Button } from '../../components/ui/Button'; import { Message } from '../../components/chat/Message'; @@ -21,6 +21,7 @@ export default function AskQuestion() { const [isNewChat, setIsNewChat] = useState(false); const [hasExistingChats, setHasExistingChats] = useState(null); const [isTyping, setIsTyping] = useState(false); + const [showScrollButton, setShowScrollButton] = useState(false); const messagesEndRef = useRef(null); const scrollAreaRef = useRef(null); @@ -168,14 +169,53 @@ export default function AskQuestion() { } }; + // Function to handle scroll events + const handleScroll = () => { + if (scrollAreaRef.current) { + const { scrollTop, scrollHeight, clientHeight } = scrollAreaRef.current; + const isAtBottom = scrollHeight - scrollTop - clientHeight < 100; + setShowScrollButton(!isAtBottom); + } + }; + + // Add scroll event listener + useEffect(() => { + const scrollArea = scrollAreaRef.current; + if (scrollArea) { + scrollArea.addEventListener('scroll', handleScroll); + // Initial check for scroll position + handleScroll(); + return () => scrollArea.removeEventListener('scroll', handleScroll); + } + }, []); + // Auto-scroll when new messages arrive useEffect(() => { if (scrollAreaRef.current) { const scrollArea = scrollAreaRef.current; - scrollArea.scrollTop = scrollArea.scrollHeight; + const { scrollTop, scrollHeight, clientHeight } = scrollArea; + const isAtBottom = scrollHeight - scrollTop - clientHeight < 100; + + // Only auto-scroll if user is already at bottom + if (isAtBottom) { + scrollArea.scrollTop = scrollArea.scrollHeight; + } + + // Check if we should show scroll button + handleScroll(); } }, [messages, isTyping]); + // Function to scroll to bottom + const scrollToBottom = () => { + if (scrollAreaRef.current) { + scrollAreaRef.current.scrollTo({ + top: scrollAreaRef.current.scrollHeight, + behavior: 'smooth' + }); + } + }; + // Show loading state while checking for existing chats if (hasExistingChats === null) { return ( @@ -240,17 +280,20 @@ export default function AskQuestion() { - {/* Main Chat Container - Added pb-20 to create space at bottom */} -
- {/* Messages Container */} -
- {error && ( -
- - {error} -
- )} + {/* Main Chat Container */} +
+ {/* Messages Container with ref moved to correct div */} +
+ {error && ( +
+ + {error} +
+ )} {!isNewChat && !chatId ? (
@@ -372,68 +415,96 @@ export default function AskQuestion() { )}
-
-
- {/* Input Box - Keep original styling */} -
-
-
-
-