{"version":3,"file":"useScrollerViewport.mjs","names":[],"sources":["../../../../../../src/components/MessageList/hooks/VirtualizedMessageList/useScrollerViewport.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\n// Virtuoso's at-bottom and at-top thresholds are absolute pixels, so the threshold is a fraction of\n// the viewport height, clamped between Virtuoso's own default (4px) and the value for a\n// full-height list (100px).\nconst THRESHOLD_RATIO = 0.1;\nconst MIN_THRESHOLD = 4;\nconst MAX_THRESHOLD = 100;\n\nconst clamp = (value: number, min: number, max: number) =>\n  Math.min(Math.max(value, min), max);\n\nexport type ScrollerViewport = {\n  /** Threshold proportional to the viewport, for Virtuoso's at-bottom / at-top detection. */\n  threshold: number;\n  /** Callback for Virtuoso's `scrollerRef` prop. */\n  handleScrollerRef: (element: HTMLElement | Window | null) => void;\n  /**\n   * Schedules a measurement on the next animation frame. Content can grow (e.g. an image landing\n   * late) without a resize or scroll event, which moves the list away from its true bottom.\n   */\n  remeasure: () => void;\n  /**\n   * Whether the list is within `threshold` of its bottom. It shares Virtuoso's threshold so that\n   * gating mark-read on it never disagrees with the jump-to-latest control. Unlike Virtuoso's own\n   * flag, which updates only when the at-bottom state flips, it is re-measured on every content or\n   * viewport change.\n   */\n  isAtTrueBottom: boolean;\n};\n\n/**\n * Tracks the scroll container's height and whether it sits at its true bottom, deriving a\n * threshold proportional to the viewport.\n */\nexport const useScrollerViewport = (): ScrollerViewport => {\n  // State, not a ref: the scroller arrives after mount and the effect below must re-run then.\n  const [scrollTarget, setScrollTarget] = useState<HTMLElement | Window | null>(null);\n  // Until measured, assumes a full-height list.\n  const [viewportHeight, setViewportHeight] = useState(MAX_THRESHOLD / THRESHOLD_RATIO);\n  const [isAtTrueBottom, setIsAtTrueBottom] = useState(true);\n  const frameRef = useRef<number | null>(null);\n  // Assigned by the effect below; held in a ref so `remeasure` can stay stable for callers.\n  const measureRef = useRef<() => void>(() => undefined);\n\n  const remeasure = useCallback(() => {\n    if (frameRef.current !== null) return;\n    frameRef.current = requestAnimationFrame(() => {\n      frameRef.current = null;\n      measureRef.current();\n    });\n  }, []);\n\n  const handleScrollerRef = useCallback((element: HTMLElement | Window | null) => {\n    setScrollTarget(element);\n  }, []);\n\n  useEffect(() => {\n    if (!scrollTarget) return;\n\n    // With `useWindowScroll` Virtuoso hands back the window: scroll events arrive on it, but the\n    // sizes come from the document's scrolling element.\n    const isWindow = !(scrollTarget instanceof HTMLElement);\n    const measured = isWindow\n      ? ((document.scrollingElement ?? document.documentElement) as HTMLElement | null)\n      : scrollTarget;\n    if (!measured) return;\n\n    const measure = () => {\n      const height = measured.clientHeight;\n      setViewportHeight(height);\n      // Uses the height just measured, not state, so it agrees with the reported threshold.\n      const threshold = clamp(height * THRESHOLD_RATIO, MIN_THRESHOLD, MAX_THRESHOLD);\n      setIsAtTrueBottom(\n        measured.scrollHeight - measured.scrollTop - measured.clientHeight <= threshold,\n      );\n    };\n\n    measureRef.current = measure;\n    const scheduleMeasure = remeasure;\n\n    scrollTarget.addEventListener('scroll', scheduleMeasure, { passive: true });\n\n    // The panel can resize without a scroll; without ResizeObserver (jsdom) only scrolls measure.\n    let resizeObserver: ResizeObserver | undefined;\n    if (typeof ResizeObserver !== 'undefined') {\n      resizeObserver = new ResizeObserver(scheduleMeasure);\n      resizeObserver.observe(measured);\n    }\n    measure();\n\n    return () => {\n      scrollTarget.removeEventListener('scroll', scheduleMeasure);\n      resizeObserver?.disconnect();\n      if (frameRef.current !== null) cancelAnimationFrame(frameRef.current);\n      frameRef.current = null;\n      measureRef.current = () => undefined;\n    };\n  }, [remeasure, scrollTarget]);\n\n  return {\n    handleScrollerRef,\n    isAtTrueBottom,\n    remeasure,\n    threshold: clamp(viewportHeight * THRESHOLD_RATIO, MIN_THRESHOLD, MAX_THRESHOLD),\n  };\n};\n"],"mappings":";;AAKA,IAAM,kBAAkB;AACxB,IAAM,gBAAgB;AACtB,IAAM,gBAAgB;AAEtB,IAAM,SAAS,OAAe,KAAa,QACzC,KAAK,IAAI,K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