2021-10-29 20:11:23 +02:00
|
|
|
/*
|
|
|
|
|
* 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 from 'react';
|
2021-10-30 16:10:34 +03:30
|
|
|
import MangaGrid, { IMangaGridProps } from 'components/MangaGrid';
|
2022-03-06 16:45:25 +00:00
|
|
|
import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext';
|
2022-04-07 12:53:14 +02:00
|
|
|
import { StringParam, useQueryParam } from 'use-query-params';
|
2021-10-29 20:11:23 +02:00
|
|
|
|
|
|
|
|
const FILTERED_OUT_MESSAGE = 'There are no Manga matching this filter';
|
|
|
|
|
|
|
|
|
|
function unreadFilter(unread: NullAndUndefined<boolean>, { unreadCount }: IMangaCard): boolean {
|
|
|
|
|
switch (unread) {
|
|
|
|
|
case true:
|
|
|
|
|
return !!unreadCount && unreadCount >= 1;
|
|
|
|
|
case false:
|
|
|
|
|
return unreadCount === 0;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-07 15:59:48 +05:30
|
|
|
function downloadedFilter(downloaded: NullAndUndefined<boolean>,
|
|
|
|
|
{ downloadCount }: IMangaCard): boolean {
|
|
|
|
|
switch (downloaded) {
|
|
|
|
|
case true:
|
|
|
|
|
return !!downloadCount && downloadCount >= 1;
|
|
|
|
|
case false:
|
|
|
|
|
return downloadCount === 0;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 12:45:34 +01:00
|
|
|
function queryFilter(query: NullAndUndefined<string>, { title }: IMangaCard): boolean {
|
|
|
|
|
if (!query) return true;
|
|
|
|
|
return title.toLowerCase().includes(query.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 20:11:23 +02:00
|
|
|
function filterManga(mangas: IMangaCard[]): IMangaCard[] {
|
2022-04-07 12:53:14 +02:00
|
|
|
const [query] = useQueryParam('query', StringParam);
|
|
|
|
|
|
|
|
|
|
const { options: { downloaded, unread } } = useLibraryOptionsContext();
|
2021-10-29 20:11:23 +02:00
|
|
|
return mangas
|
2021-11-07 15:59:48 +05:30
|
|
|
.filter((manga) => downloadedFilter(downloaded, manga)
|
|
|
|
|
&& unreadFilter(unread, manga)
|
|
|
|
|
&& queryFilter(query, manga));
|
2021-10-29 20:11:23 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-24 06:16:21 +00:00
|
|
|
function toReadSort(a: IMangaCard, b: IMangaCard): number {
|
|
|
|
|
if (!a.unreadCount) return -1;
|
|
|
|
|
if (!b.unreadCount) return 1;
|
|
|
|
|
return a.unreadCount > b.unreadCount ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toSortAlph(a: IMangaCard, b: IMangaCard): number {
|
|
|
|
|
return a.title < b.title ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function toSortID(a: IMangaCard, b: IMangaCard): number {
|
|
|
|
|
return a.id > b.id ? 1 : -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortManga(mangas: IMangaCard[]): IMangaCard[] {
|
2022-04-07 12:53:14 +02:00
|
|
|
const { options: { sorts, sortDesc } } = useLibraryOptionsContext();
|
2022-02-24 06:16:21 +00:00
|
|
|
return (sorts === 'sortID' || sorts === undefined) && !sortDesc ? mangas : mangas.sort((a, b) => {
|
|
|
|
|
const c = sortDesc === true ? b : a;
|
|
|
|
|
const d = sortDesc === true ? a : b;
|
|
|
|
|
if (sorts === 'sortToRead') { return toReadSort(c, d); }
|
|
|
|
|
if (sorts === 'sortAlph') { return toSortAlph(c, d); }
|
|
|
|
|
if (sorts === 'sortID' || sorts === undefined) { return toSortID(c, d); }
|
|
|
|
|
return 1;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-29 20:11:23 +02:00
|
|
|
export default function LibraryMangaGrid(props: IMangaGridProps) {
|
|
|
|
|
const {
|
|
|
|
|
mangas, isLoading, hasNextPage, lastPageNum, setLastPageNum, message,
|
|
|
|
|
} = props;
|
|
|
|
|
|
2022-04-07 12:53:14 +02:00
|
|
|
const [query] = useQueryParam('query', StringParam);
|
2022-11-24 09:41:03 +01:00
|
|
|
const { options } = useLibraryOptionsContext();
|
|
|
|
|
const { unread, downloaded } = options;
|
2021-10-29 20:11:23 +02:00
|
|
|
const filteredManga = filterManga(mangas);
|
2022-02-24 06:16:21 +00:00
|
|
|
const sortedManga = sortManga(filteredManga);
|
2022-03-11 15:54:45 +00:00
|
|
|
const DoneManga = sortedManga.map((ele) => {
|
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
|
ele.inLibrary = undefined;
|
|
|
|
|
return ele;
|
|
|
|
|
});
|
2022-11-24 09:41:03 +01:00
|
|
|
const showFilteredOutMessage = (unread != null || downloaded != null || query)
|
2021-11-10 09:44:27 +01:00
|
|
|
&& filteredManga.length === 0 && mangas.length > 0;
|
2021-10-29 20:11:23 +02:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<MangaGrid
|
2022-03-11 15:54:45 +00:00
|
|
|
mangas={DoneManga}
|
2021-10-29 20:11:23 +02:00
|
|
|
isLoading={isLoading}
|
|
|
|
|
hasNextPage={hasNextPage}
|
|
|
|
|
lastPageNum={lastPageNum}
|
|
|
|
|
setLastPageNum={setLastPageNum}
|
|
|
|
|
message={showFilteredOutMessage ? FILTERED_OUT_MESSAGE : message}
|
2022-03-06 16:45:25 +00:00
|
|
|
gridLayout={options.gridLayout}
|
2021-10-29 20:11:23 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|