diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 90aee38189..1ab83a8a25 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -17,6 +17,8 @@ import { type DragEvent, type FormEvent, type KeyboardEvent, + type PointerEvent, + type TouchEvent, type WheelEvent, memo, type RefObject, @@ -404,7 +406,9 @@ export default function ChatView({ threadId }: ChatViewProps) { const messagesScrollRef = useRef(null); const shouldAutoScrollRef = useRef(true); const lastKnownScrollTopRef = useRef(0); - const lastKnownScrollHeightRef = useRef(0); + const isPointerScrollActiveRef = useRef(false); + const lastTouchClientYRef = useRef(null); + const pendingUserScrollUpIntentRef = useRef(false); const pendingAutoScrollFrameRef = useRef(null); const textareaRef = useRef(null); const composerCommandInputRef = useRef(null); @@ -1105,7 +1109,6 @@ export default function ChatView({ threadId }: ChatViewProps) { if (!scrollContainer) return; scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior }); lastKnownScrollTopRef.current = scrollContainer.scrollTop; - lastKnownScrollHeightRef.current = scrollContainer.scrollHeight; shouldAutoScrollRef.current = true; }, []); const requestAutoTailToBottom = useCallback(() => { @@ -1120,29 +1123,60 @@ export default function ChatView({ threadId }: ChatViewProps) { const scrollContainer = messagesScrollRef.current; if (!scrollContainer) return; const currentScrollTop = scrollContainer.scrollTop; - const currentScrollHeight = scrollContainer.scrollHeight; const atBottom = isScrollContainerAtBottom(scrollContainer); - if (atBottom) { + if (!shouldAutoScrollRef.current && atBottom) { shouldAutoScrollRef.current = true; - } else if (shouldAutoScrollRef.current) { - // While tailing, only relinquish control when the user scrolls upward. + pendingUserScrollUpIntentRef.current = false; + } else if (shouldAutoScrollRef.current && pendingUserScrollUpIntentRef.current) { const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1; - const contentHeightChanged = - Math.abs(currentScrollHeight - lastKnownScrollHeightRef.current) > 1; - if (scrolledUp && !contentHeightChanged) { + if (scrolledUp) { + shouldAutoScrollRef.current = false; + } + pendingUserScrollUpIntentRef.current = false; + } else if (shouldAutoScrollRef.current && isPointerScrollActiveRef.current) { + // Pointer-driven upward scroll means user takes control. + const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1; + if (scrolledUp) { shouldAutoScrollRef.current = false; } } lastKnownScrollTopRef.current = currentScrollTop; - lastKnownScrollHeightRef.current = currentScrollHeight; }, []); const onMessagesWheel = useCallback((event: WheelEvent) => { + // Wheel-up indicates user intent to move away from bottom. if (event.deltaY < 0) { - shouldAutoScrollRef.current = false; + pendingUserScrollUpIntentRef.current = true; } }, []); + const onMessagesPointerDown = useCallback((_event: PointerEvent) => { + isPointerScrollActiveRef.current = true; + }, []); + const onMessagesPointerUp = useCallback((_event: PointerEvent) => { + isPointerScrollActiveRef.current = false; + }, []); + const onMessagesPointerCancel = useCallback((_event: PointerEvent) => { + isPointerScrollActiveRef.current = false; + }, []); + const onMessagesTouchStart = useCallback((event: TouchEvent) => { + const touch = event.touches[0]; + if (!touch) return; + lastTouchClientYRef.current = touch.clientY; + }, []); + const onMessagesTouchMove = useCallback((event: TouchEvent) => { + const touch = event.touches[0]; + if (!touch) return; + const previousTouchY = lastTouchClientYRef.current; + if (previousTouchY !== null && touch.clientY > previousTouchY + 1) { + // Finger moved downward => user intends to scroll content upward. + pendingUserScrollUpIntentRef.current = true; + } + lastTouchClientYRef.current = touch.clientY; + }, []); + const onMessagesTouchEnd = useCallback((_event: TouchEvent) => { + lastTouchClientYRef.current = null; + }, []); useLayoutEffect(() => { if (!activeThread?.id) return; scrollMessagesToBottom(); @@ -2089,6 +2123,13 @@ export default function ChatView({ threadId }: ChatViewProps) { className="min-h-0 flex-1 overflow-y-auto px-5 py-4" onScroll={onMessagesScroll} onWheel={onMessagesWheel} + onPointerDown={onMessagesPointerDown} + onPointerUp={onMessagesPointerUp} + onPointerCancel={onMessagesPointerCancel} + onTouchStart={onMessagesTouchStart} + onTouchMove={onMessagesTouchMove} + onTouchEnd={onMessagesTouchEnd} + onTouchCancel={onMessagesTouchEnd} > 0}