import React from 'react'; import { Virtualizer } from '@tanstack/react-virtual'; interface UseVirtualizedScrollParams { virtualizer: Virtualizer; scrollToRow?: number; onScroll?: (props: { scrollDirection: 'forward' | 'backward'; scrollOffset: number; scrollUpdateWasRequested: boolean; }) => void; } interface UseVirtualizedScrollResult { /** Clears programmatic-scroll tracking so the next scroll event is treated as user-initiated. */ clearScrollTracking: () => void; } // Duration for smooth scroll animation to settle (ms) const SCROLL_ANIMATION_DURATION_MS = 300; /** * Custom hook to manage scroll behavior for virtualized log viewer * * Handles: * - Scroll offset monitoring and onScroll callback * - Scroll direction detection (forward/backward) * - Programmatic scroll tracking (user vs program initiated) * - ScrollToRow functionality with proper cleanup */ export const useVirtualizedScroll = ({ virtualizer, scrollToRow, onScroll, }: UseVirtualizedScrollParams): UseVirtualizedScrollResult => { const prevScrollOffset = React.useRef(0); const scrollToIndexRef = React.useRef(undefined); const clearScrollTracking = React.useCallback(() => { scrollToIndexRef.current = undefined; }, []); // Track scroll offset changes and trigger onScroll callback const scrollOffset = virtualizer.scrollOffset ?? 0; React.useEffect(() => { if (!onScroll) return; // Determine scroll direction const scrollDirection: 'forward' | 'backward' = scrollOffset >= prevScrollOffset.current ? 'forward' : 'backward'; // Match PatternFly LogViewer semantics: // scrollUpdateWasRequested is true when user manually scrolls (NOT programmatic) // This is opposite to what the name suggests, but matches PatternFly behavior const scrollUpdateWasRequested = scrollToIndexRef.current === undefined; prevScrollOffset.current = scrollOffset; onScroll({ scrollDirection, scrollOffset, scrollUpdateWasRequested, }); }, [scrollOffset, onScroll]); // Scroll to specific row when scrollToRow changes React.useEffect(() => { if (scrollToRow !== undefined && scrollToRow > 0) { const targetIndex = scrollToRow - 1; scrollToIndexRef.current = targetIndex; virtualizer.scrollToIndex(targetIndex, { align: 'center' }); // Clear the flag after scroll animation completes const timer = setTimeout(() => { scrollToIndexRef.current = undefined; }, SCROLL_ANIMATION_DURATION_MS); return () => clearTimeout(timer); } // Clear the flag immediately when scrollToRow becomes undefined/0 scrollToIndexRef.current = undefined; }, [scrollToRow, virtualizer]); return { clearScrollTracking }; };