2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
2021-01-26 23:32:12 +03:30
|
|
|
* 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/. */
|
|
|
|
|
|
2021-01-22 21:11:00 +03:30
|
|
|
import React, { useEffect, useRef } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import Grid from '@mui/material/Grid';
|
2021-10-30 16:10:34 +03:30
|
|
|
import EmptyView from 'components/util/EmptyView';
|
|
|
|
|
import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
|
2021-01-20 15:26:52 +03:30
|
|
|
import MangaCard from './MangaCard';
|
|
|
|
|
|
2021-10-29 20:11:23 +02:00
|
|
|
export interface IMangaGridProps{
|
2021-03-17 19:17:03 +03:30
|
|
|
mangas: IMangaCard[]
|
2021-10-24 22:56:10 +03:30
|
|
|
isLoading: boolean
|
2021-01-20 15:26:52 +03:30
|
|
|
message?: string
|
2021-09-19 17:30:34 +04:30
|
|
|
messageExtra?: JSX.Element
|
2021-01-22 21:11:00 +03:30
|
|
|
hasNextPage: boolean
|
|
|
|
|
lastPageNum: number
|
|
|
|
|
setLastPageNum: (lastPageNum: number) => void
|
2021-01-20 15:26:52 +03:30
|
|
|
}
|
|
|
|
|
|
2021-10-29 20:11:23 +02:00
|
|
|
export default function MangaGrid(props: IMangaGridProps) {
|
2021-01-22 21:11:00 +03:30
|
|
|
const {
|
2021-10-24 22:56:10 +03:30
|
|
|
mangas, isLoading, message, messageExtra, hasNextPage, lastPageNum, setLastPageNum,
|
2021-01-22 21:11:00 +03:30
|
|
|
} = props;
|
2021-01-20 15:26:52 +03:30
|
|
|
let mapped;
|
2021-09-09 18:37:23 +04:30
|
|
|
const lastManga = useRef<HTMLDivElement>(null);
|
2021-01-22 21:11:00 +03: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]);
|
2021-01-20 15:26:52 +03:30
|
|
|
|
|
|
|
|
if (mangas.length === 0) {
|
2021-10-24 22:56:10 +03:30
|
|
|
if (isLoading) {
|
|
|
|
|
mapped = (
|
|
|
|
|
<LoadingPlaceholder />
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
mapped = (
|
|
|
|
|
<EmptyView message={message!} messageExtra={messageExtra} />
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-01-20 15:26:52 +03:30
|
|
|
} else {
|
2021-01-22 21:11:00 +03:30
|
|
|
mapped = mangas.map((it, idx) => {
|
|
|
|
|
if (idx === mangas.length - 1) {
|
2021-09-09 20:43:13 +04:30
|
|
|
return (
|
|
|
|
|
<MangaCard
|
|
|
|
|
key={it.id}
|
|
|
|
|
manga={it}
|
|
|
|
|
ref={lastManga}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2021-01-22 21:11:00 +03:30
|
|
|
}
|
2021-09-09 20:43:13 +04:30
|
|
|
return (
|
|
|
|
|
<MangaCard
|
|
|
|
|
key={it.id}
|
|
|
|
|
manga={it}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2021-01-22 21:11:00 +03:30
|
|
|
});
|
2021-01-20 15:26:52 +03:30
|
|
|
}
|
|
|
|
|
|
2021-01-22 21:11:00 +03:30
|
|
|
return (
|
2021-03-17 19:17:03 +03:30
|
|
|
<Grid container spacing={1} style={{ margin: 0, width: '100%', padding: '5px' }}>
|
2021-01-22 21:11:00 +03:30
|
|
|
{mapped}
|
2021-01-23 01:58:18 +03:30
|
|
|
</Grid>
|
2021-01-22 21:11:00 +03:30
|
|
|
);
|
2021-01-20 15:26:52 +03:30
|
|
|
}
|
2021-01-22 21:11:00 +03:30
|
|
|
|
|
|
|
|
MangaGrid.defaultProps = {
|
2021-10-24 22:56:10 +03:30
|
|
|
message: '',
|
2021-09-19 17:30:34 +04:30
|
|
|
messageExtra: undefined,
|
2021-01-22 21:11:00 +03:30
|
|
|
};
|