π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix: defer scroll-to-user-message until spacer is mounted (#13378)
Inneie7c496352f52
diff --git a/src/features/Conversation/ChatList/components/VirtualizedList.tsx b/src/features/Conversation/ChatList/components/VirtualizedList.tsxindex 1a30e63b334..963e43de23f 100644--- a/src/features/Conversation/ChatList/components/VirtualizedList.tsx+++ b/src/features/Conversation/ChatList/components/VirtualizedList.tsx@@ -32,8 +32,14 @@ interface VirtualizedListProps {const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent }) => {const virtuaRef = useRef<VListHandle>(null);const scrollEndTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);- const { isSpacerMessage, listData, spacerHeight, spacerActive } =- useConversationSpacer(dataSource);+ const {+ handleScrollOffset,+ isSpacerMessage,+ listData,+ scrollShrinking,+ spacerHeight,+ spacerActive,+ } = useConversationSpacer(dataSource);const isAutoScrollEnabled = useAutoScrollEnabled();// Store actions@@ -69,6 +75,12 @@ const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent })setScrollState({ isScrolling: true });+ // Shrink spacer on scroll up when not streaming+ const ref = virtuaRef.current;+ if (ref) {+ handleScrollOffset(ref.scrollOffset);+ }+// Check if at bottomconst isAtBottom = checkAtBottom();setScrollState({ atBottom: isAtBottom });@@ -82,7 +94,7 @@ const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent })scrollEndTimerRef.current = setTimeout(() => {setScrollState({ isScrolling: false });}, 150);- }, [activeIndex, checkAtBottom, setActiveIndex, setScrollState]);+ }, [activeIndex, checkAtBottom, handleScrollOffset, setActiveIndex, setScrollState]);const handleScrollEnd = useCallback(() => {setScrollState({ isScrolling: false });@@ -136,6 +148,7 @@ const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent })dataSourceLength: dataSource.length,isSecondLastMessageFromUser,scrollToIndex: virtuaRef.current?.scrollToIndex ?? null,+ spacerActive,});// Scroll to bottom on initial render@@ -169,7 +182,9 @@ const VirtualizedList = memo<VirtualizedListProps>(({ dataSource, itemContent })style={{height: spacerHeight,pointerEvents: 'none',- transition: `height ${CONVERSATION_SPACER_TRANSITION_MS}ms ease`,+ transition: scrollShrinking+ ? 'none'+ : `height ${CONVERSATION_SPACER_TRANSITION_MS}ms ease`,width: '100%',}}/>diff --git a/src/features/Conversation/ChatList/hooks/useConversationSpacer.ts b/src/features/Conversation/ChatList/hooks/useConversationSpacer.tsindex 0dc4c401ac2..6829f2d2f89 100644--- a/src/features/Conversation/ChatList/hooks/useConversationSpacer.ts+++ b/src/features/Conversation/ChatList/hooks/useConversationSpacer.ts@@ -42,7 +42,8 @@ export const useConversationSpacer = (dataSource: string[]) => {const getItemSize = useConversationStore((s) => s.virtuaScrollMethods?.getItemSize);const getViewportSize = useConversationStore((s) => s.virtuaScrollMethods?.getViewportSize);- const [height, setHeight] = useState(0);+ const [naturalHeight, setNaturalHeight] = useState(0);+ const [scrollReduction, setScrollReduction] = useState(0);const [mounted, setMounted] = useState(false);const prevLengthRef = useRef(dataSource.length);@@ -51,6 +52,18 @@ export const useConversationSpacer = (dataSource: string[]) => {const userMessageIndexRef = useRef<number | null>(null);const assistantMessageIndexRef = useRef<number | null>(null);+ // Refs for scroll-shrink feature (stable callback access)+ const mountedRef = useRef(false);+ mountedRef.current = mounted;+ const isAIGeneratingRef = useRef(isAIGenerating);+ isAIGeneratingRef.current = isAIGenerating;+ const prevScrollOffsetRef = useRef<number | null>(null);+ const scrollShrinkEndTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);++ // Derived rendered height: natural height minus user scroll reduction+ const renderedHeight = Math.max(naturalHeight - scrollReduction, 0);+ const isScrollShrinking = scrollReduction > 0;+const getTrackedMessages = useCallback(() => {const userIndex = userMessageIndexRef.current;const assistantIndex = assistantMessageIndexRef.current;@@ -115,13 +128,13 @@ export const useConversationSpacer = (dataSource: string[]) => {}if (nextHeight === 0) {- setHeight(0);+ setNaturalHeight(0);scheduleUnmount();return;}setMounted(true);- setHeight(nextHeight);+ setNaturalHeight(nextHeight);}, [clearRemoveTimer,getTrackedMessages,@@ -131,10 +144,43 @@ export const useConversationSpacer = (dataSource: string[]) => {scheduleUnmount,]);+ // Reset prev scroll offset when generation state changes to avoid stale deltas+ useEffect(() => {+ prevScrollOffsetRef.current = null;+ }, [isAIGenerating]);++ // Stable scroll handler for shrinking spacer on scroll-up when not streaming+ const handleScrollOffset = useCallback((currentScrollOffset: number) => {+ const prevOffset = prevScrollOffsetRef.current;+ prevScrollOffsetRef.current = currentScrollOffset;++ if (!mountedRef.current || isAIGeneratingRef.current || prevOffset === null) return;++ const delta = currentScrollOffset - prevOffset;+ if (delta >= 0) return;++ setScrollReduction((prev) => prev + Math.abs(delta));++ if (scrollShrinkEndTimerRef.current) clearTimeout(scrollShrinkEndTimerRef.current);+ scrollShrinkEndTimerRef.current = setTimeout(() => {+ scrollShrinkEndTimerRef.current = null;+ }, 150);+ }, []);++ // Unmount when rendered height reaches zero via scroll reduction+ useEffect(() => {+ if (renderedHeight === 0 && mounted && scrollReduction > 0) {+ setMounted(false);+ setScrollReduction(0);+ prevScrollOffsetRef.current = null;+ }+ }, [renderedHeight, mounted, scrollReduction]);+useEffect(() => {return () => {cleanupObserver();clearRemoveTimer();+ if (scrollShrinkEndTimerRef.current) clearTimeout(scrollShrinkEndTimerRef.current);};}, [cleanupObserver, clearRemoveTimer]);@@ -147,6 +193,8 @@ export const useConversationSpacer = (dataSource: string[]) => {if (newMessageCount !== 2 || userMessage?.role !== 'user' || !assistantMessage) return;+ setScrollReduction(0);+ prevScrollOffsetRef.current = null;userMessageIndexRef.current = dataSource.length - 2;assistantMessageIndexRef.current = dataSource.length - 1;@@ -197,9 +245,11 @@ export const useConversationSpacer = (dataSource: string[]) => {);return {+ handleScrollOffset,isSpacerMessage: (id: string) => id === CONVERSATION_SPACER_ID,listData,+ scrollShrinking: isScrollShrinking,spacerActive: mounted,- spacerHeight: height,+ spacerHeight: renderedHeight,};};diff --git a/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.test.ts b/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.test.tsindex 61c2ad8dd39..49295f93e70 100644--- a/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.test.ts+++ b/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.test.ts@@ -13,28 +13,32 @@ describe('useScrollToUserMessage', () => {});describe('when user sends a new message', () => {- it('should retry scrolling to user message when 2 new messages are added (user + assistant pair)', () => {+ it('should retry scrolling to user message when 2 new messages are added and spacer is active', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 2,isSecondLastMessageFromUser: false,+ spacerActive: false,},},);// User sends a new message (2 messages added: user + assistant, second-to-last is user)+ // spacerActive is already true (e.g. from previous message)rerender({dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: true,});act(() => {@@ -47,20 +51,111 @@ describe('useScrollToUserMessage', () => {expect(scrollToIndex).toHaveBeenNthCalledWith(3, 2, { align: 'start', smooth: true });});+ it('should scroll immediately and re-scroll when spacer becomes active', () => {+ const scrollToIndex = vi.fn();++ const { rerender } = renderHook(+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>+ useScrollToUserMessage({+ dataSourceLength,+ isSecondLastMessageFromUser,+ scrollToIndex,+ spacerActive,+ }),+ {+ initialProps: {+ dataSourceLength: 2,+ isSecondLastMessageFromUser: false,+ spacerActive: false,+ },+ },+ );++ // User sends a new message, spacer not yet active+ rerender({+ dataSourceLength: 4,+ isSecondLastMessageFromUser: true,+ spacerActive: false,+ });++ act(() => {+ vi.runAllTimers();+ });++ // Should scroll immediately even without spacer (content may fill viewport)+ expect(scrollToIndex).toHaveBeenCalledTimes(3);+ expect(scrollToIndex).toHaveBeenNthCalledWith(1, 2, { align: 'start', smooth: true });++ scrollToIndex.mockClear();++ // Spacer becomes active β should re-scroll with correct fill height+ rerender({+ dataSourceLength: 4,+ isSecondLastMessageFromUser: true,+ spacerActive: true,+ });++ act(() => {+ vi.runAllTimers();+ });++ // Follow-up scroll after spacer mounted+ expect(scrollToIndex).toHaveBeenCalledTimes(3);+ expect(scrollToIndex).toHaveBeenNthCalledWith(1, 2, { align: 'start', smooth: true });+ });++ it('should scroll without spacer when spacer never mounts (content fills viewport)', () => {+ const scrollToIndex = vi.fn();++ const { rerender } = renderHook(+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>+ useScrollToUserMessage({+ dataSourceLength,+ isSecondLastMessageFromUser,+ scrollToIndex,+ spacerActive,+ }),+ {+ initialProps: {+ dataSourceLength: 2,+ isSecondLastMessageFromUser: false,+ spacerActive: false,+ },+ },+ );++ // User sends a new message, spacer will never mount (height = 0)+ rerender({+ dataSourceLength: 4,+ isSecondLastMessageFromUser: true,+ spacerActive: false,+ });++ act(() => {+ vi.runAllTimers();+ });++ // Should still scroll β no spacer needed when content fills viewport+ expect(scrollToIndex).toHaveBeenCalledTimes(3);+ expect(scrollToIndex).toHaveBeenNthCalledWith(1, 2, { align: 'start', smooth: true });+ });+it('should scroll to correct index when multiple user messages are sent', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 4,isSecondLastMessageFromUser: false,+ spacerActive: false,},},);@@ -69,6 +164,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 6,isSecondLastMessageFromUser: true,+ spacerActive: true,});act(() => {@@ -84,16 +180,18 @@ describe('useScrollToUserMessage', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: false,},},);@@ -102,6 +200,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 5,isSecondLastMessageFromUser: false,+ spacerActive: true,});expect(scrollToIndex).not.toHaveBeenCalled();@@ -111,16 +210,18 @@ describe('useScrollToUserMessage', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: false,},},);@@ -129,6 +230,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 5,isSecondLastMessageFromUser: false,+ spacerActive: true,});expect(scrollToIndex).not.toHaveBeenCalled();@@ -137,6 +239,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 6,isSecondLastMessageFromUser: false,+ spacerActive: true,});expect(scrollToIndex).not.toHaveBeenCalled();@@ -146,16 +249,18 @@ describe('useScrollToUserMessage', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 4,isSecondLastMessageFromUser: false,+ spacerActive: false,},},);@@ -164,6 +269,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 6,isSecondLastMessageFromUser: false,+ spacerActive: true,});expect(scrollToIndex).not.toHaveBeenCalled();@@ -175,16 +281,18 @@ describe('useScrollToUserMessage', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 6,isSecondLastMessageFromUser: true,+ spacerActive: false,},},);@@ -193,6 +301,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: true,});expect(scrollToIndex).not.toHaveBeenCalled();@@ -202,16 +311,18 @@ describe('useScrollToUserMessage', () => {const scrollToIndex = vi.fn();const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}),{initialProps: {dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: false,},},);@@ -220,6 +331,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: true,});expect(scrollToIndex).not.toHaveBeenCalled();@@ -227,16 +339,18 @@ describe('useScrollToUserMessage', () => {it('should handle null scrollToIndex gracefully', () => {const { rerender } = renderHook(- ({ dataSourceLength, isSecondLastMessageFromUser }) =>+ ({ dataSourceLength, isSecondLastMessageFromUser, spacerActive }) =>useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex: null,+ spacerActive,}),{initialProps: {dataSourceLength: 2,isSecondLastMessageFromUser: false,+ spacerActive: false,},},);@@ -246,6 +360,7 @@ describe('useScrollToUserMessage', () => {rerender({dataSourceLength: 4,isSecondLastMessageFromUser: true,+ spacerActive: true,});}).not.toThrow();});@@ -258,6 +373,7 @@ describe('useScrollToUserMessage', () => {dataSourceLength: 6,isSecondLastMessageFromUser: true,scrollToIndex,+ spacerActive: true,}),);diff --git a/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.ts b/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.tsindex dc45ca87c27..07f5f328fe8 100644--- a/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.ts+++ b/src/features/Conversation/ChatList/hooks/useScrollToUserMessage.ts@@ -18,6 +18,13 @@ interface UseScrollToUserMessageOptions {scrollToIndex:| ((index: number, options?: { align?: 'start' | 'center' | 'end'; smooth?: boolean }) => void)| null;+ /**+ * Whether the conversation spacer is mounted and providing fill height.+ * When the spacer mounts after the initial scroll, a follow-up scroll is+ * fired so the user message lands at the correct position once the extra+ * height is available.+ */+ spacerActive: boolean;}/**@@ -25,6 +32,10 @@ interface UseScrollToUserMessageOptions {* Only triggers scroll when user sends a new message (detected by checking if* 2 new messages were added and the second-to-last is from user).*+ * Scrolls immediately on message send (works when content already fills the+ * viewport). If a conversation spacer mounts afterwards (adding fill height),+ * a follow-up scroll is fired so the user message lands correctly.+ ** This ensures that in group chat scenarios, when multiple agents are responding,* the view doesn't jump around as each agent starts speaking.*/@@ -32,9 +43,12 @@ export function useScrollToUserMessage({dataSourceLength,isSecondLastMessageFromUser,scrollToIndex,+ spacerActive,}: UseScrollToUserMessageOptions): void {const prevLengthRef = useRef(dataSourceLength);const timerIdsRef = useRef<number[]>([]);+ // Index of the user message that needs to be scrolled to, or null if no pending scroll+ const pendingScrollIndexRef = useRef<number | null>(null);const clearPendingPins = useCallback(() => {timerIdsRef.current.forEach((timerId) => {@@ -43,17 +57,9 @@ export function useScrollToUserMessage({timerIdsRef.current = [];}, []);- useEffect(() => {- return clearPendingPins;- }, [clearPendingPins]);-- useEffect(() => {- const newMessageCount = dataSourceLength - prevLengthRef.current;- prevLengthRef.current = dataSourceLength;-- // Only scroll when user sends a new message (2 messages added: user + assistant pair)- if (newMessageCount === 2 && isSecondLastMessageFromUser && scrollToIndex) {- const userMessageIndex = dataSourceLength - 2;+ const executeScroll = useCallback(+ (userMessageIndex: number) => {+ if (!scrollToIndex) return;clearPendingPins();@@ -67,6 +73,36 @@ export function useScrollToUserMessage({timerIdsRef.current.push(timerId);});+ },+ [clearPendingPins, scrollToIndex],+ );++ useEffect(() => {+ return clearPendingPins;+ }, [clearPendingPins]);++ // Detect when user sends a new message and scroll immediately+ useEffect(() => {+ const newMessageCount = dataSourceLength - prevLengthRef.current;+ prevLengthRef.current = dataSourceLength;++ // Only scroll when user sends a new message (2 messages added: user + assistant pair)+ if (newMessageCount === 2 && isSecondLastMessageFromUser && scrollToIndex) {+ const userMessageIndex = dataSourceLength - 2;++ // Always scroll immediately β works when content already fills the viewport.+ // Also store the index so a follow-up scroll can fire once the spacer mounts.+ pendingScrollIndexRef.current = userMessageIndex;+ executeScroll(userMessageIndex);+ }+ }, [dataSourceLength, isSecondLastMessageFromUser, scrollToIndex, executeScroll]);++ // Re-scroll when spacer mounts (provides the extra fill height)+ useEffect(() => {+ if (spacerActive && pendingScrollIndexRef.current !== null) {+ const index = pendingScrollIndexRef.current;+ pendingScrollIndexRef.current = null;+ executeScroll(index);}- }, [clearPendingPins, dataSourceLength, isSecondLastMessageFromUser, scrollToIndex]);+ }, [spacerActive, executeScroll]);}