/*
* 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 DeleteIcon from '@mui/icons-material/Delete';
import DragHandle from '@mui/icons-material/DragHandle';
import PauseIcon from '@mui/icons-material/Pause';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import { Card, CardActionArea, Stack, Box, Tooltip } from '@mui/material';
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';
import { useTranslation } from 'react-i18next';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { StrictModeDroppable } from '@/lib/StrictModeDroppable';
import { makeToast } from '@/components/util/Toast';
import { NavbarToolbar } from '@/components/navbar/DefaultNavBar';
import { DownloadStateIndicator } from '@/components/molecules/DownloadStateIndicator';
import { EmptyView } from '@/components/util/EmptyView';
import { DownloadType } from '@/lib/graphql/generated/graphql.ts';
import { TChapter } from '@/typings.ts';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
export const DownloadQueue: React.FC = () => {
const { t } = useTranslation();
const { data: downloaderData } = requestManager.useDownloadSubscription();
const queue = (downloaderData?.downloadChanged.queue as DownloadType[]) ?? [];
const status = downloaderData?.downloadChanged.state ?? 'STARTED';
const { setTitle, setAction } = useContext(NavBarContext);
const toggleQueueStatus = () => {
if (status === 'STOPPED') {
requestManager.startDownloads();
} else {
requestManager.stopDownloads();
}
};
useEffect(() => {
setTitle(t('download.queue.title'));
setAction(null);
}, [t]);
const onDragEnd = () => {};
if (queue.length === 0) {
return ;
}
const handleDelete = async (chapter: TChapter) => {
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
requestManager.removeChapterFromDownloadQueue(chapter.id).response,
// delete partial download, should be handle server side?
// bug: The folder and the last image downloaded are not deleted
requestManager.deleteDownloadedChapter(chapter.id).response,
]);
} catch (error) {
makeToast(t('download.queue.error.label.failed_to_remove'), 'error');
}
if (!isRunning) {
return;
}
requestManager.startDownloads().response.catch(() => {});
};
return (
<>
{status === 'STOPPED' ? : }
{(droppableProvided) => (
{queue.map((item, index) => (
{(draggableProvided, snapshot) => (
{item.chapter.manga.title}
{item.chapter.name}
{
e.preventDefault();
e.stopPropagation();
handleDelete(item.chapter);
}}
size="large"
>
)}
))}
{droppableProvided.placeholder}
)}
>
);
};