import React from "react";
import {
batchClearDownloads,
batchDeleteDownloads,
batchRetryDownloads,
clearCompletedDownloads,
deleteDownload,
deleteOnceDownloads,
killDownload,
restartDownload,
retryOnceDownloads
} from "../../api";
import {Link} from "react-router";
import {
APIButton,
CookiesLockedMessage,
DisableDownloadsToggle,
DailyLimitMessage,
DownloadWindowMessage,
ErrorMessage,
formatFrequency,
humanBandwidth,
humanFileSize,
isoDatetimeToElapsedPopup,
useLocalStorage,
useTitle,
WROLModeMessage
} from "../Common";
import {
Button,
ButtonGroup,
Checkbox,
Header,
IconButton,
Label,
Loader,
Modal,
Panel,
Placeholder,
Progress,
Table,
} from "../ui";
import {useDownloads} from "../../hooks/customHooks";
import {
EditArchiveDownloadForm,
EditChannelDownloadForm,
EditRSSDownloadForm,
EditScrapeFilesDownloadForm,
EditVideosDownloadForm,
EditZimDownloadForm
} from "../Download";
import {Downloaders} from "../Vars";
import {SortableTable} from "../SortableTable";
// A header too long for a phone. Both forms are rendered and `.label-*` in App.css shows
// one; the mobile column widths are sized for the short word.
const shortHeader = (short, full) => <>
{short}{full}
>;
function DownloadProgressModal({progress, url}) {
const [open, setOpen] = React.useState(false);
const [navColor] = useLocalStorage('nav_color', 'violet');
const speedText = humanBandwidth(progress.speed);
const etaText = progress.eta || '...';
// A percent: this button sits in a column sized for its header, and speed-and-ETA does not
// fit there. The modal it opens carries the detail, and `title` the full text.
const buttonLabel = Number.isFinite(progress.percent) ? `${Math.round(progress.percent)}%` : '...';
return <>
setOpen(false)} size='fullscreen'>
Download Progress
)}
);
}
return onSelect(id)}
/>
{link(url)}
{completedAtCell}
{status === 'pending' && !download.progress ? : null}
{download.progress && }
{buttonCell}
{errorModal}
{editModal}
}
export function OnceDownloadsTable({downloads, fetchDownloads}) {
const [selectedIds, setSelectedIds] = React.useState([]);
// Clear selection when downloads change (e.g., after deletion)
React.useEffect(() => {
if (downloads) {
// Remove any selected IDs that no longer exist
const downloadIds = new Set(downloads.map(d => d.id));
setSelectedIds(prev => prev.filter(id => downloadIds.has(id)));
}
}, [downloads]);
const onSelect = (id) => {
if (selectedIds.includes(id)) {
setSelectedIds(selectedIds.filter(i => i !== id));
} else {
setSelectedIds([...selectedIds, id]);
}
};
const clearSelection = () => setSelectedIds([]);
const toggleSelectAll = () => {
if (downloads && selectedIds.length === downloads.length) {
setSelectedIds([]);
} else if (downloads) {
setSelectedIds(downloads.map(d => d.id));
}
};
// Widths come from the `col-*` classes in App.css, which change at the tablet breakpoint.
// Only the URL names none: it takes the remainder and truncates.
const tableHeaders = [
{
key: 'select',
text: 0 && selectedIds.length === downloads.length}
indeterminate={selectedIds.length > 0 && selectedIds.length < (downloads?.length || 0)}
onChange={toggleSelectAll}
/>,
sortBy: null,
className: 'col-select',
},
{key: 'url', text: 'URL', sortBy: i => i.url.toLowerCase()},
{key: 'completed_at', text: shortHeader('Done', 'Completed At'),
sortBy: i => i.last_successful_download || '', className: 'col-completed'},
{key: 'control', text: 'Control', sortBy: null, className: 'col-control-group'},
];
const rowFunc = (download) => (
);
const footer =
;
if (downloads && downloads.length >= 1) {
return
} else if (downloads) {
return No downloads are scheduled.
} else if (downloads === undefined) {
return Unable to fetch downloads
}
return
}
export function RecurringDownloadsTable({downloads, fetchDownloads, onDelete}) {
if (downloads && downloads.length >= 1) {
return
{/* Widths come from the `col-*` classes in App.css, not percentages: what each
column holds is a fixed amount of text or a fixed number of buttons, and a
percentage of a phone's width is not that. Only the URL names none -- it
takes the remainder and truncates. */}
URL
{shortHeader('Freq', 'Download Frequency')}
{shortHeader('Done', 'Completed At')}
NextControl
{downloads.map(i => {
return
})}
} else if (downloads) {
return No downloads are scheduled.
} else if (downloads === undefined) {
return Unable to fetch downloads
}
return
}
export function DownloadsPage() {
useTitle('Downloads');
const {onceDownloads, recurringDownloads, pendingOnceDownloads, fetchDownloads} = useDownloads();
const pendingOnceDownloadsSpan = pendingOnceDownloads > 0 ?
: null;
return <>
Downloads {pendingOnceDownloadsSpan}Recurring Downloads
>
}