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 as SButton, ButtonGroup, Checkbox, Label, Loader, PlaceholderLine, TableBody, TableCell, TableFooter, TableHeader, TableHeaderCell, TableRow } from "semantic-ui-react"; import {Button, Header, Modal, Placeholder, Progress, Segment, Table} from "../Theme"; import {useDownloads} from "../../hooks/customHooks"; import { EditArchiveDownloadForm, EditChannelDownloadForm, EditRSSDownloadForm, EditScrapeFilesDownloadForm, EditVideosDownloadForm, EditZimDownloadForm } from "../Download"; import {Downloaders} from "../Vars"; import {SortableTable} from "../SortableTable"; 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 || '...'; const buttonLabel = `${speedText} / ${etaText}`; return <> setOpen(true)} style={{marginLeft: '0.5em'}}> {buttonLabel} setOpen(false)} size='small'> Download Progress {humanFileSize(progress.bytes_downloaded)} / {humanFileSize(progress.total_bytes)} Speed {speedText} ETA {progress.eta || 'Calculating...'} Downloaded {humanFileSize(progress.bytes_downloaded)} / {humanFileSize(progress.total_bytes)} {url && URL {url} } setOpen(false)}>Close >; } 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} Close ; 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} {error && !download.progress && errorModal} {editButton} {restartButton} {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')} ); } if (error && !download.progress) { completedAtCell = ( } > Download Error {error} ) } // Create edit modal for video or archive downloads let editModal = null; 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} {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)); } }; const tableHeaders = [ { key: 'select', text: 0 && selectedIds.length === downloads.length} indeterminate={selectedIds.length > 0 && selectedIds.length < (downloads?.length || 0)} onChange={toggleSelectAll} />, sortBy: null }, {key: 'url', text: 'URL', sortBy: i => i.url.toLowerCase()}, {key: 'completed_at', text: 'Completed At', sortBy: i => i.last_successful_download || ''}, {key: 'control', text: 'Control', sortBy: null}, ]; 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 URL Download Frequency 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 ? {pendingOnceDownloads} : null; return <> Downloads {pendingOnceDownloadsSpan} Recurring Downloads > }
{error}
From: {parentDownloadUrl}