{"version":3,"file":"InfiniteScroll.mjs","names":[],"sources":["../../../../src/components/InfiniteScrollPaginator/InfiniteScroll.tsx"],"sourcesContent":["import type { PropsWithChildren } from 'react';\nimport React, { useEffect, useRef, useState } from 'react';\nimport type { PaginatorProps } from '../../types/types';\nimport { DEFAULT_LOAD_PAGE_SCROLL_THRESHOLD } from '../../constants/limits';\n\n// Prevents Chrome hangups, see:\n// https://stackoverflow.com/questions/47524205/random-high-content-download-time-in-chrome/47684257#47684257\nconst mousewheelListener = (event: Event) => {\n  if (event instanceof WheelEvent && event.deltaY === 1) {\n    event.preventDefault();\n  }\n};\n\nexport type InfiniteScrollProps = PaginatorProps & {\n  className?: string;\n  element?: React.ElementType;\n  /** Rendered before `loader` and `children`. */\n  head?: React.ReactNode;\n  initialLoad?: boolean;\n  isLoading?: boolean;\n  listenToScroll?: (offset: number, reverseOffset: number, threshold: number) => void;\n  loader?: React.ReactNode;\n  useCapture?: boolean;\n};\n\n/**\n * Calls `loadPreviousPage` or `loadNextPage` when the parent scroll container comes within\n * `threshold` of its top or bottom. The `resize` listener on that container never fires for element\n * resizes (only the window emits `resize`), so growing content does not re-run the check.\n */\nexport const InfiniteScroll = (props: PropsWithChildren<InfiniteScrollProps>) => {\n  const {\n    children,\n    element: Component = 'div',\n    hasNextPage,\n    hasPreviousPage,\n    head,\n    initialLoad = true,\n    isLoading,\n    listenToScroll,\n    loader,\n    loadNextPage,\n    loadPreviousPage,\n    threshold = DEFAULT_LOAD_PAGE_SCROLL_THRESHOLD,\n    useCapture = false,\n    ...elementProps\n  } = props;\n\n  const [scrollComponent, setScrollComponent] = useState<HTMLElement | null>(null);\n  const previousOffset = useRef<number | undefined>(undefined);\n  const previousReverseOffset = useRef<number | undefined>(undefined);\n\n  const scrollListenerRef = useRef<() => void>(undefined);\n  scrollListenerRef.current = () => {\n    const element = scrollComponent;\n\n    if (!element || element.offsetParent === null) {\n      return;\n    }\n\n    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n    const parentElement = element.parentElement!;\n\n    const offset =\n      element.scrollHeight - parentElement.scrollTop - parentElement.clientHeight;\n    const reverseOffset = parentElement.scrollTop;\n\n    if (listenToScroll) {\n      listenToScroll(offset, reverseOffset, threshold);\n    }\n\n    if (isLoading) return;\n\n    if (\n      previousOffset.current === offset &&\n      previousReverseOffset.current === reverseOffset\n    )\n      return;\n    previousOffset.current = offset;\n    previousReverseOffset.current = reverseOffset;\n\n    // FIXME: this triggers loadMore call when a user types messages in thread and the scroll container expands\n    if (\n      reverseOffset < Number(threshold) &&\n      typeof loadPreviousPage === 'function' &&\n      hasPreviousPage\n    ) {\n      loadPreviousPage();\n    }\n\n    if (offset < Number(threshold) && typeof loadNextPage === 'function' && hasNextPage) {\n      loadNextPage();\n    }\n  };\n\n  useEffect(() => {\n    const scrollElement = scrollComponent?.parentNode;\n\n    if (!scrollElement) return;\n\n    const scrollListener = () => scrollListenerRef.current?.();\n\n    scrollElement.addEventListener('scroll', scrollListener, useCapture);\n    scrollElement.addEventListener('resize', scrollListener, useCapture);\n\n    // Deferred so a pending scroll-to-bottom from a layout effect is applied first; otherwise\n    // `scrollTop` is still 0 on mount, which falsely triggers `loadPreviousPage`.\n    const rafId = requestAnimationFrame(() => {\n      scrollListener();\n    });\n\n    return () => {\n      cancelAnimationFrame(rafId);\n      scrollElement.removeEventListener('scroll', scrollListener, useCapture);\n      scrollElement.removeEventListener('resize', scrollListener, useCapture);\n    };\n  }, [initialLoad, scrollComponent, useCapture]);\n\n  useEffect(() => {\n    const scrollElement = scrollComponent?.parentNode;\n\n    if (!scrollElement) return;\n\n    scrollElement.addEventListener('wheel', mousewheelListener, { passive: false });\n\n    return () => {\n      scrollElement.removeEventListener('wheel', mousewheelListener);\n    };\n  }, [scrollComponent]);\n\n  return (\n    <Component {...elementProps} ref={setScrollComponent}>\n      {head}\n      {loader}\n      {children}\n    </Component>\n  );\n};\n"],"mappings":";;;AAOA,IAAM,sBAAsB,UAAiB;CAC3C,IAAI,iBAAiB,cAAc,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