2024-02-02 10:17:14 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import { useInView } from "react-intersection-observer";
|
|
|
|
import { FormattedMessage } from "react-intl";
|
2024-02-02 11:51:57 +00:00
|
|
|
import usePageDimensions from "@/Hooks/usePageDimensions";
|
|
|
|
import {debounce} from "@/Utils";
|
2024-02-02 10:17:14 +00:00
|
|
|
|
|
|
|
interface ShowMoreProps {
|
|
|
|
text?: string;
|
|
|
|
className?: string;
|
|
|
|
onClick: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LoadMore = ({ text, onClick, className = "" }: ShowMoreProps) => {
|
|
|
|
return (
|
|
|
|
<button type="button" className={className} onClick={onClick}>
|
|
|
|
{text || <FormattedMessage defaultMessage="Load more" id="00LcfG" />}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LoadMore;
|
|
|
|
|
|
|
|
export function AutoLoadMore({ text, onClick, className }: ShowMoreProps) {
|
2024-02-02 11:51:57 +00:00
|
|
|
const { ref, inView } = useInView({ rootMargin: "1000px" });
|
|
|
|
const { height } = usePageDimensions();
|
2024-02-02 10:17:14 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (inView) {
|
2024-02-02 11:51:57 +00:00
|
|
|
// TODO improve feed performance. Something in image grid makes it slow when feed size grows.
|
|
|
|
return debounce(100, onClick);
|
2024-02-02 10:17:14 +00:00
|
|
|
}
|
2024-02-02 11:51:57 +00:00
|
|
|
}, [inView, height]);
|
2024-02-02 10:17:14 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div ref={ref}>
|
|
|
|
<LoadMore onClick={onClick} text={text} className={className} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|