diff --git a/packages/webui/components/chatbot-sidebar.tsx b/packages/webui/components/chatbot-sidebar.tsx index 1cd85128..2a1560fe 100644 --- a/packages/webui/components/chatbot-sidebar.tsx +++ b/packages/webui/components/chatbot-sidebar.tsx @@ -88,6 +88,61 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp const [inputText, setInputText] = useState('') const messagesEndRef = useRef(null) + const scrollAreaRef = useRef(null) + const isFirstScrollRef = useRef(true) + + const isRestoringRef = useRef(true) + + useEffect(() => { + isRestoringRef.current = true + isFirstScrollRef.current = true + }, [projectId, contextAssetId, isHistoryMode]) + + // Restore scroll position on mount or when navigation changes the project/context + useEffect(() => { + const viewport = scrollAreaRef.current?.querySelector('[data-slot="scroll-area-viewport"]') + if (!viewport) return + + const { scrollTop, isAtBottom } = useChatbotStore.getState() + + // Use a timeout to ensure the DOM has settled and scrollHeight is fully calculated + const timer = setTimeout(() => { + if (isAtBottom) { + viewport.scrollTop = viewport.scrollHeight + } else { + viewport.scrollTop = scrollTop + } + + // Allow scroll events triggered by this restoration to fire and be ignored before tracking + const enableTimer = setTimeout(() => { + isRestoringRef.current = false + }, 50) + + return () => clearTimeout(enableTimer) + }, 0) + + return () => clearTimeout(timer) + }, [projectId, contextAssetId, isHistoryMode]) + + // Listen to scroll events on the viewport to save scroll state + useEffect(() => { + const viewport = scrollAreaRef.current?.querySelector('[data-slot="scroll-area-viewport"]') + if (!viewport) return + + const handleScroll = () => { + if (isRestoringRef.current) return + + const scrollTop = viewport.scrollTop + // We consider the user at the bottom if they are within 10px of the bottom boundary + const isAtBottom = viewport.scrollHeight - scrollTop - viewport.clientHeight < 10 + useChatbotStore.getState().setScrollState(scrollTop, isAtBottom) + } + + viewport.addEventListener('scroll', handleScroll) + return () => { + viewport.removeEventListener('scroll', handleScroll) + } + }, [projectId, contextAssetId, isHistoryMode]) const { ref: setDroppableRef, isDropTarget: isOver } = useDroppable({ id: 'chatbot-sidebar', @@ -99,7 +154,12 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp // Auto-scroll messages list to bottom useEffect(() => { if (!isHistoryMode) { - messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) + const { isAtBottom } = useChatbotStore.getState() + if (isAtBottom || isStreaming) { + const behavior = isFirstScrollRef.current ? 'auto' : 'smooth' + messagesEndRef.current?.scrollIntoView({ behavior }) + } + isFirstScrollRef.current = false } }, [messages, isStreaming, isHistoryMode]) @@ -473,7 +533,7 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp ) : ( <> {/* Active Chat view */} - + {messages.length === 0 ? (
diff --git a/packages/webui/stores/chatbot.ts b/packages/webui/stores/chatbot.ts index 83b6c5bf..5fef4fb9 100644 --- a/packages/webui/stores/chatbot.ts +++ b/packages/webui/stores/chatbot.ts @@ -29,6 +29,10 @@ interface ChatbotState { selectedAgentId: string | null setSelectedAgentId: (id: string | null) => void + scrollTop: number + isAtBottom: boolean + setScrollState: (scrollTop: number, isAtBottom: boolean) => void + fetchHistorySessions: (teamId: string) => Promise loadSession: (teamId: string, sessionId: string) => Promise deleteSession: (teamId: string, sessionId: string) => Promise @@ -68,6 +72,9 @@ export const useChatbotStore = create((set) => ({ setIsStreaming: (streaming) => set({ isStreaming: streaming }), selectedAgentId: null, setSelectedAgentId: (id) => set({ selectedAgentId: id }), + scrollTop: 0, + isAtBottom: true, + setScrollState: (scrollTop, isAtBottom) => set({ scrollTop, isAtBottom }), fetchHistorySessions: async (teamId) => { try { @@ -99,6 +106,8 @@ export const useChatbotStore = create((set) => ({ currentSessionId: sessionId, messages: messages as ChatMessage[], isHistoryMode: false, + scrollTop: 0, + isAtBottom: true, ...(agentId ? { selectedAgentId: agentId } : {}), }) } else { @@ -136,6 +145,8 @@ export const useChatbotStore = create((set) => ({ currentSessionId: null, messages: [], chatAssets: [], + scrollTop: 0, + isAtBottom: true, }) }, @@ -175,6 +186,7 @@ export const useChatbotStore = create((set) => ({ messages: newMessages, isStreaming: true, chatAssets: [], + isAtBottom: true, }) const assetIds = state.chatAssets.map((a) => a.id)