Files
suwayomi-material-you-webui/src/components/anime/AnimeGrid.tsx

63 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-05-27 17:13:22 +04:30
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import React, { useEffect, useRef } from 'react';
2021-09-09 17:51:22 +04:30
import Grid from '@mui/material/Grid';
2021-05-27 17:13:22 +04:30
import AnimeCard from './AnimeCard';
interface IProps{
mangas: IMangaCard[]
message?: string
hasNextPage: boolean
lastPageNum: number
setLastPageNum: (lastPageNum: number) => void
}
export default function AnimeGrid(props: IProps) {
const {
mangas, message, hasNextPage, lastPageNum, setLastPageNum,
} = props;
let mapped;
const lastManga = useRef<HTMLInputElement>(null);
2021-05-27 17:13:22 +04:30
const scrollHandler = () => {
if (lastManga.current) {
const rect = lastManga.current.getBoundingClientRect();
if (((rect.y + rect.height) / window.innerHeight < 2) && hasNextPage) {
setLastPageNum(lastPageNum + 1);
}
}
};
useEffect(() => {
window.addEventListener('scroll', scrollHandler, true);
return () => {
window.removeEventListener('scroll', scrollHandler, true);
};
}, [hasNextPage, mangas]);
if (mangas.length === 0) {
mapped = <h3>{message}</h3>;
} else {
mapped = mangas.map((it, idx) => {
if (idx === mangas.length - 1) {
return <AnimeCard manga={it} ref={lastManga} />;
}
return <AnimeCard manga={it} />;
});
}
return (
<Grid container spacing={1} style={{ margin: 0, width: '100%', padding: '5px' }}>
{mapped}
</Grid>
);
}
AnimeGrid.defaultProps = {
message: 'loading...',
};