import { times } from "lodash-es"; import { useEffect, useRef, useState, type ReactNode } from "react"; import type { PropsWithSkeleton } from "../../types/propsWithSkeleton"; import "./index.css"; export interface IncrementalLoaderProps { initial: number; intermediate: number; rate?: number; skeleton?: boolean; data: Props[]; Component: (props: PropsWithSkeleton>) => ReactNode; } export function IncrementalLoader({ initial, rate = 2, data, skeleton, intermediate, Component, }: IncrementalLoaderProps) { const [count, setCount] = useState(initial); const sliced = data.slice(0, count); useEffect(() => setCount(initial), [data, initial]); return ( <> {!skeleton && sliced.map(({ key, ...props }) => )} {times( skeleton ? initial + intermediate : Math.min(intermediate, data.length - count), (index) => ( ) )} ); } interface ItemProps { children: ReactNode; rate: number; setCount: (setter: (state: number) => number) => void; } function Item({ setCount, rate, children }: ItemProps) { const wrapper = useRef(null); const observer = useRef(null); useEffect(() => { function dispose() { observer.current?.disconnect(); observer.current = null; } if (!wrapper.current) return; if (observer.current === null) { observer.current = new IntersectionObserver(([entry]) => { if (!entry.isIntersecting) return; setCount((state) => state + rate); }); observer.current.observe(wrapper.current); } return dispose; }, []); return (
{children}
); }