2025-12-07 19:32:53 +02:00
|
|
|
import React from 'react';
|
2026-01-03 13:39:59 +02:00
|
|
|
import type { Part } from '@opencode-ai/sdk/v2';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
import { MessageFreshnessDetector } from '@/lib/messageFreshness';
|
2026-03-12 23:45:45 +02:00
|
|
|
import { createScrollSpy } from '@/components/chat/lib/scroll/scrollSpy';
|
|
|
|
|
import {
|
|
|
|
|
isNearBottom,
|
|
|
|
|
normalizeWheelDelta,
|
|
|
|
|
shouldPauseAutoScrollOnWheel,
|
|
|
|
|
} from '@/components/chat/lib/scroll/scrollIntent';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
import { useScrollEngine } from './useScrollEngine';
|
|
|
|
|
|
|
|
|
|
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
|
|
|
|
|
|
|
|
export type ContentChangeReason = 'text' | 'structural' | 'permission';
|
|
|
|
|
|
|
|
|
|
interface ChatMessageRecord {
|
|
|
|
|
info: Record<string, unknown>;
|
|
|
|
|
parts: Part[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface SessionMemoryState {
|
|
|
|
|
viewportAnchor: number;
|
|
|
|
|
isStreaming: boolean;
|
|
|
|
|
lastAccessedAt: number;
|
|
|
|
|
backgroundMessageCount: number;
|
|
|
|
|
totalAvailableMessages?: number;
|
|
|
|
|
hasMoreAbove?: boolean;
|
|
|
|
|
streamStartTime?: number;
|
|
|
|
|
isZombie?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface UseChatScrollManagerOptions {
|
|
|
|
|
currentSessionId: string | null;
|
|
|
|
|
sessionMessages: ChatMessageRecord[];
|
|
|
|
|
sessionPermissions: unknown[];
|
|
|
|
|
streamingMessageId: string | null;
|
|
|
|
|
sessionMemoryState: Map<string, SessionMemoryState>;
|
|
|
|
|
updateViewportAnchor: (sessionId: string, anchor: number) => void;
|
|
|
|
|
isSyncing: boolean;
|
|
|
|
|
isMobile: boolean;
|
2026-03-12 23:45:45 +02:00
|
|
|
chatRenderMode?: 'sorted' | 'live';
|
2025-12-07 19:32:53 +02:00
|
|
|
messageStreamStates: Map<string, unknown>;
|
2026-03-12 23:45:45 +02:00
|
|
|
onActiveTurnChange?: (turnId: string | null) => void;
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AnimationHandlers {
|
|
|
|
|
onChunk: () => void;
|
|
|
|
|
onComplete: () => void;
|
|
|
|
|
onStreamingCandidate?: () => void;
|
|
|
|
|
onAnimationStart?: () => void;
|
|
|
|
|
onReservationCancelled?: () => void;
|
|
|
|
|
onReasoningBlock?: () => void;
|
|
|
|
|
onAnimatedHeightChange?: (height: number) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface UseChatScrollManagerResult {
|
|
|
|
|
scrollRef: React.RefObject<HTMLDivElement | null>;
|
|
|
|
|
handleMessageContentChange: (reason?: ContentChangeReason) => void;
|
|
|
|
|
getAnimationHandlers: (messageId: string) => AnimationHandlers;
|
|
|
|
|
showScrollButton: boolean;
|
2026-01-20 03:23:39 +02:00
|
|
|
scrollToBottom: (options?: { instant?: boolean; force?: boolean }) => void;
|
|
|
|
|
scrollToPosition: (position: number, options?: { instant?: boolean }) => void;
|
2026-03-12 23:45:45 +02:00
|
|
|
releasePinnedScroll: () => void;
|
2026-01-20 03:23:39 +02:00
|
|
|
isPinned: boolean;
|
2026-03-12 23:45:45 +02:00
|
|
|
isOverflowing: boolean;
|
|
|
|
|
isProgrammaticFollowActive: boolean;
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-19 02:48:56 +02:00
|
|
|
const PROGRAMMATIC_SCROLL_SUPPRESS_MS = 200;
|
2026-01-19 15:38:15 +02:00
|
|
|
const DIRECT_SCROLL_INTENT_WINDOW_MS = 250;
|
2026-01-20 03:23:39 +02:00
|
|
|
// Threshold for re-pinning: 10% of container height (matches bottom spacer)
|
|
|
|
|
const PIN_THRESHOLD_RATIO = 0.10;
|
2026-03-20 01:01:03 +02:00
|
|
|
const VIEWPORT_ANCHOR_MIN_UPDATE_MS = 150;
|
2026-01-19 02:48:56 +02:00
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
export const useChatScrollManager = ({
|
|
|
|
|
currentSessionId,
|
|
|
|
|
sessionMessages,
|
2026-02-17 18:25:03 +02:00
|
|
|
streamingMessageId,
|
2025-12-07 19:32:53 +02:00
|
|
|
updateViewportAnchor,
|
|
|
|
|
isSyncing,
|
|
|
|
|
isMobile,
|
2026-03-12 23:45:45 +02:00
|
|
|
onActiveTurnChange,
|
2025-12-07 19:32:53 +02:00
|
|
|
}: UseChatScrollManagerOptions): UseChatScrollManagerResult => {
|
|
|
|
|
const scrollRef = React.useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const scrollEngine = useScrollEngine({ containerRef: scrollRef, isMobile });
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const getPinThreshold = React.useCallback(() => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container || container.clientHeight <= 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
const raw = container.clientHeight * PIN_THRESHOLD_RATIO;
|
|
|
|
|
return Math.max(24, Math.min(200, raw));
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-03-12 23:45:45 +02:00
|
|
|
const getAutoFollowThreshold = React.useCallback(() => {
|
|
|
|
|
return getPinThreshold();
|
2026-03-20 01:01:03 +02:00
|
|
|
}, [getPinThreshold]);
|
2026-03-12 23:45:45 +02:00
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
const [showScrollButton, setShowScrollButton] = React.useState(false);
|
2026-01-20 03:23:39 +02:00
|
|
|
const [isPinned, setIsPinned] = React.useState(true);
|
2026-03-12 23:45:45 +02:00
|
|
|
const [isOverflowing, setIsOverflowing] = React.useState(false);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
const lastSessionIdRef = React.useRef<string | null>(null);
|
2026-01-19 02:48:56 +02:00
|
|
|
const suppressUserScrollUntilRef = React.useRef<number>(0);
|
2026-01-19 15:38:15 +02:00
|
|
|
const lastDirectScrollIntentAtRef = React.useRef<number>(0);
|
2026-01-20 03:23:39 +02:00
|
|
|
const isPinnedRef = React.useRef(true);
|
|
|
|
|
const lastScrollTopRef = React.useRef<number>(0);
|
2026-03-12 23:45:45 +02:00
|
|
|
const touchLastYRef = React.useRef<number | null>(null);
|
2026-03-20 01:01:03 +02:00
|
|
|
const pinnedSyncRafRef = React.useRef<number | null>(null);
|
|
|
|
|
const viewportAnchorTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
|
const pendingViewportAnchorRef = React.useRef<{ sessionId: string; anchor: number } | null>(null);
|
|
|
|
|
const lastViewportAnchorRef = React.useRef<{ sessionId: string; anchor: number } | null>(null);
|
|
|
|
|
const lastViewportAnchorWriteAtRef = React.useRef<number>(0);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const markProgrammaticScroll = React.useCallback(() => {
|
|
|
|
|
suppressUserScrollUntilRef.current = Date.now() + PROGRAMMATIC_SCROLL_SUPPRESS_MS;
|
2025-12-07 19:32:53 +02:00
|
|
|
}, []);
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const getDistanceFromBottom = React.useCallback(() => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) return 0;
|
|
|
|
|
return container.scrollHeight - container.scrollTop - container.clientHeight;
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const updatePinnedState = React.useCallback((newPinned: boolean) => {
|
|
|
|
|
if (isPinnedRef.current !== newPinned) {
|
|
|
|
|
isPinnedRef.current = newPinned;
|
|
|
|
|
setIsPinned(newPinned);
|
|
|
|
|
}
|
2026-01-19 16:23:27 +02:00
|
|
|
}, []);
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const scrollToBottomInternal = React.useCallback((options?: { instant?: boolean; followBottom?: boolean }) => {
|
2025-12-07 19:32:53 +02:00
|
|
|
const container = scrollRef.current;
|
2026-01-20 03:23:39 +02:00
|
|
|
if (!container) return;
|
2026-01-19 02:48:56 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const bottom = container.scrollHeight - container.clientHeight;
|
|
|
|
|
markProgrammaticScroll();
|
|
|
|
|
scrollEngine.scrollToPosition(Math.max(0, bottom), options);
|
|
|
|
|
}, [markProgrammaticScroll, scrollEngine]);
|
2026-01-18 18:00:19 +02:00
|
|
|
|
2026-02-17 18:25:03 +02:00
|
|
|
const scrollPinnedToBottom = React.useCallback(() => {
|
|
|
|
|
if (streamingMessageId) {
|
|
|
|
|
scrollToBottomInternal({ followBottom: true });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scrollToBottomInternal({ instant: true });
|
|
|
|
|
}, [scrollToBottomInternal, streamingMessageId]);
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
const updateScrollButtonVisibility = React.useCallback(() => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) {
|
|
|
|
|
setShowScrollButton(false);
|
2026-03-12 23:45:45 +02:00
|
|
|
setIsOverflowing(false);
|
2025-12-07 19:32:53 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hasScrollableContent = container.scrollHeight > container.clientHeight;
|
2026-03-12 23:45:45 +02:00
|
|
|
setIsOverflowing(hasScrollableContent);
|
2025-12-07 19:32:53 +02:00
|
|
|
if (!hasScrollableContent) {
|
|
|
|
|
setShowScrollButton(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Show scroll button when scrolled above the 10vh threshold
|
|
|
|
|
const distanceFromBottom = getDistanceFromBottom();
|
2026-03-12 23:45:45 +02:00
|
|
|
setShowScrollButton(!isNearBottom(distanceFromBottom, getPinThreshold()));
|
2026-01-20 03:23:39 +02:00
|
|
|
}, [getDistanceFromBottom, getPinThreshold]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-03-20 01:01:03 +02:00
|
|
|
const syncPinnedStateAndIndicators = React.useCallback(() => {
|
|
|
|
|
pinnedSyncRafRef.current = null;
|
|
|
|
|
updateScrollButtonVisibility();
|
|
|
|
|
if (!isPinnedRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const distanceFromBottom = getDistanceFromBottom();
|
|
|
|
|
if (distanceFromBottom > getAutoFollowThreshold()) {
|
|
|
|
|
scrollPinnedToBottom();
|
|
|
|
|
}
|
|
|
|
|
}, [getAutoFollowThreshold, getDistanceFromBottom, scrollPinnedToBottom, updateScrollButtonVisibility]);
|
|
|
|
|
|
|
|
|
|
const schedulePinnedStateAndIndicators = React.useCallback(() => {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
syncPinnedStateAndIndicators();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (pinnedSyncRafRef.current !== null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pinnedSyncRafRef.current = window.requestAnimationFrame(() => {
|
|
|
|
|
syncPinnedStateAndIndicators();
|
|
|
|
|
});
|
|
|
|
|
}, [syncPinnedStateAndIndicators]);
|
|
|
|
|
|
|
|
|
|
const flushViewportAnchor = React.useCallback(() => {
|
|
|
|
|
if (viewportAnchorTimerRef.current !== null) {
|
|
|
|
|
clearTimeout(viewportAnchorTimerRef.current);
|
|
|
|
|
viewportAnchorTimerRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pending = pendingViewportAnchorRef.current;
|
|
|
|
|
if (!pending) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const lastPersisted = lastViewportAnchorRef.current;
|
|
|
|
|
if (lastPersisted && lastPersisted.sessionId === pending.sessionId && lastPersisted.anchor === pending.anchor) {
|
|
|
|
|
pendingViewportAnchorRef.current = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateViewportAnchor(pending.sessionId, pending.anchor);
|
|
|
|
|
lastViewportAnchorRef.current = pending;
|
|
|
|
|
pendingViewportAnchorRef.current = null;
|
|
|
|
|
lastViewportAnchorWriteAtRef.current = Date.now();
|
|
|
|
|
}, [updateViewportAnchor]);
|
|
|
|
|
|
|
|
|
|
const queueViewportAnchor = React.useCallback((sessionId: string, anchor: number) => {
|
|
|
|
|
const lastPersisted = lastViewportAnchorRef.current;
|
|
|
|
|
if (lastPersisted && lastPersisted.sessionId === sessionId && lastPersisted.anchor === anchor) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pendingViewportAnchorRef.current = { sessionId, anchor };
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const elapsed = now - lastViewportAnchorWriteAtRef.current;
|
|
|
|
|
if (elapsed >= VIEWPORT_ANCHOR_MIN_UPDATE_MS) {
|
|
|
|
|
flushViewportAnchor();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (viewportAnchorTimerRef.current !== null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
viewportAnchorTimerRef.current = setTimeout(() => {
|
|
|
|
|
viewportAnchorTimerRef.current = null;
|
|
|
|
|
flushViewportAnchor();
|
|
|
|
|
}, VIEWPORT_ANCHOR_MIN_UPDATE_MS - elapsed);
|
|
|
|
|
}, [flushViewportAnchor]);
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const scrollToPosition = React.useCallback((position: number, options?: { instant?: boolean }) => {
|
2025-12-07 19:32:53 +02:00
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) return;
|
|
|
|
|
|
2026-01-19 02:48:56 +02:00
|
|
|
markProgrammaticScroll();
|
2026-01-20 03:23:39 +02:00
|
|
|
scrollEngine.scrollToPosition(Math.max(0, position), options);
|
|
|
|
|
}, [markProgrammaticScroll, scrollEngine]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
const scrollToBottom = React.useCallback((options?: { instant?: boolean; force?: boolean }) => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) return;
|
2026-01-19 02:48:56 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Re-pin when explicitly scrolling to bottom
|
|
|
|
|
updatePinnedState(true);
|
2026-01-19 02:48:56 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
scrollToBottomInternal(options);
|
|
|
|
|
setShowScrollButton(false);
|
|
|
|
|
}, [scrollToBottomInternal, updatePinnedState]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-03-12 23:45:45 +02:00
|
|
|
const releasePinnedScroll = React.useCallback(() => {
|
|
|
|
|
scrollEngine.cancelFollow();
|
|
|
|
|
updatePinnedState(false);
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
|
|
|
|
}, [schedulePinnedStateAndIndicators, scrollEngine, updatePinnedState]);
|
2026-03-12 23:45:45 +02:00
|
|
|
|
2025-12-13 16:34:17 +02:00
|
|
|
const handleScrollEvent = React.useCallback((event?: Event) => {
|
2025-12-07 19:32:53 +02:00
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container || !currentSessionId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 15:38:15 +02:00
|
|
|
const now = Date.now();
|
2026-01-20 03:23:39 +02:00
|
|
|
const isProgrammatic = now < suppressUserScrollUntilRef.current;
|
2026-01-19 15:38:15 +02:00
|
|
|
const hasDirectIntent = now - lastDirectScrollIntentAtRef.current <= DIRECT_SCROLL_INTENT_WINDOW_MS;
|
2026-01-19 02:48:56 +02:00
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
scrollEngine.handleScroll();
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Handle pin/unpin logic
|
|
|
|
|
const currentScrollTop = container.scrollTop;
|
2026-03-12 23:45:45 +02:00
|
|
|
const scrollingUp = currentScrollTop < lastScrollTopRef.current;
|
|
|
|
|
|
2026-03-17 13:18:54 +02:00
|
|
|
// Unpin requires strict user intent check
|
|
|
|
|
if (event?.isTrusted && !isProgrammatic && hasDirectIntent) {
|
|
|
|
|
if (scrollingUp && isPinnedRef.current) {
|
2026-01-20 03:23:39 +02:00
|
|
|
updatePinnedState(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 13:18:54 +02:00
|
|
|
// Re-pin at bottom should always work (even momentum scroll)
|
|
|
|
|
if (!isPinnedRef.current) {
|
|
|
|
|
const distanceFromBottom = getDistanceFromBottom();
|
2026-03-20 01:01:03 +02:00
|
|
|
if (distanceFromBottom <= getPinThreshold()) {
|
2026-01-20 03:23:39 +02:00
|
|
|
updatePinnedState(true);
|
|
|
|
|
}
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
lastScrollTopRef.current = currentScrollTop;
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
const { scrollTop, scrollHeight, clientHeight } = container;
|
|
|
|
|
const position = (scrollTop + clientHeight / 2) / Math.max(scrollHeight, 1);
|
|
|
|
|
const estimatedIndex = Math.floor(position * sessionMessages.length);
|
2026-03-20 01:01:03 +02:00
|
|
|
queueViewportAnchor(currentSessionId, estimatedIndex);
|
2025-12-07 19:32:53 +02:00
|
|
|
}, [
|
|
|
|
|
currentSessionId,
|
2026-01-20 03:23:39 +02:00
|
|
|
getDistanceFromBottom,
|
|
|
|
|
getPinThreshold,
|
2026-03-20 01:01:03 +02:00
|
|
|
queueViewportAnchor,
|
|
|
|
|
schedulePinnedStateAndIndicators,
|
2025-12-07 19:32:53 +02:00
|
|
|
scrollEngine,
|
|
|
|
|
sessionMessages.length,
|
2026-01-20 03:23:39 +02:00
|
|
|
updatePinnedState,
|
2025-12-07 19:32:53 +02:00
|
|
|
]);
|
|
|
|
|
|
2026-03-12 23:45:45 +02:00
|
|
|
const handleWheelIntent = React.useCallback((event: WheelEvent) => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const delta = normalizeWheelDelta({
|
|
|
|
|
deltaY: event.deltaY,
|
|
|
|
|
deltaMode: event.deltaMode,
|
|
|
|
|
rootHeight: container.clientHeight,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (isPinnedRef.current && shouldPauseAutoScrollOnWheel({
|
|
|
|
|
root: container,
|
|
|
|
|
target: event.target,
|
|
|
|
|
delta,
|
|
|
|
|
})) {
|
|
|
|
|
scrollEngine.cancelFollow();
|
|
|
|
|
updatePinnedState(false);
|
|
|
|
|
}
|
|
|
|
|
}, [scrollEngine, updatePinnedState]);
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) return;
|
|
|
|
|
|
2026-01-19 15:38:15 +02:00
|
|
|
const markDirectIntent = () => {
|
|
|
|
|
lastDirectScrollIntentAtRef.current = Date.now();
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-12 23:45:45 +02:00
|
|
|
const handleTouchStartIntent = (event: TouchEvent) => {
|
|
|
|
|
markDirectIntent();
|
|
|
|
|
const touch = event.touches.item(0);
|
|
|
|
|
touchLastYRef.current = touch ? touch.clientY : null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleTouchMoveIntent = (event: TouchEvent) => {
|
|
|
|
|
markDirectIntent();
|
|
|
|
|
|
|
|
|
|
const touch = event.touches.item(0);
|
|
|
|
|
if (!touch) {
|
|
|
|
|
touchLastYRef.current = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const previousY = touchLastYRef.current;
|
|
|
|
|
touchLastYRef.current = touch.clientY;
|
|
|
|
|
if (previousY === null || !isPinnedRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fingerDelta = touch.clientY - previousY;
|
|
|
|
|
if (Math.abs(fingerDelta) < 2) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const syntheticWheelDelta = -fingerDelta;
|
|
|
|
|
if (syntheticWheelDelta >= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldPauseAutoScrollOnWheel({
|
|
|
|
|
root: container,
|
|
|
|
|
target: event.target,
|
|
|
|
|
delta: syntheticWheelDelta,
|
|
|
|
|
})) {
|
|
|
|
|
scrollEngine.cancelFollow();
|
|
|
|
|
updatePinnedState(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleTouchEndIntent = () => {
|
|
|
|
|
touchLastYRef.current = null;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-13 16:34:17 +02:00
|
|
|
container.addEventListener('scroll', handleScrollEvent as EventListener, { passive: true });
|
2026-03-12 23:45:45 +02:00
|
|
|
container.addEventListener('touchstart', handleTouchStartIntent as EventListener, { passive: true });
|
|
|
|
|
container.addEventListener('touchmove', handleTouchMoveIntent as EventListener, { passive: true });
|
|
|
|
|
container.addEventListener('touchend', handleTouchEndIntent as EventListener, { passive: true });
|
|
|
|
|
container.addEventListener('touchcancel', handleTouchEndIntent as EventListener, { passive: true });
|
|
|
|
|
container.addEventListener('wheel', handleWheelIntent as EventListener, { passive: true });
|
2026-01-19 15:38:15 +02:00
|
|
|
container.addEventListener('wheel', markDirectIntent as EventListener, { passive: true });
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
return () => {
|
2025-12-13 16:34:17 +02:00
|
|
|
container.removeEventListener('scroll', handleScrollEvent as EventListener);
|
2026-03-12 23:45:45 +02:00
|
|
|
container.removeEventListener('touchstart', handleTouchStartIntent as EventListener);
|
|
|
|
|
container.removeEventListener('touchmove', handleTouchMoveIntent as EventListener);
|
|
|
|
|
container.removeEventListener('touchend', handleTouchEndIntent as EventListener);
|
|
|
|
|
container.removeEventListener('touchcancel', handleTouchEndIntent as EventListener);
|
|
|
|
|
container.removeEventListener('wheel', handleWheelIntent as EventListener);
|
2026-01-19 15:38:15 +02:00
|
|
|
container.removeEventListener('wheel', markDirectIntent as EventListener);
|
2025-12-07 19:32:53 +02:00
|
|
|
};
|
2026-03-12 23:45:45 +02:00
|
|
|
}, [handleScrollEvent, handleWheelIntent, scrollEngine, updatePinnedState]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Session switch - always start pinned at bottom
|
2025-12-07 19:32:53 +02:00
|
|
|
useIsomorphicLayoutEffect(() => {
|
2026-01-19 02:48:56 +02:00
|
|
|
if (!currentSessionId || currentSessionId === lastSessionIdRef.current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-19 02:48:56 +02:00
|
|
|
lastSessionIdRef.current = currentSessionId;
|
|
|
|
|
MessageFreshnessDetector.getInstance().recordSessionStart(currentSessionId);
|
2026-03-20 01:01:03 +02:00
|
|
|
flushViewportAnchor();
|
|
|
|
|
pendingViewportAnchorRef.current = null;
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Always start pinned at bottom on session switch
|
|
|
|
|
updatePinnedState(true);
|
2026-01-19 02:48:56 +02:00
|
|
|
setShowScrollButton(false);
|
2026-01-18 18:00:19 +02:00
|
|
|
|
|
|
|
|
const container = scrollRef.current;
|
2026-01-20 03:23:39 +02:00
|
|
|
if (container) {
|
|
|
|
|
markProgrammaticScroll();
|
|
|
|
|
scrollToBottomInternal({ instant: true });
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
2026-03-20 01:01:03 +02:00
|
|
|
}, [currentSessionId, flushViewportAnchor, markProgrammaticScroll, scrollToBottomInternal, updatePinnedState]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Maintain pin-to-bottom when content changes
|
|
|
|
|
React.useEffect(() => {
|
2026-03-20 01:01:03 +02:00
|
|
|
if (isSyncing) {
|
|
|
|
|
return;
|
2025-12-07 19:32:53 +02:00
|
|
|
}
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
|
|
|
|
}, [isSyncing, schedulePinnedStateAndIndicators, sessionMessages.length]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Use ResizeObserver to detect content changes and maintain pin
|
2025-12-07 19:32:53 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container || typeof ResizeObserver === 'undefined') return;
|
|
|
|
|
|
|
|
|
|
const observer = new ResizeObserver(() => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-07 19:32:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
observer.observe(container);
|
|
|
|
|
|
2026-01-20 03:23:39 +02:00
|
|
|
// Also observe children for content changes
|
|
|
|
|
const childObserver = new MutationObserver(() => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2026-01-20 03:23:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
childObserver.observe(container, { childList: true, subtree: true });
|
|
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
return () => {
|
|
|
|
|
observer.disconnect();
|
2026-01-20 03:23:39 +02:00
|
|
|
childObserver.disconnect();
|
2025-12-07 19:32:53 +02:00
|
|
|
};
|
2026-03-20 01:01:03 +02:00
|
|
|
}, [schedulePinnedStateAndIndicators]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2025-12-18 19:02:42 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (typeof window === 'undefined') {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-18 19:02:42 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rafId = window.requestAnimationFrame(() => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-18 19:02:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
window.cancelAnimationFrame(rafId);
|
|
|
|
|
};
|
2026-03-20 01:01:03 +02:00
|
|
|
}, [currentSessionId, schedulePinnedStateAndIndicators, sessionMessages.length]);
|
2025-12-18 19:02:42 +02:00
|
|
|
|
2025-12-07 19:32:53 +02:00
|
|
|
const animationHandlersRef = React.useRef<Map<string, AnimationHandlers>>(new Map());
|
|
|
|
|
|
|
|
|
|
const handleMessageContentChange = React.useCallback(() => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
|
|
|
|
}, [schedulePinnedStateAndIndicators]);
|
2026-02-11 19:28:22 +02:00
|
|
|
|
|
|
|
|
const getAnimationHandlers = React.useCallback((messageId: string): AnimationHandlers => {
|
2025-12-07 19:32:53 +02:00
|
|
|
const existing = animationHandlersRef.current.get(messageId);
|
|
|
|
|
if (existing) {
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handlers: AnimationHandlers = {
|
|
|
|
|
onChunk: () => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-07 19:32:53 +02:00
|
|
|
},
|
|
|
|
|
onComplete: () => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-07 19:32:53 +02:00
|
|
|
},
|
2026-01-20 03:23:39 +02:00
|
|
|
onStreamingCandidate: () => {},
|
|
|
|
|
onAnimationStart: () => {},
|
2025-12-07 19:32:53 +02:00
|
|
|
onAnimatedHeightChange: () => {
|
2026-03-20 01:01:03 +02:00
|
|
|
schedulePinnedStateAndIndicators();
|
2025-12-07 19:32:53 +02:00
|
|
|
},
|
2026-01-20 03:23:39 +02:00
|
|
|
onReservationCancelled: () => {},
|
|
|
|
|
onReasoningBlock: () => {},
|
2025-12-07 19:32:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
animationHandlersRef.current.set(messageId, handlers);
|
|
|
|
|
return handlers;
|
2026-03-20 01:01:03 +02:00
|
|
|
}, [schedulePinnedStateAndIndicators]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
return () => {
|
|
|
|
|
if (pinnedSyncRafRef.current !== null && typeof window !== 'undefined') {
|
|
|
|
|
window.cancelAnimationFrame(pinnedSyncRafRef.current);
|
|
|
|
|
pinnedSyncRafRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flushViewportAnchor();
|
|
|
|
|
if (viewportAnchorTimerRef.current !== null) {
|
|
|
|
|
clearTimeout(viewportAnchorTimerRef.current);
|
|
|
|
|
viewportAnchorTimerRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [flushViewportAnchor]);
|
2026-03-12 23:45:45 +02:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!onActiveTurnChange) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const container = scrollRef.current;
|
|
|
|
|
if (!container) {
|
|
|
|
|
onActiveTurnChange(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const spy = createScrollSpy({
|
|
|
|
|
onActive: (turnId) => {
|
|
|
|
|
onActiveTurnChange(turnId);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
spy.setContainer(container);
|
|
|
|
|
|
2026-03-20 01:01:03 +02:00
|
|
|
const elementByTurnId = new Map<string, HTMLElement>();
|
|
|
|
|
|
|
|
|
|
const registerTurnNode = (node: HTMLElement): boolean => {
|
|
|
|
|
const turnId = node.dataset.turnId;
|
|
|
|
|
if (!turnId) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
elementByTurnId.set(turnId, node);
|
|
|
|
|
spy.register(node, turnId);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const unregisterTurnNode = (node: HTMLElement): boolean => {
|
|
|
|
|
const turnId = node.dataset.turnId;
|
|
|
|
|
if (!turnId) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (elementByTurnId.get(turnId) !== node) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
elementByTurnId.delete(turnId);
|
|
|
|
|
spy.unregister(turnId);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const collectTurnNodes = (node: Node): HTMLElement[] => {
|
|
|
|
|
if (!(node instanceof HTMLElement)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const collected: HTMLElement[] = [];
|
|
|
|
|
if (node.matches('[data-turn-id]')) {
|
|
|
|
|
collected.push(node);
|
|
|
|
|
}
|
|
|
|
|
node.querySelectorAll<HTMLElement>('[data-turn-id]').forEach((turnNode) => {
|
|
|
|
|
collected.push(turnNode);
|
2026-03-12 23:45:45 +02:00
|
|
|
});
|
2026-03-20 01:01:03 +02:00
|
|
|
return collected;
|
2026-03-12 23:45:45 +02:00
|
|
|
};
|
|
|
|
|
|
2026-03-20 01:01:03 +02:00
|
|
|
container.querySelectorAll<HTMLElement>('[data-turn-id]').forEach((node) => {
|
|
|
|
|
registerTurnNode(node);
|
|
|
|
|
});
|
|
|
|
|
spy.markDirty();
|
|
|
|
|
|
|
|
|
|
const mutationObserver = new MutationObserver((records) => {
|
|
|
|
|
let changed = false;
|
|
|
|
|
|
|
|
|
|
records.forEach((record) => {
|
|
|
|
|
record.removedNodes.forEach((node) => {
|
|
|
|
|
collectTurnNodes(node).forEach((turnNode) => {
|
|
|
|
|
if (unregisterTurnNode(turnNode)) {
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
record.addedNodes.forEach((node) => {
|
|
|
|
|
collectTurnNodes(node).forEach((turnNode) => {
|
|
|
|
|
if (registerTurnNode(turnNode)) {
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-03-12 23:45:45 +02:00
|
|
|
|
2026-03-20 01:01:03 +02:00
|
|
|
if (changed) {
|
|
|
|
|
spy.markDirty();
|
|
|
|
|
}
|
2026-03-12 23:45:45 +02:00
|
|
|
});
|
|
|
|
|
mutationObserver.observe(container, { subtree: true, childList: true });
|
|
|
|
|
|
|
|
|
|
const handleScroll = () => {
|
|
|
|
|
spy.onScroll();
|
|
|
|
|
};
|
|
|
|
|
container.addEventListener('scroll', handleScroll, { passive: true });
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
container.removeEventListener('scroll', handleScroll);
|
|
|
|
|
mutationObserver.disconnect();
|
|
|
|
|
spy.destroy();
|
|
|
|
|
onActiveTurnChange(null);
|
|
|
|
|
};
|
|
|
|
|
}, [currentSessionId, onActiveTurnChange, scrollRef, sessionMessages.length]);
|
2025-12-07 19:32:53 +02:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
scrollRef,
|
|
|
|
|
handleMessageContentChange,
|
|
|
|
|
getAnimationHandlers,
|
|
|
|
|
showScrollButton,
|
|
|
|
|
scrollToBottom,
|
2026-01-20 03:23:39 +02:00
|
|
|
scrollToPosition,
|
2026-03-12 23:45:45 +02:00
|
|
|
releasePinnedScroll,
|
2026-01-20 03:23:39 +02:00
|
|
|
isPinned,
|
2026-03-12 23:45:45 +02:00
|
|
|
isOverflowing,
|
|
|
|
|
isProgrammaticFollowActive: scrollEngine.isFollowingBottom,
|
2025-12-07 19:32:53 +02:00
|
|
|
};
|
|
|
|
|
};
|