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

182 lines
7.2 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
2021-09-09 17:51:22 +04:30
import PauseIcon from '@mui/icons-material/Pause';
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
import Box from '@mui/material/Box';
2021-09-09 17:51:22 +04:30
import IconButton from '@mui/material/IconButton';
2025-05-03 13:24:55 +02:00
import React, { useMemo, useState } from 'react';
import DeleteSweepIcon from '@mui/icons-material/DeleteSweep';
import { closestCenter, DndContext, DragEndEvent } from '@dnd-kit/core';
import { SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable';
2025-05-02 17:56:56 +02:00
import { useWindowEvent } from '@mantine/hooks';
import { useLingui } from '@lingui/react/macro';
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { makeToast } from '@/base/utils/Toast.ts';
import { EmptyViewAbsoluteCentered } from '@/base/components/feedback/EmptyViewAbsoluteCentered.tsx';
import { LoadingPlaceholder } from '@/base/components/feedback/LoadingPlaceholder.tsx';
2024-10-05 16:09:34 +02:00
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
2025-04-25 01:35:07 +02:00
import { DownloaderState } from '@/lib/graphql/generated/graphql.ts';
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
import { DndSortableItem } from '@/lib/dnd-kit/DndSortableItem.tsx';
import { DndKitUtil } from '@/lib/dnd-kit/DndKitUtil.ts';
import { DndOverlayItem } from '@/lib/dnd-kit/DndOverlayItem.tsx';
2025-08-15 22:02:58 +02:00
import { DownloadQueueChapterCard } from '@/features/downloads/components/DownloadQueueChapterCard.tsx';
import { useAppTitle } from '@/features/navigation-bar/hooks/useAppTitle.ts';
import { useAppAction } from '@/features/navigation-bar/hooks/useAppAction.ts';
import { ChapterDownloadStatus } from '@/features/chapter/Chapter.types.ts';
import { VirtuosoPersisted } from '@/lib/virtuoso/Component/VirtuosoPersisted.tsx';
2021-05-30 04:01:49 +04:30
2023-10-28 00:32:02 +02:00
export const DownloadQueue: React.FC = () => {
const { t } = useLingui();
2023-02-14 20:08:03 +03:30
useAppTitle(t`Download queue`);
2025-05-03 13:00:03 +02:00
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;
const queue = downloaderData?.queue ?? [];
2025-04-25 01:35:07 +02:00
const status = downloaderData?.state ?? DownloaderState.Started;
const isQueueEmpty = !queue.length;
2021-05-30 04:01:49 +04:30
const dndItems = useMemo(() => queue.map((download) => download.chapter), [queue]);
const dndSensors = DndKitUtil.useSensorsForDevice();
const [dndActiveDownload, setDndActiveDownload] = useState<ChapterDownloadStatus | null>(null);
const clearQueue = async () => {
try {
await requestManager.clearDownloads().response;
} catch (e) {
makeToast(t`Could not remove all downloads from the queue`, 'error', getErrorMessage(e));
}
};
2021-05-30 04:01:49 +04:30
const toggleQueueStatus = () => {
if (status === DownloaderState.Stopped) {
requestManager.startDownloads();
2021-05-30 04:01:49 +04:30
} else {
requestManager.stopDownloads();
2021-05-30 04:01:49 +04:30
}
};
2025-04-25 01:35:07 +02:00
const categoryReorder = (list: ChapterDownloadStatus[], from: number, to: number) => {
if (from === to) {
return;
}
reorderDownload({ variables: { input: { chapterId: list[from].chapter.id, to } } }).catch(() => {
revertReorder();
});
};
const onDragEnd = (event: DragEndEvent) => {
const { active, over } = event;
setDndActiveDownload(null);
if (!over || active.id === over.id) {
return;
}
const oldIndex = queue.findIndex((download) => download.chapter.id === active.id);
const newIndex = queue.findIndex((download) => download.chapter.id === over.id);
categoryReorder(queue, oldIndex, newIndex);
};
2025-05-03 13:24:55 +02:00
useAppAction(
<>
<CustomTooltip title={t`Delete all`}>
2025-05-03 13:24:55 +02:00
<IconButton onClick={clearQueue} color="inherit">
<DeleteSweepIcon />
</IconButton>
</CustomTooltip>
<CustomTooltip title={status === DownloaderState.Started ? t`Stop` : t`Start`} disabled={isQueueEmpty}>
2025-05-03 13:24:55 +02:00
<IconButton onClick={toggleQueueStatus} disabled={isQueueEmpty} color="inherit">
{status === DownloaderState.Stopped ? <PlayArrowIcon /> : <PauseIcon />}
</IconButton>
</CustomTooltip>
</>,
[status, isQueueEmpty],
2025-05-03 13:24:55 +02:00
);
2021-05-30 04:01:49 +04:30
2025-05-02 17:56:56 +02:00
// Virtuoso's resize observer can throw this error,
// which is caught by DnD and aborts dragging.
useWindowEvent('error', (e) => {
if (
e.message === 'ResizeObserver loop completed with undelivered notifications.' ||
e.message === 'ResizeObserver loop limit exceeded'
) {
e.stopImmediatePropagation();
}
});
if (isLoading) {
return <LoadingPlaceholder />;
}
2024-04-27 22:28:55 +02:00
if (error) {
return (
2024-04-29 15:29:33 +02:00
<EmptyViewAbsoluteCentered
message={t`Unable 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'))}
/>
);
}
if (isQueueEmpty) {
return <EmptyViewAbsoluteCentered message={t`No downloads`} />;
}
2021-05-30 04:01:49 +04:30
return (
2025-04-25 01:35:07 +02:00
<Box sx={{ pb: 1 }}>
<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}>
<VirtuosoPersisted
persistKey="download-queue"
2025-04-25 01:35:07 +02:00
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}
>
<DownloadQueueChapterCard item={queue[index]} status={status} />
</DndSortableItem>
)}
/>
</SortableContext>
<DndOverlayItem isActive={!!dndActiveDownload}>
<DownloadQueueChapterCard item={dndActiveDownload!} status={status} />
</DndOverlayItem>
</DndContext>
</Box>
2021-05-30 04:01:49 +04:30
);
};