mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-03 00:33:49 +03:00
basic infinite scroll added to catalogue page
This commit is contained in:
parent
e93b0a786e
commit
68205a9aac
@ -21,10 +21,13 @@ export function Catalogue() {
|
|||||||
|
|
||||||
const [searchResults, setSearchResults] = useState<CatalogueEntry[]>([]);
|
const [searchResults, setSearchResults] = useState<CatalogueEntry[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [isLoadingInfScroll, setIsLoadingInfScroll] = useState(false);
|
||||||
|
const [resultsExhausted, setResultsExhausted] = useState(false);
|
||||||
|
|
||||||
const contentRef = useRef<HTMLElement>(null);
|
const contentRef = useRef<HTMLElement>(null);
|
||||||
|
|
||||||
const cursorRef = useRef<number>(0);
|
const cursorRef = useRef<number>(0);
|
||||||
|
const cursorInfScrollRef = useRef<number>(cursorRef.current + 24);
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
@ -62,9 +65,42 @@ export function Catalogue() {
|
|||||||
cursor: cursorRef.current.toString(),
|
cursor: cursorRef.current.toString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
resetInfiniteScroll()
|
||||||
|
|
||||||
navigate(`/catalogue?${params.toString()}`);
|
navigate(`/catalogue?${params.toString()}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const resetInfiniteScroll = () =>{
|
||||||
|
cursorInfScrollRef.current = cursorRef.current + 24
|
||||||
|
setResultsExhausted(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
const infiniteLoading = () => {
|
||||||
|
if(resultsExhausted) return
|
||||||
|
const isAtBottom = contentRef.current?.offsetHeight! + contentRef.current?.scrollTop! == contentRef.current?.scrollHeight
|
||||||
|
|
||||||
|
if (isAtBottom) {
|
||||||
|
setIsLoadingInfScroll(true);
|
||||||
|
window.electron
|
||||||
|
.getGames(24, cursorInfScrollRef.current)
|
||||||
|
.then(({ results, cursor }) => {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
if (results.length == 0) {
|
||||||
|
setResultsExhausted(true)
|
||||||
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
cursorInfScrollRef.current += cursor;
|
||||||
|
setSearchResults([...searchResults, ...results]);
|
||||||
|
resolve(null);
|
||||||
|
}, 500);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setIsLoadingInfScroll(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SkeletonTheme baseColor={vars.color.background} highlightColor="#444">
|
<SkeletonTheme baseColor={vars.color.background} highlightColor="#444">
|
||||||
<section
|
<section
|
||||||
@ -78,7 +114,7 @@ export function Catalogue() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => navigate(-1)}
|
onClick={() => { resetInfiniteScroll(); navigate(-1) }}
|
||||||
theme="outline"
|
theme="outline"
|
||||||
disabled={cursor === 0 || isLoading}
|
disabled={cursor === 0 || isLoading}
|
||||||
>
|
>
|
||||||
@ -92,7 +128,7 @@ export function Catalogue() {
|
|||||||
</Button>
|
</Button>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section ref={contentRef} className={styles.content}>
|
<section ref={contentRef} className={styles.content} onScroll={infiniteLoading}>
|
||||||
<section className={styles.cards}>
|
<section className={styles.cards}>
|
||||||
{isLoading &&
|
{isLoading &&
|
||||||
Array.from({ length: 12 }).map((_, index) => (
|
Array.from({ length: 12 }).map((_, index) => (
|
||||||
@ -110,6 +146,11 @@ export function Catalogue() {
|
|||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{isLoadingInfScroll &&
|
||||||
|
Array.from({ length: 12 }).map((_, index) => (
|
||||||
|
<Skeleton key={index} className={styles.cardSkeleton} />
|
||||||
|
))}
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
</SkeletonTheme>
|
</SkeletonTheme>
|
||||||
|
Loading…
Reference in New Issue
Block a user