Files
suwayomi-material-you-webui/src/screens/DownloadQueue.tsx

175 lines
8.1 KiB
TypeScript
Raw Normal View History

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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2021-05-30 04:01:49 +04:30
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';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
2023-11-05 17:22:29 +01:00
import { Card, CardActionArea, Stack, Box, Tooltip } from '@mui/material';
2021-09-09 17:51:22 +04:30
import IconButton from '@mui/material/IconButton';
import React, { useContext, useEffect } from 'react';
import { DragDropContext, Draggable } from 'react-beautiful-dnd';
import Typography from '@mui/material/Typography';
import { Link } from 'react-router-dom';
2023-02-14 20:08:03 +03:30
import { useTranslation } from 'react-i18next';
import DeleteSweepIcon from '@mui/icons-material/DeleteSweep';
2023-10-28 00:32:02 +02:00
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { StrictModeDroppable } from '@/lib/StrictModeDroppable';
import { makeToast } from '@/components/util/Toast';
import { DownloadStateIndicator } from '@/components/molecules/DownloadStateIndicator';
import { EmptyView } from '@/components/util/EmptyView';
2023-10-15 16:03:08 +02:00
import { DownloadType } from '@/lib/graphql/generated/graphql.ts';
import { TChapter } from '@/typings.ts';
2023-10-28 00:32:02 +02:00
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
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-10-04 22:34:32 +02:00
const { data: downloaderData } = requestManager.useDownloadSubscription();
const queue = (downloaderData?.downloadChanged.queue as DownloadType[]) ?? [];
const status = downloaderData?.downloadChanged.state ?? 'STARTED';
const isQueueEmpty = !queue.length;
2021-05-30 04:01:49 +04:30
2023-10-28 00:32:02 +02:00
const { setTitle, setAction } = useContext(NavBarContext);
2021-05-30 04:01:49 +04:30
const clearQueue = async () => {
try {
await requestManager.clearDownloads().response;
} catch (e) {
makeToast(t('download.queue.error.label.failed_delete_all'), 'error');
}
};
2021-05-30 04:01:49 +04:30
const toggleQueueStatus = () => {
2023-10-04 22:34:32 +02:00
if (status === 'STOPPED') {
requestManager.startDownloads();
2021-05-30 04:01:49 +04:30
} else {
requestManager.stopDownloads();
2021-05-30 04:01:49 +04:30
}
};
useEffect(() => {
setTitle(t('download.queue.title'));
setAction(
<>
<Tooltip title={t('download.queue.label.delete_all')}>
<IconButton onClick={clearQueue} size="large" disabled={isQueueEmpty}>
<DeleteSweepIcon />
</IconButton>
</Tooltip>
2021-05-30 04:01:49 +04:30
<Tooltip title={t(status === 'STOPPED' ? 'global.button.start' : 'global.button.stop')}>
<IconButton onClick={toggleQueueStatus} size="large" disabled={isQueueEmpty}>
{status === 'STOPPED' ? <PlayArrowIcon /> : <PauseIcon />}
</IconButton>
</Tooltip>
</>,
);
}, [t, status, isQueueEmpty]);
2021-05-30 04:01:49 +04:30
const onDragEnd = () => {};
2023-10-15 16:03:08 +02:00
const handleDelete = async (chapter: TChapter) => {
2023-10-04 22:34:32 +02:00
const isRunning = status === 'STARTED';
try {
if (isRunning) {
// required to stop before deleting otherwise the download kept going. Server issue?
await requestManager.stopDownloads().response;
}
await Promise.all([
// remove from download queue
2023-09-01 20:40:24 +02:00
requestManager.removeChapterFromDownloadQueue(chapter.id).response,
// 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,
]);
} catch (error) {
makeToast(t('download.queue.error.label.failed_to_remove'), 'error');
}
if (!isRunning) {
return;
}
requestManager.startDownloads().response.catch(() => {});
};
if (isQueueEmpty) {
return <EmptyView message={t('download.queue.label.no_downloads')} />;
}
2021-05-30 04:01:49 +04:30
return (
<DragDropContext onDragEnd={onDragEnd}>
<StrictModeDroppable droppableId="droppable">
{(droppableProvided) => (
<Box ref={droppableProvided.innerRef} sx={{ pt: 1 }}>
{queue.map((item, index) => (
<Draggable
key={`${item.chapter.manga.id}-${item.chapter.sourceOrder}`}
draggableId={`${item.chapter.manga.id}-${item.chapter.sourceOrder}`}
index={index}
>
{(draggableProvided, snapshot) => (
<Box
{...draggableProvided.draggableProps}
{...draggableProvided.dragHandleProps}
ref={draggableProvided.innerRef}
sx={{ p: 1, pb: 2 }}
>
<Card
sx={{
backgroundColor: snapshot.isDragging ? 'custom.light' : undefined,
}}
2021-05-30 04:01:49 +04:30
>
<CardActionArea
component={Link}
to={`/manga/${item.chapter.mangaId}`}
sx={{
display: 'flex',
alignItems: 'center',
p: 1,
2021-05-30 04:01:49 +04:30
}}
>
<IconButton sx={{ pointerEvents: 'none' }}>
<DragHandle />
</IconButton>
<Stack sx={{ flex: 1, ml: 1 }} direction="column">
<Typography variant="h6">{item.chapter.manga.title}</Typography>
<Typography variant="caption" display="block" gutterBottom>
{item.chapter.name}
</Typography>
</Stack>
<DownloadStateIndicator download={item} />
<Tooltip title={t('chapter.action.download.delete.label.action')}>
<IconButton
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handleDelete(item.chapter);
}}
size="large"
>
<DeleteIcon />
</IconButton>
</Tooltip>
</CardActionArea>
</Card>
</Box>
)}
</Draggable>
))}
{droppableProvided.placeholder}
</Box>
)}
</StrictModeDroppable>
</DragDropContext>
2021-05-30 04:01:49 +04:30
);
};