-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(web): dashboard projects is loading more than expected (#1193)
- Loading branch information
Showing
6 changed files
with
103 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { useCallback, useEffect, useRef } from "react"; | ||
|
||
type Props = { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
data: any; | ||
onLoadMore: () => void; | ||
}; | ||
|
||
const BOTTOM_ALLOWANCE = 50; // when there are 50px left to scroll, load more | ||
const SHORTER_LIMIT = 20; // when the content minuse this is smaller than wrapper we load more | ||
|
||
export default ({ data, onLoadMore }: Props) => { | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
const contentRef = useRef<HTMLDivElement>(null); | ||
|
||
const onLoadMoreRef = useRef(onLoadMore); | ||
onLoadMoreRef.current = onLoadMore; | ||
|
||
const checkForLoadMore = useCallback(() => { | ||
const wrapperElement = wrapperRef.current; | ||
const contentElement = contentRef.current; | ||
|
||
if (wrapperElement && contentElement) { | ||
if ( | ||
contentElement.offsetHeight - SHORTER_LIMIT < | ||
wrapperElement.offsetHeight || | ||
contentElement.scrollHeight - | ||
wrapperElement.scrollTop - | ||
wrapperElement.clientHeight < | ||
BOTTOM_ALLOWANCE | ||
) { | ||
onLoadMoreRef.current?.(); | ||
} | ||
} | ||
}, []); | ||
|
||
// Check on data change | ||
useEffect(() => { | ||
checkForLoadMore(); | ||
}, [data, checkForLoadMore]); | ||
|
||
// Check on scroll | ||
useEffect(() => { | ||
const wrapperElement = wrapperRef.current; | ||
if (!wrapperElement) return; | ||
|
||
wrapperElement.addEventListener("scroll", checkForLoadMore); | ||
|
||
return () => { | ||
wrapperElement.removeEventListener("scroll", checkForLoadMore); | ||
}; | ||
}, [data, checkForLoadMore]); | ||
|
||
// Check on resize | ||
useEffect(() => { | ||
const wrapperElement = wrapperRef.current; | ||
const contentElement = contentRef.current; | ||
|
||
if (!wrapperElement || !contentElement) return; | ||
|
||
const wrapperObserver = new ResizeObserver(checkForLoadMore); | ||
const contentObserver = new ResizeObserver(checkForLoadMore); | ||
|
||
wrapperObserver.observe(wrapperElement); | ||
contentObserver.observe(contentElement); | ||
|
||
checkForLoadMore(); | ||
|
||
return () => { | ||
wrapperObserver.disconnect(); | ||
contentObserver.disconnect(); | ||
}; | ||
}, [data, checkForLoadMore]); | ||
|
||
return { | ||
wrapperRef, | ||
contentRef | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.