2021-05-30 04:01:49 +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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2022-11-26 13:51:56 +01:00
|
|
|
import DeleteIcon from '@mui/icons-material/Delete';
|
|
|
|
|
import DragHandle from '@mui/icons-material/DragHandle';
|
2021-09-09 17:51:22 +04:30
|
|
|
import PauseIcon from '@mui/icons-material/Pause';
|
2022-11-26 13:51:56 +01:00
|
|
|
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Card from '@mui/material/Card';
|
|
|
|
|
import CardActionArea from '@mui/material/CardActionArea';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
2025-04-18 14:28:06 +02:00
|
|
|
import Box from '@mui/material/Box';
|
2021-09-09 17:51:22 +04:30
|
|
|
import IconButton from '@mui/material/IconButton';
|
2025-04-18 14:28:06 +02:00
|
|
|
import React, { memo, useCallback, useEffect, useLayoutEffect, useMemo, useState } from 'react';
|
2022-03-03 21:05:14 +00:00
|
|
|
import Typography from '@mui/material/Typography';
|
2022-11-26 13:51:56 +01:00
|
|
|
import { Link } from 'react-router-dom';
|
2023-02-14 20:08:03 +03:30
|
|
|
import { useTranslation } from 'react-i18next';
|
2023-12-10 00:09:00 +01:00
|
|
|
import DeleteSweepIcon from '@mui/icons-material/DeleteSweep';
|
2023-12-13 01:55:38 +01:00
|
|
|
import { Virtuoso } from 'react-virtuoso';
|
2024-08-31 16:36:03 +02:00
|
|
|
import CardContent from '@mui/material/CardContent';
|
2024-09-12 16:02:20 +02:00
|
|
|
import Refresh from '@mui/icons-material/Refresh';
|
2025-04-18 14:28:06 +02:00
|
|
|
import { closestCenter, DndContext, DragEndEvent } from '@dnd-kit/core';
|
|
|
|
|
import { SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { DownloadStateIndicator } from '@/modules/core/components/DownloadStateIndicator.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/placeholder/EmptyViewAbsoluteCentered.tsx';
|
|
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/placeholder/LoadingPlaceholder.tsx';
|
|
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-05 19:15:25 +02:00
|
|
|
import { ChapterDownloadStatus, ChapterIdInfo } from '@/modules/chapter/services/Chapters.ts';
|
2024-08-21 21:29:01 +02:00
|
|
|
import { DownloaderState, DownloadState } from '@/lib/graphql/generated/graphql.ts';
|
2024-12-11 20:10:59 +01:00
|
|
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
2025-04-18 14:28:06 +02:00
|
|
|
import { getErrorMessage, noOp } from '@/lib/HelperFunctions.ts';
|
2025-01-21 20:34:17 +01:00
|
|
|
import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
|
2025-03-29 03:12:36 +01:00
|
|
|
import { MUIUtil } from '@/lib/mui/MUI.util.ts';
|
2025-04-18 14:28:06 +02:00
|
|
|
import { DndSortableItem } from '@/lib/dnd-kit/DndSortableItem.tsx';
|
|
|
|
|
import { DndKitUtil } from '@/lib/dnd-kit/DndKitUtil.ts';
|
|
|
|
|
import { DndOverlayItem } from '@/lib/dnd-kit/DndOverlayItem.tsx';
|
2023-12-13 01:55:38 +01:00
|
|
|
|
2025-01-12 18:57:33 +01:00
|
|
|
const DownloadChapterItem = memo(
|
|
|
|
|
({
|
|
|
|
|
item,
|
|
|
|
|
handleDelete,
|
|
|
|
|
handleRetry,
|
|
|
|
|
}: {
|
|
|
|
|
item: ChapterDownloadStatus;
|
|
|
|
|
handleDelete: (chapter: ChapterIdInfo) => void;
|
|
|
|
|
handleRetry: (chapter: ChapterIdInfo) => void;
|
|
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
2023-12-13 01:55:38 +01:00
|
|
|
|
2025-01-12 18:57:33 +01:00
|
|
|
return (
|
2025-04-18 14:28:06 +02:00
|
|
|
<Box sx={{ p: 1, pb: 0 }}>
|
2025-01-12 18:57:33 +01:00
|
|
|
<Card>
|
|
|
|
|
<CardActionArea component={Link} to={AppRoutes.manga.path(item.manga.id)}>
|
|
|
|
|
<CardContent
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
p: 1.5,
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-03-29 03:12:36 +01:00
|
|
|
<IconButton {...MUIUtil.preventRippleProp()} sx={{ pointerEvents: 'none' }}>
|
2025-01-12 18:57:33 +01:00
|
|
|
<DragHandle />
|
|
|
|
|
</IconButton>
|
|
|
|
|
<Stack sx={{ flex: 1, ml: 1 }} direction="column">
|
|
|
|
|
<Typography variant="h6" component="h3">
|
|
|
|
|
{item.manga.title}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'block',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{item.chapter.name}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
<DownloadStateIndicator chapterId={item.chapter.id} />
|
|
|
|
|
{item.state === DownloadState.Error && (
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('global.button.retry')}>
|
2025-01-12 18:57:33 +01:00
|
|
|
<IconButton
|
2025-03-29 03:12:36 +01:00
|
|
|
{...MUIUtil.preventRippleProp()}
|
2025-01-12 18:57:33 +01:00
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
handleRetry(item.chapter);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Refresh />
|
|
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2025-01-12 18:57:33 +01:00
|
|
|
)}
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('chapter.action.download.delete.label.action')}>
|
2024-09-12 16:02:20 +02:00
|
|
|
<IconButton
|
2025-03-29 03:12:36 +01:00
|
|
|
{...MUIUtil.preventRippleProp()}
|
2024-09-12 16:02:20 +02:00
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
2025-01-12 18:57:33 +01:00
|
|
|
handleDelete(item.chapter);
|
2024-09-12 16:02:20 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2025-01-12 18:57:33 +01:00
|
|
|
<DeleteIcon />
|
2024-09-12 16:02:20 +02:00
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2025-01-12 18:57:33 +01:00
|
|
|
</CardContent>
|
|
|
|
|
</CardActionArea>
|
|
|
|
|
</Card>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const DownloadQueue: React.FC = () => {
|
2023-02-14 20:08:03 +03:30
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2023-12-13 02:05:03 +01:00
|
|
|
const [reorderDownload, { reset: revertReorder }] = requestManager.useReorderChapterInDownloadQueue();
|
|
|
|
|
|
2024-04-27 22:28:55 +02:00
|
|
|
const {
|
|
|
|
|
data: downloadStatusData,
|
|
|
|
|
loading: isLoading,
|
|
|
|
|
error,
|
|
|
|
|
refetch,
|
|
|
|
|
} = requestManager.useGetDownloadStatus({ notifyOnNetworkStatusChange: true });
|
2023-12-13 02:05:03 +01:00
|
|
|
const downloaderData = downloadStatusData?.downloadStatus;
|
|
|
|
|
|
2024-07-11 17:41:38 +02:00
|
|
|
const queue = downloaderData?.queue ?? [];
|
2023-12-13 02:05:03 +01:00
|
|
|
const status = downloaderData?.state ?? 'STARTED';
|
2023-12-10 00:09:00 +01:00
|
|
|
const isQueueEmpty = !queue.length;
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2025-01-21 20:34:17 +01:00
|
|
|
const { setTitle, setAction } = useNavBarContext();
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2025-04-18 14:28:06 +02:00
|
|
|
const dndItems = useMemo(() => queue.map((download) => download.chapter), [queue]);
|
|
|
|
|
const dndSensors = DndKitUtil.useSensorsForDevice();
|
|
|
|
|
const [dndActiveDownload, setDndActiveDownload] = useState<ChapterDownloadStatus | null>(null);
|
|
|
|
|
|
2023-12-10 00:09:00 +01:00
|
|
|
const clearQueue = async () => {
|
|
|
|
|
try {
|
|
|
|
|
await requestManager.clearDownloads().response;
|
|
|
|
|
} catch (e) {
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('download.queue.error.label.failed_delete_all'), 'error', getErrorMessage(e));
|
2023-12-10 00:09:00 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
const toggleQueueStatus = () => {
|
2024-08-21 21:29:01 +02:00
|
|
|
if (status === DownloaderState.Stopped) {
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.startDownloads();
|
2021-05-30 04:01:49 +04:30
|
|
|
} else {
|
2023-05-18 13:25:19 +02:00
|
|
|
requestManager.stopDownloads();
|
2021-05-30 04:01:49 +04:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-26 15:33:22 +02:00
|
|
|
useLayoutEffect(() => {
|
2023-03-23 13:59:32 +01:00
|
|
|
setTitle(t('download.queue.title'));
|
2023-12-10 00:09:00 +01:00
|
|
|
setAction(
|
|
|
|
|
<>
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('download.queue.label.delete_all')}>
|
2025-03-28 19:05:21 +01:00
|
|
|
<IconButton onClick={clearQueue} color="inherit">
|
2023-12-10 00:09:00 +01:00
|
|
|
<DeleteSweepIcon />
|
|
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip
|
|
|
|
|
title={t(status === DownloaderState.Started ? 'global.button.start' : 'global.button.stop')}
|
|
|
|
|
disabled={isQueueEmpty}
|
|
|
|
|
>
|
2025-03-28 19:05:21 +01:00
|
|
|
<IconButton onClick={toggleQueueStatus} disabled={isQueueEmpty} color="inherit">
|
2024-08-21 21:29:01 +02:00
|
|
|
{status === DownloaderState.Stopped ? <PlayArrowIcon /> : <PauseIcon />}
|
2023-12-10 00:09:00 +01:00
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</CustomTooltip>
|
2023-12-10 00:09:00 +01:00
|
|
|
</>,
|
|
|
|
|
);
|
2024-01-26 20:50:51 +01:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
setTitle('');
|
|
|
|
|
setAction(null);
|
|
|
|
|
};
|
2023-12-10 00:09:00 +01:00
|
|
|
}, [t, status, isQueueEmpty]);
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2023-12-13 01:55:38 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
const ignoreError = (e: WindowEventMap['error']) => {
|
|
|
|
|
if (
|
|
|
|
|
e.message === 'ResizeObserver loop completed with undelivered notifications.' ||
|
|
|
|
|
e.message === 'ResizeObserver loop limit exceeded'
|
|
|
|
|
) {
|
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Virtuoso's resize observer can throw this error,
|
|
|
|
|
// which is caught by DnD and aborts dragging.
|
|
|
|
|
window.addEventListener('error', ignoreError);
|
|
|
|
|
|
|
|
|
|
return () => window.removeEventListener('error', ignoreError);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-07-11 17:41:38 +02:00
|
|
|
const categoryReorder = (list: ChapterDownloadStatus[], from: number, to: number) => {
|
2023-12-13 02:05:03 +01:00
|
|
|
if (from === to) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reorderDownload({ variables: { input: { chapterId: list[from].chapter.id, to } } }).catch(() => {
|
|
|
|
|
revertReorder();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-18 14:28:06 +02:00
|
|
|
const onDragEnd = (event: DragEndEvent) => {
|
|
|
|
|
const { active, over } = event;
|
|
|
|
|
|
|
|
|
|
setDndActiveDownload(null);
|
|
|
|
|
|
|
|
|
|
if (!over || active.id === over.id) {
|
2023-12-13 02:05:03 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 14:28:06 +02:00
|
|
|
const oldIndex = queue.findIndex((download) => download.chapter.id === active.id);
|
|
|
|
|
const newIndex = queue.findIndex((download) => download.chapter.id === over.id);
|
|
|
|
|
|
|
|
|
|
categoryReorder(queue, oldIndex, newIndex);
|
2023-12-13 02:05:03 +01:00
|
|
|
};
|
2021-10-29 17:51:10 +05:30
|
|
|
|
2025-01-12 18:57:33 +01:00
|
|
|
const handleRetry = useCallback(async (chapter: ChapterIdInfo) => {
|
2024-09-12 16:02:20 +02:00
|
|
|
try {
|
|
|
|
|
await requestManager.addChapterToDownloadQueue(chapter.id).response;
|
|
|
|
|
} catch (e) {
|
2024-12-20 17:24:40 +01:00
|
|
|
makeToast(t('download.queue.error.label.failed_to_remove'), 'error', getErrorMessage(e));
|
2024-09-12 16:02:20 +02:00
|
|
|
}
|
2025-01-12 18:57:33 +01:00
|
|
|
}, []);
|
2024-09-12 16:02:20 +02:00
|
|
|
|
2025-01-12 18:57:33 +01:00
|
|
|
const handleDelete = useCallback(async (chapter: ChapterIdInfo) => {
|
2024-08-21 21:29:01 +02:00
|
|
|
const isRunning = status === DownloaderState.Started;
|
2023-05-15 18:15:28 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (isRunning) {
|
|
|
|
|
// required to stop before deleting otherwise the download kept going. Server issue?
|
2023-05-23 13:54:14 +02:00
|
|
|
await requestManager.stopDownloads().response;
|
2023-05-15 18:15:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
2022-11-26 13:51:56 +01:00
|
|
|
// remove from download queue
|
2023-09-01 20:40:24 +02:00
|
|
|
requestManager.removeChapterFromDownloadQueue(chapter.id).response,
|
2022-11-26 13:51:56 +01:00
|
|
|
// delete partial download, should be handle server side?
|
|
|
|
|
// bug: The folder and the last image downloaded are not deleted
|
2023-09-01 20:36:40 +02:00
|
|
|
requestManager.deleteDownloadedChapter(chapter.id).response,
|
2023-05-15 18:15:28 +02:00
|
|
|
]);
|
2024-04-27 22:28:55 +02:00
|
|
|
} catch (e) {
|
2025-01-01 16:44:18 +01:00
|
|
|
makeToast(t('download.queue.error.label.failed_to_remove'), 'error', getErrorMessage(e));
|
2023-05-15 18:15:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isRunning) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-31 01:42:54 +01:00
|
|
|
requestManager.startDownloads().response.catch(defaultPromiseErrorHandler('DownloadQueue::startDownloads'));
|
2025-01-12 18:57:33 +01:00
|
|
|
}, []);
|
2022-06-16 09:29:43 -04:00
|
|
|
|
2023-12-13 01:55:38 +01:00
|
|
|
if (isLoading) {
|
|
|
|
|
return <LoadingPlaceholder />;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-27 22:28:55 +02:00
|
|
|
if (error) {
|
|
|
|
|
return (
|
2024-04-29 15:29:33 +02:00
|
|
|
<EmptyViewAbsoluteCentered
|
2024-04-27 22:28:55 +02:00
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
2024-12-21 23:27:03 +01:00
|
|
|
messageExtra={getErrorMessage(error)}
|
2024-04-27 22:28:55 +02:00
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('DownloadQueue::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-10 00:09:00 +01:00
|
|
|
if (isQueueEmpty) {
|
2024-04-29 15:29:33 +02:00
|
|
|
return <EmptyViewAbsoluteCentered message={t('download.queue.label.no_downloads')} />;
|
2023-12-10 00:09:00 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
return (
|
2025-04-18 14:28:06 +02:00
|
|
|
<DndContext
|
|
|
|
|
sensors={dndSensors}
|
|
|
|
|
collisionDetection={closestCenter}
|
|
|
|
|
onDragStart={(event) =>
|
|
|
|
|
setDndActiveDownload(queue.find((download) => download.chapter.id === event.active.id) ?? null)
|
|
|
|
|
}
|
|
|
|
|
onDragEnd={onDragEnd}
|
|
|
|
|
onDragCancel={() => setDndActiveDownload(null)}
|
|
|
|
|
onDragAbort={() => setDndActiveDownload(null)}
|
|
|
|
|
>
|
|
|
|
|
<SortableContext items={dndItems} strategy={verticalListSortingStrategy}>
|
|
|
|
|
<Virtuoso
|
|
|
|
|
useWindowScroll
|
|
|
|
|
overscan={window.innerHeight * 0.5}
|
|
|
|
|
totalCount={queue.length}
|
|
|
|
|
computeItemKey={(index) => queue[index].chapter.id}
|
|
|
|
|
itemContent={(index) => (
|
|
|
|
|
<DndSortableItem
|
|
|
|
|
id={queue[index].chapter.id}
|
|
|
|
|
isDragging={queue[index].chapter.id === dndActiveDownload?.chapter.id}
|
|
|
|
|
>
|
|
|
|
|
<DownloadChapterItem
|
|
|
|
|
item={queue[index]}
|
|
|
|
|
handleDelete={handleDelete}
|
|
|
|
|
handleRetry={handleRetry}
|
|
|
|
|
/>
|
|
|
|
|
</DndSortableItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</SortableContext>
|
|
|
|
|
<DndOverlayItem isActive={!!dndActiveDownload}>
|
|
|
|
|
<DownloadChapterItem item={dndActiveDownload!} handleDelete={noOp} handleRetry={noOp} />
|
|
|
|
|
</DndOverlayItem>
|
|
|
|
|
</DndContext>
|
2021-05-30 04:01:49 +04:30
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|