add pagination to library and fix some prettier issues (#237)

Co-authored-by: akxer <>
This commit is contained in:
akabhirav
2023-02-12 20:43:41 +05:30
committed by GitHub
parent ad9a12b2ec
commit aaedbb7be9
5 changed files with 37 additions and 31 deletions

1
.gitignore vendored
View File

@@ -2,5 +2,6 @@ node_modules/
.eslintcache .eslintcache
.vscode .vscode
.env .env
.idea
build/* build/*

View File

@@ -23,7 +23,7 @@ export interface IMangaGridProps {
lastPageNum: number; lastPageNum: number;
setLastPageNum: (lastPageNum: number) => void; setLastPageNum: (lastPageNum: number) => void;
gridLayout?: GridLayout; gridLayout?: GridLayout;
horisontal?: boolean | undefined; horizontal?: boolean | undefined;
noFaces?: boolean | undefined; noFaces?: boolean | undefined;
inLibraryIndicator?: boolean; inLibraryIndicator?: boolean;
} }
@@ -38,7 +38,7 @@ const MangaGrid: React.FC<IMangaGridProps> = (props) => {
lastPageNum, lastPageNum,
setLastPageNum, setLastPageNum,
gridLayout, gridLayout,
horisontal, horizontal,
noFaces, noFaces,
inLibraryIndicator, inLibraryIndicator,
} = props; } = props;
@@ -113,7 +113,7 @@ const MangaGrid: React.FC<IMangaGridProps> = (props) => {
container container
spacing={1} spacing={1}
style={ style={
horisontal horizontal
? { ? {
margin: 0, margin: 0,
width: '100%', width: '100%',

View File

@@ -6,10 +6,11 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
import React, { useMemo } from 'react'; import React, { useEffect, useState } from 'react';
import MangaGrid, { IMangaGridProps } from 'components/MangaGrid'; import MangaGrid from 'components/MangaGrid';
import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext';
import { StringParam, useQueryParam } from 'use-query-params'; import { StringParam, useQueryParam } from 'use-query-params';
import { useMediaQuery, useTheme } from '@mui/material';
const FILTERED_OUT_MESSAGE = 'There are no Manga matching this filter'; const FILTERED_OUT_MESSAGE = 'There are no Manga matching this filter';
@@ -90,37 +91,51 @@ const sortManga = (
return result; return result;
}; };
const LibraryMangaGrid: React.FC<IMangaGridProps & { lastLibraryUpdate: number }> = ({ interface LibraryMangaGridProps {
mangas: IMangaCard[];
isLoading: boolean;
message?: string;
}
const LibraryMangaGrid: React.FC<LibraryMangaGridProps & { lastLibraryUpdate: number }> = ({
mangas, mangas,
isLoading, isLoading,
hasNextPage,
lastPageNum,
setLastPageNum,
message, message,
lastLibraryUpdate, lastLibraryUpdate,
}) => { }) => {
const [query] = useQueryParam('query', StringParam); const [query] = useQueryParam('query', StringParam);
const { options } = useLibraryOptionsContext(); const { options } = useLibraryOptionsContext();
const { unread, downloaded } = options; const { unread, downloaded } = options;
const [filteredPaginatedMangas, setFilteredPaginatedMangas] = useState<IMangaCard[]>([]);
const [sortedManga, setSortedManga] = useState<IMangaCard[]>([]);
const [filteredManga, setFilteredManga] = useState<IMangaCard[]>([]);
const totalPages = (mangas ?? []).length / 10;
const theme = useTheme();
const isLargeScreen = useMediaQuery(theme.breakpoints.up('sm'));
const defaultPageNumber = isLargeScreen ? 4 : 1;
const [lastPageNum, setLastPageNum] = useState<number>(defaultPageNumber);
useEffect(() => {
setFilteredManga(filterManga(mangas, query, unread, downloaded));
setLastPageNum(defaultPageNumber);
window.scrollTo(0, 0);
}, [mangas, query, unread, downloaded]);
const sortedManga = useMemo( useEffect(() => {
() => sortManga(mangas, options.sorts, options.sortDesc), setSortedManga(sortManga(filteredManga, options.sorts, options.sortDesc));
[mangas, lastLibraryUpdate, options.sorts, options.sortDesc], }, [filteredManga, lastLibraryUpdate, options.sorts, options.sortDesc]);
);
const filteredManga = useMemo( useEffect(() => {
() => filterManga(sortedManga, query, unread, downloaded), setFilteredPaginatedMangas((sortedManga ?? []).slice(0, lastPageNum * 10));
[sortedManga, lastLibraryUpdate, query, unread, downloaded], }, [lastPageNum, sortedManga]);
);
const showFilteredOutMessage = const showFilteredOutMessage =
(unread != null || downloaded != null || query) && filteredManga.length === 0 && mangas.length > 0; (unread != null || downloaded != null || query) && filteredManga.length === 0 && mangas.length > 0;
return ( return (
<MangaGrid <MangaGrid
mangas={filteredManga} mangas={filteredPaginatedMangas}
isLoading={isLoading} isLoading={isLoading}
hasNextPage={hasNextPage} hasNextPage={lastPageNum < totalPages}
lastPageNum={lastPageNum} lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum} setLastPageNum={setLastPageNum}
message={showFilteredOutMessage ? FILTERED_OUT_MESSAGE : message} message={showFilteredOutMessage ? FILTERED_OUT_MESSAGE : message}

View File

@@ -51,10 +51,6 @@ export default function Library() {
}; };
}, []); }, []);
// a hack so MangaGrid doesn't stop working. I won't change it in case
// if I do manga pagination for library..
const [lastPageNum, setLastPageNum] = useState<number>(1);
const handleTabChange = (newTab: number) => { const handleTabChange = (newTab: number) => {
setTabSearchParam(newTab === 0 ? undefined : newTab); setTabSearchParam(newTab === 0 ? undefined : newTab);
}; };
@@ -76,9 +72,6 @@ export default function Library() {
<LibraryMangaGrid <LibraryMangaGrid
mangas={mangas} mangas={mangas}
lastLibraryUpdate={lastLibraryUpdate} lastLibraryUpdate={lastLibraryUpdate}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
message="Your Library is empty" message="Your Library is empty"
isLoading={activeTab != null && mangaLoading} isLoading={activeTab != null && mangaLoading}
/> />
@@ -117,9 +110,6 @@ export default function Library() {
<LibraryMangaGrid <LibraryMangaGrid
mangas={mangas} mangas={mangas}
lastLibraryUpdate={lastLibraryUpdate} lastLibraryUpdate={lastLibraryUpdate}
hasNextPage={false}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
message="Category is Empty" message="Category is Empty"
isLoading={mangaLoading} isLoading={mangaLoading}
/> />

View File

@@ -211,7 +211,7 @@ const SearchAll: React.FC = () => {
hasNextPage={false} hasNextPage={false}
lastPageNum={lastPageNum} lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum} setLastPageNum={setLastPageNum}
horisontal horizontal
noFaces noFaces
message={fetched[id] ? 'No manga was found!' : undefined} message={fetched[id] ? 'No manga was found!' : undefined}
inLibraryIndicator inLibraryIndicator