2021-05-30 04:01:49 +04:30
|
|
|
/* 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/. */
|
|
|
|
|
|
2021-10-30 16:10:34 +03:30
|
|
|
import NavbarContext from 'components/context/NavbarContext';
|
2022-11-02 21:48:22 +01:00
|
|
|
import React, { useContext, useEffect } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
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';
|
2021-05-30 04:01:49 +04:30
|
|
|
import client from 'util/client';
|
|
|
|
|
import {
|
|
|
|
|
DragDropContext, Draggable, DraggingStyle, Droppable, DropResult, NotDraggingStyle,
|
|
|
|
|
} from 'react-beautiful-dnd';
|
2021-09-09 17:51:22 +04:30
|
|
|
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';
|
2021-10-30 16:10:34 +03:30
|
|
|
import EmptyView from 'components/util/EmptyView';
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2022-03-03 21:05:14 +00:00
|
|
|
import { Box } from '@mui/system';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-11-02 21:48:22 +01:00
|
|
|
import useSubscription from 'components/library/useSubscription';
|
2021-05-30 04:01:49 +04:30
|
|
|
|
|
|
|
|
const getItemStyle = (isDragging: boolean,
|
|
|
|
|
draggableStyle: DraggingStyle | NotDraggingStyle | undefined, palette: Palette) => ({
|
|
|
|
|
// styles we need to apply on draggables
|
|
|
|
|
...draggableStyle,
|
|
|
|
|
|
|
|
|
|
...(isDragging && {
|
2021-09-09 17:51:22 +04:30
|
|
|
background: palette.mode === 'dark' ? '#424242' : 'rgb(235,235,235)',
|
2021-05-30 04:01:49 +04:30
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const initialQueue = {
|
|
|
|
|
status: 'Stopped',
|
|
|
|
|
queue: [],
|
|
|
|
|
} as IQueue;
|
|
|
|
|
|
|
|
|
|
export default function DownloadQueue() {
|
2022-11-02 21:48:22 +01:00
|
|
|
const { data: queueState } = useSubscription<IQueue>('/api/v1/downloads');
|
|
|
|
|
const { queue, status } = queueState ?? initialQueue;
|
2021-05-30 04:01:49 +04:30
|
|
|
|
2022-03-03 21:05:14 +00:00
|
|
|
const history = useHistory();
|
|
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
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 (
|
2021-09-09 17:51:22 +04:30
|
|
|
<IconButton onClick={toggleQueueStatus} size="large">
|
2021-05-30 04:01:49 +04:30
|
|
|
<PlayArrowIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
2021-09-09 17:51:22 +04:30
|
|
|
<IconButton onClick={toggleQueueStatus} size="large">
|
2021-05-30 04:01:49 +04:30
|
|
|
<PauseIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}, [status]);
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
|
const onDragEnd = (result: DropResult) => {
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-29 17:51:10 +05:30
|
|
|
if (queue.length === 0) {
|
|
|
|
|
return <EmptyView message="No downloads" />;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 09:29:43 -04:00
|
|
|
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));
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<DragDropContext onDragEnd={onDragEnd}>
|
|
|
|
|
<Droppable droppableId="droppable">
|
|
|
|
|
{(provided) => (
|
|
|
|
|
<List ref={provided.innerRef}>
|
|
|
|
|
{queue.map((item, index) => (
|
|
|
|
|
<Draggable
|
|
|
|
|
key={`${item.mangaId}-${item.chapterIndex}`}
|
|
|
|
|
draggableId={`${item.mangaId}-${item.chapterIndex}`}
|
|
|
|
|
index={index}
|
|
|
|
|
>
|
|
|
|
|
{(provided, snapshot) => (
|
|
|
|
|
<ListItem
|
|
|
|
|
ContainerProps={{ ref: provided.innerRef } as any}
|
2022-03-03 21:05:14 +00:00
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
justifyContent: 'flex-start',
|
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
|
padding: 2,
|
|
|
|
|
margin: '10px',
|
|
|
|
|
'&:hover': {
|
|
|
|
|
backgroundColor: 'action.hover',
|
|
|
|
|
transition: 'background-color 100ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
|
|
|
|
|
},
|
|
|
|
|
'&:active': {
|
|
|
|
|
backgroundColor: 'action.selected',
|
|
|
|
|
transition: 'background-color 100ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
onClick={() => history.push(`/manga/${item.chapter.mangaId}`)}
|
2021-05-30 04:01:49 +04:30
|
|
|
{...provided.draggableProps}
|
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
|
style={getItemStyle(
|
|
|
|
|
snapshot.isDragging,
|
|
|
|
|
provided.draggableProps.style,
|
|
|
|
|
theme.palette,
|
|
|
|
|
)}
|
|
|
|
|
ref={provided.innerRef}
|
|
|
|
|
>
|
2022-03-03 21:05:14 +00:00
|
|
|
<ListItemIcon sx={{ margin: 'auto 0' }}>
|
2021-05-30 04:01:49 +04:30
|
|
|
<DragHandleIcon />
|
|
|
|
|
</ListItemIcon>
|
2022-03-03 21:05:14 +00:00
|
|
|
<Box sx={{ display: 'flex' }}>
|
|
|
|
|
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
|
|
|
|
|
<Typography variant="h5" component="h2">
|
|
|
|
|
{item.manga.title}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="caption" display="block" gutterBottom>
|
|
|
|
|
{`${item.chapter.name} `
|
|
|
|
|
+ `(${(item.progress * 100).toFixed(2)}%)`
|
|
|
|
|
+ ` => state: ${item.state}`}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
2021-05-30 04:01:49 +04:30
|
|
|
<IconButton
|
2022-03-03 21:05:14 +00:00
|
|
|
sx={{ marginLeft: 'auto' }}
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
// deleteCategory(index);
|
|
|
|
|
// prevent parent tags from getting the event
|
|
|
|
|
e.stopPropagation();
|
2022-06-16 09:29:43 -04:00
|
|
|
|
|
|
|
|
// delete chapter from download queue
|
|
|
|
|
deleteChapterQueue(item.chapter);
|
2021-05-30 04:01:49 +04:30
|
|
|
}}
|
2021-09-09 17:51:22 +04:30
|
|
|
size="large"
|
2021-05-30 04:01:49 +04:30
|
|
|
>
|
|
|
|
|
<DeleteIcon />
|
2021-08-19 19:31:39 +04:30
|
|
|
</IconButton>
|
2021-05-30 04:01:49 +04:30
|
|
|
</ListItem>
|
|
|
|
|
)}
|
|
|
|
|
</Draggable>
|
2022-03-03 21:05:14 +00:00
|
|
|
|
2021-05-30 04:01:49 +04:30
|
|
|
))}
|
|
|
|
|
{provided.placeholder}
|
|
|
|
|
</List>
|
|
|
|
|
)}
|
|
|
|
|
</Droppable>
|
|
|
|
|
</DragDropContext>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|