2022-11-09 18:09:15 +01: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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-12-25 21:15:31 +01:00
|
|
|
import { Box, CircularProgress, Stack, styled, Tooltip } from '@mui/material';
|
2022-11-09 18:09:15 +01:00
|
|
|
import Typography from '@mui/material/Typography';
|
2023-12-30 04:04:59 +01:00
|
|
|
import React, { useMemo } from 'react';
|
2022-11-09 18:09:15 +01:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-12-18 22:13:43 +01:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import DownloadIcon from '@mui/icons-material/Download';
|
2023-12-18 22:25:25 +01:00
|
|
|
import DoneAllIcon from '@mui/icons-material/DoneAll';
|
2023-12-30 14:30:04 +01:00
|
|
|
import { TChapter, TManga } from '@/typings.ts';
|
2023-10-28 00:32:02 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2023-12-30 14:30:04 +01:00
|
|
|
import { ChapterCard } from '@/components/chapter/ChapterCard.tsx';
|
|
|
|
|
import { ResumeFab } from '@/components/manga/ResumeFAB.tsx';
|
|
|
|
|
import { filterAndSortChapters, useChapterOptions } from '@/components/chapter/util.tsx';
|
|
|
|
|
import { EmptyView } from '@/components/util/EmptyView.tsx';
|
|
|
|
|
import { ChaptersToolbarMenu } from '@/components/chapter/ChaptersToolbarMenu.tsx';
|
|
|
|
|
import { SelectionFAB } from '@/components/collection/SelectionFAB.tsx';
|
|
|
|
|
import { DEFAULT_FULL_FAB_HEIGHT } from '@/components/util/StyledFab.tsx';
|
2023-12-30 04:04:59 +01:00
|
|
|
import { DownloadType } from '@/lib/graphql/generated/graphql.ts';
|
2023-12-25 21:20:00 +01:00
|
|
|
import { useSelectableCollection } from '@/components/collection/useSelectableCollection.ts';
|
|
|
|
|
import { SelectableCollectionSelectAll } from '@/components/collection/SelectableCollectionSelectAll.tsx';
|
2023-12-30 04:04:59 +01:00
|
|
|
import { Chapters } from '@/lib/data/Chapters.ts';
|
|
|
|
|
import { ChaptersWithMeta } from '@/lib/data/ChaptersWithMeta.ts';
|
2023-12-30 14:30:04 +01:00
|
|
|
import { ChapterActionMenuItems } from '@/components/chapter/ChapterActionMenuItems.tsx';
|
2024-03-02 15:28:58 +01:00
|
|
|
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-12-18 21:40:56 +01:00
|
|
|
const ChapterListHeader = styled(Stack)(({ theme }) => ({
|
|
|
|
|
margin: 8,
|
|
|
|
|
marginBottom: 0,
|
|
|
|
|
marginRight: '10px',
|
|
|
|
|
minHeight: 40,
|
|
|
|
|
[theme.breakpoints.down('md')]: {
|
|
|
|
|
marginRight: 0,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
const StyledVirtuoso = styled(Virtuoso)(({ theme }) => ({
|
|
|
|
|
listStyle: 'none',
|
|
|
|
|
padding: 0,
|
|
|
|
|
minHeight: '200px',
|
|
|
|
|
[theme.breakpoints.up('md')]: {
|
|
|
|
|
width: '50vw',
|
|
|
|
|
// 64px for the Appbar, 48px for the ChapterCount Header
|
|
|
|
|
height: 'calc(100vh - 64px - 48px)',
|
|
|
|
|
margin: 0,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
2022-11-21 01:33:21 +01:00
|
|
|
export interface IChapterWithMeta {
|
2023-10-15 16:03:08 +02:00
|
|
|
chapter: TChapter;
|
2023-10-04 22:34:32 +02:00
|
|
|
downloadChapter: DownloadType | undefined;
|
2023-02-06 10:06:33 +01:00
|
|
|
selected: boolean | null;
|
2022-11-21 01:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-09 18:09:15 +01:00
|
|
|
interface IProps {
|
2023-10-15 16:03:08 +02:00
|
|
|
manga: TManga;
|
2023-09-30 16:55:35 +02:00
|
|
|
isRefreshing: boolean;
|
2022-11-09 18:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const ChapterList: React.FC<IProps> = ({ manga, isRefreshing }) => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-01-01 22:40:39 +01:00
|
|
|
const { data: downloaderData } = requestManager.useGetDownloadStatus();
|
|
|
|
|
const queue = (downloaderData?.downloadStatus.queue as DownloadType[]) ?? [];
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const [options, dispatch] = useChapterOptions(manga.id);
|
2023-12-16 19:00:05 +01:00
|
|
|
const { data: chaptersData, loading: isLoading } = requestManager.useGetMangaChapters(manga.id);
|
2023-10-15 16:03:08 +02:00
|
|
|
const chapters = useMemo(() => chaptersData?.chapters.nodes ?? [], [chaptersData?.chapters.nodes]);
|
2023-11-19 21:17:35 +01:00
|
|
|
|
2024-04-05 02:03:00 +02:00
|
|
|
const chapterIds = useMemo(() => chapters.map((chapter) => chapter.id), [chapters]);
|
|
|
|
|
|
2023-12-25 21:20:00 +01:00
|
|
|
const { areNoItemsSelected, areAllItemsSelected, selectedItemIds, handleSelectAll, handleSelection } =
|
2024-04-05 02:03:00 +02:00
|
|
|
useSelectableCollection(chapters.length, { itemIds: chapterIds, currentKey: 'default' });
|
2023-12-25 21:20:00 +01:00
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const visibleChapters = useMemo(() => filterAndSortChapters(chapters, options), [chapters, options]);
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2024-04-01 00:57:47 +02:00
|
|
|
const nextChapterIndexToRead = manga.firstUnreadChapter?.sourceOrder ?? 1;
|
2024-01-11 03:53:06 +01:00
|
|
|
const isLatestChapterRead = manga.chapters.totalCount === manga.latestReadChapter?.sourceOrder;
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-12-18 22:25:25 +01:00
|
|
|
const areAllChaptersRead = manga.unreadCount === 0;
|
2023-12-18 22:13:43 +01:00
|
|
|
const areAllChaptersDownloaded = manga.downloadCount === manga.chapters.totalCount;
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
const noChaptersFound = chapters.length === 0;
|
|
|
|
|
const noChaptersMatchingFilter = !noChaptersFound && visibleChapters.length === 0;
|
|
|
|
|
|
|
|
|
|
const chaptersWithMeta: IChapterWithMeta[] = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
visibleChapters.map((chapter) => {
|
|
|
|
|
const downloadChapter = queue?.find(
|
2023-10-04 22:34:32 +02:00
|
|
|
(cd) => cd.chapter.sourceOrder === chapter.sourceOrder && cd.chapter.manga.id === chapter.manga.id,
|
2023-09-30 16:55:35 +02:00
|
|
|
);
|
2023-12-25 21:20:00 +01:00
|
|
|
const selected = !areNoItemsSelected ? selectedItemIds.includes(chapter.id) : null;
|
2023-09-30 16:55:35 +02:00
|
|
|
return {
|
|
|
|
|
chapter,
|
|
|
|
|
downloadChapter,
|
|
|
|
|
selected,
|
|
|
|
|
};
|
|
|
|
|
}),
|
2023-12-25 21:20:00 +01:00
|
|
|
[queue, selectedItemIds, visibleChapters],
|
2023-09-30 16:55:35 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const chapterListFAB = useMemo(() => {
|
2023-12-25 21:20:00 +01:00
|
|
|
const selectedChapters = chaptersWithMeta.filter((chapter) => chapter.selected);
|
|
|
|
|
|
|
|
|
|
if (selectedChapters.length) {
|
|
|
|
|
return (
|
|
|
|
|
<SelectionFAB selectedItemsCount={selectedChapters.length} title="chapter.title">
|
|
|
|
|
{(handleClose) => (
|
2023-12-30 04:04:59 +01:00
|
|
|
<ChapterActionMenuItems selectedChapters={selectedChapters} onClose={handleClose} />
|
2023-12-25 21:20:00 +01:00
|
|
|
)}
|
|
|
|
|
</SelectionFAB>
|
|
|
|
|
);
|
2023-09-30 16:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isLatestChapterRead) {
|
|
|
|
|
return <ResumeFab chapterIndex={nextChapterIndexToRead} mangaId={manga.id} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2023-12-25 21:20:00 +01:00
|
|
|
}, [chaptersWithMeta, isLatestChapterRead]);
|
2023-09-30 16:55:35 +02:00
|
|
|
|
|
|
|
|
if (isLoading || (noChaptersFound && isRefreshing)) {
|
2022-11-09 18:09:15 +01:00
|
|
|
return (
|
2023-02-06 10:06:33 +01:00
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
margin: '10px auto',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
}}
|
2022-11-09 18:09:15 +01:00
|
|
|
>
|
|
|
|
|
<CircularProgress thickness={5} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Stack direction="column" sx={{ position: 'relative' }}>
|
2023-12-18 21:40:56 +01:00
|
|
|
<ChapterListHeader direction="row" alignItems="center" justifyContent="space-between">
|
2022-11-09 18:09:15 +01:00
|
|
|
<Typography variant="h5">
|
2023-03-23 13:59:32 +01:00
|
|
|
{`${visibleChapters.length} ${t('chapter.title', {
|
|
|
|
|
count: visibleChapters.length,
|
|
|
|
|
})}`}
|
2022-11-09 18:09:15 +01:00
|
|
|
</Typography>
|
|
|
|
|
|
2023-12-18 21:40:56 +01:00
|
|
|
<Stack direction="row" sx={{ paddingRight: '24px' }}>
|
2023-12-18 22:25:25 +01:00
|
|
|
<Tooltip title={t('chapter.action.mark_as_read.add.label.action.current')}>
|
|
|
|
|
<IconButton
|
|
|
|
|
disabled={areAllChaptersRead}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() =>
|
|
|
|
|
Chapters.markAsRead(
|
|
|
|
|
ChaptersWithMeta.getChapters(ChaptersWithMeta.getNonRead(chaptersWithMeta)),
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-12-18 22:25:25 +01:00
|
|
|
>
|
|
|
|
|
<DoneAllIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2023-12-18 22:13:43 +01:00
|
|
|
<Tooltip title={t('chapter.action.download.add.label.action')}>
|
|
|
|
|
<IconButton
|
|
|
|
|
disabled={areAllChaptersDownloaded}
|
2023-12-30 04:04:59 +01:00
|
|
|
onClick={() =>
|
|
|
|
|
Chapters.download(
|
|
|
|
|
ChaptersWithMeta.getIds(ChaptersWithMeta.getNonDownloaded(chaptersWithMeta)),
|
2024-03-02 15:28:58 +01:00
|
|
|
).catch(defaultPromiseErrorHandler('ChapterList::download'))
|
2023-12-30 04:04:59 +01:00
|
|
|
}
|
2023-12-18 22:13:43 +01:00
|
|
|
>
|
|
|
|
|
<DownloadIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</Tooltip>
|
2022-11-09 18:09:15 +01:00
|
|
|
<ChaptersToolbarMenu options={options} optionsDispatch={dispatch} />
|
2023-12-25 21:20:00 +01:00
|
|
|
<SelectableCollectionSelectAll
|
|
|
|
|
areAllItemsSelected={areAllItemsSelected}
|
|
|
|
|
areNoItemsSelected={areNoItemsSelected}
|
|
|
|
|
onChange={(checked) =>
|
|
|
|
|
handleSelectAll(checked, checked ? chapters.map((chapter) => chapter.id) : [])
|
|
|
|
|
}
|
|
|
|
|
/>
|
2023-12-18 21:40:56 +01:00
|
|
|
</Stack>
|
|
|
|
|
</ChapterListHeader>
|
2022-11-09 18:09:15 +01:00
|
|
|
|
2023-03-23 13:59:32 +01:00
|
|
|
{noChaptersFound && <EmptyView message={t('chapter.error.label.no_chapter_found')} />}
|
|
|
|
|
{noChaptersMatchingFilter && <EmptyView message={t('chapter.error.label.no_matches')} />}
|
2022-11-09 18:09:15 +01:00
|
|
|
|
|
|
|
|
<StyledVirtuoso
|
2023-02-06 10:06:33 +01:00
|
|
|
style={{
|
|
|
|
|
// override Virtuoso default values and set them with class
|
2022-11-09 18:09:15 +01:00
|
|
|
height: 'undefined',
|
|
|
|
|
// 900 is the md breakpoint in MUI
|
|
|
|
|
overflowY: window.innerWidth < 900 ? 'visible' : 'auto',
|
|
|
|
|
}}
|
2023-12-25 21:15:31 +01:00
|
|
|
components={{ Footer: () => <Box sx={{ paddingBottom: DEFAULT_FULL_FAB_HEIGHT }} /> }}
|
|
|
|
|
totalCount={visibleChapters.length}
|
|
|
|
|
itemContent={(index: number) => (
|
|
|
|
|
<ChapterCard
|
|
|
|
|
{...chaptersWithMeta[index]}
|
|
|
|
|
allChapters={chapters}
|
|
|
|
|
showChapterNumber={options.showChapterNumber}
|
2024-04-05 02:03:00 +02:00
|
|
|
onSelect={(selected, selectRange) =>
|
|
|
|
|
handleSelection(chaptersWithMeta[index].chapter.id, selected, { selectRange })
|
|
|
|
|
}
|
2023-12-25 21:15:31 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
2022-11-09 18:09:15 +01:00
|
|
|
useWindowScroll={window.innerWidth < 900}
|
|
|
|
|
overscan={window.innerHeight * 0.5}
|
|
|
|
|
/>
|
|
|
|
|
</Stack>
|
2023-09-30 16:55:35 +02:00
|
|
|
{chapterListFAB}
|
2022-11-09 18:09:15 +01:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|