import { ComponentType, PropsWithChildren, Ref, RefAttributes, useCallback, useEffect, useLayoutEffect, useMemo, useRef, } from 'react'; import { EndlessListItemView } from './EndlessListItemView'; import { ItemComponentType } from './ItemComponentType'; import { PlaceholderComponentType } from './PlaceholderComponentType'; import { useEndlessList } from './useEndlessList'; import { Frame, useVisibleFrame } from './useVisibleFrame'; import { useVisibleItems } from './useVisibleItems'; import { mergeReferences } from '../internal/mergeReferences'; import { AnimationParameters, smoothScrollToCenter } from '../internal/smoothScrollToCenter'; import { useEvent } from '../internal/useEvent'; import { useScheduleOnNextRender } from '../internal/useScheduleOnNextRender'; import { useToggleEvent } from '../internal/useToggleEvent'; import type { KeysOfType } from '../internal/KeysOfType'; export type ContainerComponentProps = PropsWithChildren>; export type ItemKey = KeysOfType | ((value: T) => string); export type EndlessListProps = { ItemComponent: ItemComponentType; PlaceholderComponent: PlaceholderComponentType; ContainerComponent: ComponentType; initialItems: TItemType[]; items: TItemType[]; itemKey: ItemKey; triggerDistance: number; onTopReached?: () => void; onBottomReached?: () => void; compareItems: (first: TItemType, second: TItemType) => number; focusedItem?: TItemType; jumpAnimation?: AnimationParameters; canStickToBottom?: boolean; onVisibleFrameChange?: (frame: Frame) => void; containerReference?: Ref; }; const noop = () => { /** No operation */ }; const defaultAnimationParameters: AnimationParameters = { // Constant duration duration: () => 500, // Linear easing easing: (t) => t, }; export const EndlessList = ({ ItemComponent, initialItems, items, itemKey, triggerDistance, onTopReached, onBottomReached, compareItems, PlaceholderComponent, jumpAnimation = defaultAnimationParameters, focusedItem, ContainerComponent, canStickToBottom, onVisibleFrameChange, containerReference: propsContainerReference, }: EndlessListProps) => { const containerReference = useRef(null); const focusElementReference = useRef(null); const stickToBottomReached = useRef(false); const isScrolling = useRef(false); const visibleFrame = useRef({ begin: -1, end: -1 }); const hasMounted = useRef(false); const setBottomReached = useToggleEvent(onBottomReached ?? noop); const setTopReached = useToggleEvent(onTopReached ?? noop); const getKey = useMemo(() => { return typeof itemKey === 'function' ? itemKey : (value: T) => value[itemKey] as unknown as string; }, [itemKey]); useEffect(() => { setBottomReached(false); setTopReached(false); }, [items, setBottomReached, setTopReached]); const checkBounds = useEvent((frame: Frame = visibleFrame.current) => { onVisibleFrameChange?.(frame); visibleFrame.current = frame; if (frame.begin === -1 || frame.end === -1 || isScrolling.current) { return; } setBottomReached(frame.end <= triggerDistance); setTopReached(frame.begin <= triggerDistance); stickToBottomReached.current = frame.end === 0; }); const smoothScrolling = useEvent((container: HTMLElement, item: HTMLElement, abortController?: AbortController) => { return smoothScrollToCenter(container, item, jumpAnimation, abortController); }); const abortControllerReference = useRef(); const handleJumpScroll = useCallback( async (abortController = new AbortController()) => { if (abortControllerReference.current) { abortControllerReference.current.abort(); } if (!containerReference.current || !focusElementReference.current) { return; } abortControllerReference.current = abortController; isScrolling.current = true; if (hasMounted.current) { await smoothScrolling(containerReference.current, focusElementReference.current, abortController); } else { focusElementReference.current.scrollIntoView({ behavior: 'auto', block: 'center' }); } isScrolling.current = false; checkBounds(); }, [checkBounds, smoothScrolling], ); const onVisibleItemsChange = useVisibleFrame({ getKey, items, onVisibleFrameUpdated: checkBounds, }); const { observer, visibleItemKeys } = useVisibleItems(containerReference, onVisibleItemsChange); const [scheduleJumpScroll, isJumpScheduled] = useScheduleOnNextRender(handleJumpScroll); const lastScrolledItem = useRef(); const handleScrollToFocusItem = useEvent(() => { if (focusedItem === lastScrolledItem.current) { return; } lastScrolledItem.current = focusedItem; if (focusedItem && !isJumpScheduled()) { handleJumpScroll().catch(() => { /* Ignore aborted jump error */ }); } }); const itemsToRender = useEndlessList({ getKey, initialItems, items, compareItems, handleJump: scheduleJumpScroll, focusedItem, visibleItemKeys, lastScrolledItem, }); useEffect(() => { handleScrollToFocusItem(); hasMounted.current = true; }, [handleScrollToFocusItem, itemsToRender]); useEffect(() => { return () => { hasMounted.current = false; }; }, []); const handleStickToBottom = useEvent(() => { const container = containerReference.current; if (container && stickToBottomReached.current && !focusedItem) { container.scrollTo({ top: container.scrollHeight }); } }); useLayoutEffect(() => { if (canStickToBottom) { handleStickToBottom(); } }, [itemsToRender, canStickToBottom, handleStickToBottom]); return ( {itemsToRender.map((item) => ( ))} ); };