infinite scroll support

This commit is contained in:
Sriram Hariharan
2023-03-03 23:13:31 -06:00
parent e9c420a873
commit 39016c93aa
2 changed files with 67 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
import { useState, useEffect } from 'react';
import { sleep } from 'src/shared/util/time';
/**
* Hook to execute a callback when the user scrolls to the bottom of the page
* @param callback the function to be called when the user scrolls to the bottom of the page
* @returns isLoading boolean to indicate if the callback is currently being executed
*/
export default function useInfiniteScroll(callback: () => Promise<boolean>): boolean {
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
window.addEventListener('scroll', isScrolling);
return () => window.removeEventListener('scroll', isScrolling);
}, []);
useEffect(() => {
if (!isLoading) return;
callback().then(isFinished => {
if (!isFinished) {
sleep(1000).then(() => {
setIsLoading(false);
});
}
});
}, [isLoading]);
function isScrolling() {
if (
window.innerHeight + document.documentElement.scrollTop !== document.documentElement.offsetHeight ||
isLoading
)
return;
setIsLoading(true);
}
return isLoading;
}