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 Speed {speedText} ETA {progress.eta || 'Calculating...'} Downloaded {humanFileSize(progress.bytes_downloaded)} / {humanFileSize(progress.total_bytes)} {url && URL {url} }
; } function ClearDownloadsButton({callback, selectedIds, clearSelection}) { const hasSelection = selectedIds && selectedIds.length > 0; async function localClearDownloads() { try { if (hasSelection) { await batchClearDownloads(selectedIds); if (clearSelection) clearSelection(); } else { await clearCompletedDownloads(); } } finally { if (callback) { callback() } } } const label = hasSelection ? `Clear (${selectedIds.length})` : 'Clear'; return <> {label} } function RetryDownloadsButton({callback, selectedIds, clearSelection}) { const hasSelection = selectedIds && selectedIds.length > 0; async function localRetryOnce() { try { if (hasSelection) { await batchRetryDownloads(selectedIds); if (clearSelection) clearSelection(); } else { await retryOnceDownloads(); } } finally { if (callback) { callback() } } } const label = hasSelection ? `Retry (${selectedIds.length})` : 'Retry'; return {label} } function DeleteOnceDownloadsButton({callback, selectedIds, clearSelection}) { const hasSelection = selectedIds && selectedIds.length > 0; async function localDeleteOnce() { try { if (hasSelection) { await batchDeleteDownloads(selectedIds); if (clearSelection) clearSelection(); } else { await deleteOnceDownloads(); } } finally { if (callback) { callback() } } } const label = hasSelection ? `Delete (${selectedIds.length})` : 'Delete'; const confirmContent = hasSelection ? `Are you sure you want to delete ${selectedIds.length} download(s)?` : 'Are you sure you want to delete all downloads? Some may be retried!'; return {label} } function RecurringDownloadRow({download, fetchDownloads, onDelete}) { const [errorModalOpen, setErrorModalOpen] = React.useState(false); const [editModalOpen, setEditModalOpen] = React.useState(false); const handleRestart = async () => { const {id} = download; try { await restartDownload(id); } finally { if (fetchDownloads) { await fetchDownloads(); } } } const handleEditOpen = () => setEditModalOpen(true); const handleEditClose = () => setEditModalOpen(false); const handleErrorOpen = () => setErrorModalOpen(true); const handleErrorClose = () => setErrorModalOpen(false); let { url, frequency, last_successful_download, status, location, next_download, error, downloader, } = download; const link = location ? (text) => {text} : (text) => {text}; const errorModal = Download Error
{error}
; const errorTrigger = ; const hideEdit = downloader === Downloaders.MapCatalog || downloader === Downloaders.MapExtract; const editButton = hideEdit ? null : ; const restartButton = ; // Show "now" if we have passed the next_download. let next = 'now'; if (next_download && new Date() < new Date(next_download)) { next = isoDatetimeToElapsedPopup(next_download); } const onSuccess = async () => { if (fetchDownloads) { await fetchDownloads(); } handleEditClose(); } const localOnDelete = async () => { try { await deleteDownload(download.id); await onDelete(); } finally { await fetchDownloads(); handleEditClose(); } } let editForm; if (downloader === Downloaders.VideoChannel) { editForm = ; } else if (downloader === Downloaders.RSS) { editForm = ; } else if (downloader === Downloaders.KiwixCatalog) { editForm = ; } else if (downloader === Downloaders.ScrapeHtml) { editForm = ; } const editModal = Edit Download {editForm} ; return {link(url)} {formatFrequency(frequency)} {last_successful_download && isoDatetimeToElapsedPopup(last_successful_download)} {status === 'pending' && !download.progress && } {download.progress && } {next} {/* `justify-content` rather than the cell's `text-align: right`, which a flex row does not answer to. These two are icon buttons and were sharing an edge. */}
{error && !download.progress && errorTrigger} {editButton} {restartButton}
{errorModal} {editModal}
} function OnceDownloadRow({download, fetchDownloads, isSelected, onSelect}) { const [editModalOpen, setEditModalOpen] = React.useState(false); const {url, last_successful_download, status, location, error, downloader, settings, id, tag_names} = download; const parentDownloadUrl = settings?.parent_download_url; const handleDelete = async () => { try { await deleteDownload(id); } finally { await fetchDownloads(); } }; const handleStop = async () => { try { await killDownload(id); } finally { await fetchDownloads(); } }; const handleRestart = async () => { try { await restartDownload(id); } finally { await fetchDownloads(); } }; const handleEditOpen = () => setEditModalOpen(true); const handleEditClose = () => setEditModalOpen(false); const handleEditSuccess = async () => { await fetchDownloads(); handleEditClose(); }; // Open downloads (/download), or external links in an anchor. const link = location && !location.startsWith('/download') ? (text) => {text} : (text) => {text}; let completedAtCell = last_successful_download ? isoDatetimeToElapsedPopup(last_successful_download) : null; let buttonCell = ; if (status === 'pending' || status === 'new') { buttonCell = ( {status === 'new' && (downloader === Downloaders.Video || downloader === Downloaders.Archive) && ( )} ); } else if (status === 'failed' || status === 'deferred') { buttonCell = ( {(downloader === Downloaders.Video || downloader === Downloaders.Archive) && status !== 'pending' && ( )} ); } else if (status === 'complete' && location) { buttonCell = ( {link('View')} ); } // Create edit modal for video or archive downloads let editModal = null; let errorModal = null; const [errorModalOpen, setErrorModalOpen] = React.useState(false); if (error && !download.progress) { completedAtCell = ( setErrorModalOpen(true)}/> ) errorModal = setErrorModalOpen(false)} > Download Error
{error}
} if (downloader === Downloaders.Video) { editModal = ( Edit Video Download {parentDownloadUrl && (

From: {parentDownloadUrl}

)}
); } else if (downloader === Downloaders.Archive) { editModal = ( Edit Archive Download {parentDownloadUrl && (

From: {parentDownloadUrl}

)}
); } 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')} Next Control {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
}