/* eslint-disable @typescript-eslint/no-shadow */ /* eslint-disable react/destructuring-assignment */ /* * 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 NavbarContext from 'components/context/NavbarContext'; import React, { useContext, useEffect } from 'react'; import PlayArrowIcon from '@mui/icons-material/PlayArrow'; import PauseIcon from '@mui/icons-material/Pause'; import IconButton from '@mui/material/IconButton'; import DeleteIcon from '@mui/icons-material/Delete'; import client from 'util/client'; import { DragDropContext, Draggable, DraggingStyle, Droppable, DropResult, NotDraggingStyle, } from 'react-beautiful-dnd'; import { useTheme, Palette } from '@mui/material/styles'; import List from '@mui/material/List'; import DragHandleIcon from '@mui/icons-material/DragHandle'; import ListItem from '@mui/material/ListItem'; import { ListItemIcon } from '@mui/material'; import EmptyView from 'components/util/EmptyView'; import { Box } from '@mui/system'; import Typography from '@mui/material/Typography'; import { useHistory } from 'react-router-dom'; import useSubscription from 'components/library/useSubscription'; const getItemStyle = (isDragging: boolean, draggableStyle: DraggingStyle | NotDraggingStyle | undefined, palette: Palette) => ({ // styles we need to apply on draggables ...draggableStyle, ...(isDragging && { background: palette.mode === 'dark' ? '#424242' : 'rgb(235,235,235)', }), }); const initialQueue = { status: 'Stopped', queue: [], } as IQueue; export default function DownloadQueue() { const { data: queueState } = useSubscription('/api/v1/downloads'); const { queue, status } = queueState ?? initialQueue; const history = useHistory(); const theme = useTheme(); const { setTitle, setAction } = useContext(NavbarContext); const toggleQueueStatus = () => { if (status === 'Stopped') { client.get('/api/v1/downloads/start'); } else { client.get('/api/v1/downloads/stop'); } }; useEffect(() => { setTitle('Download Queue'); setAction(() => { if (status === 'Stopped') { return ( ); } return ( ); }); }, [status]); // eslint-disable-next-line @typescript-eslint/no-unused-vars const onDragEnd = (result: DropResult) => { }; if (queue.length === 0) { return ; } const callDeleteServer = (chapter: IChapter) => { // remove from download queue client.delete(`/api/v1/download/${chapter.mangaId}/chapter/${chapter.index}`); // delete partial download, should be handle server side? // bug: The folder and the last image downloaded are not deleted client.delete(`/api/v1/manga/${chapter.mangaId}/chapter/${chapter.index}`); }; const deleteChapterQueue = (chapter: IChapter) => { // required to stop before deleting otherwise the download kept going. Server issue? client.get('/api/v1/downloads/stop') .then(() => callDeleteServer(chapter)); }; return ( <> {(provided) => ( {queue.map((item, index) => ( {(provided, snapshot) => ( history.push(`/manga/${item.chapter.mangaId}`)} {...provided.draggableProps} {...provided.dragHandleProps} style={getItemStyle( snapshot.isDragging, provided.draggableProps.style, theme.palette, )} ref={provided.innerRef} > {item.manga.title} {`${item.chapter.name} ` + `(${(item.progress * 100).toFixed(2)}%)` + ` => state: ${item.state}`} { // deleteCategory(index); // prevent parent tags from getting the event e.stopPropagation(); // delete chapter from download queue deleteChapterQueue(item.chapter); }} size="large" > )} ))} {provided.placeholder} )} ); }