2023-01-07 17:22:28 -07:00
|
|
|
import React from "react";
|
2022-11-12 14:17:21 -07:00
|
|
|
import {clearCompletedDownloads, clearFailedDownloads, deleteDownload, killDownload, postDownload} from "../../api";
|
2022-09-29 12:02:09 -06:00
|
|
|
import {Link} from "react-router-dom";
|
|
|
|
|
import {
|
2023-07-21 15:14:09 -06:00
|
|
|
APIButton,
|
2022-11-12 14:17:21 -07:00
|
|
|
DisableDownloadsToggle,
|
2022-12-23 16:04:09 -07:00
|
|
|
isoDatetimeToElapsedPopup,
|
2022-09-29 12:02:09 -06:00
|
|
|
secondsToFrequency,
|
|
|
|
|
textEllipsis,
|
2022-10-25 15:57:44 -06:00
|
|
|
useTitle,
|
2022-09-29 12:02:09 -06:00
|
|
|
WROLModeMessage
|
|
|
|
|
} from "../Common";
|
|
|
|
|
import {
|
2023-04-09 17:17:50 -06:00
|
|
|
Button as SButton,
|
2022-09-29 12:02:09 -06:00
|
|
|
Confirm,
|
2022-10-25 15:06:52 -06:00
|
|
|
Loader,
|
2022-09-29 12:02:09 -06:00
|
|
|
PlaceholderLine,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHeader,
|
|
|
|
|
TableHeaderCell,
|
|
|
|
|
TableRow
|
|
|
|
|
} from "semantic-ui-react";
|
2023-04-18 22:03:45 -06:00
|
|
|
import {Button, Header, Modal, ModalActions, ModalContent, ModalHeader, Placeholder, Segment, Table} from "../Theme";
|
2022-09-29 12:02:09 -06:00
|
|
|
import {useDownloads} from "../../hooks/customHooks";
|
2023-01-04 09:14:33 -07:00
|
|
|
import _ from "lodash";
|
2022-09-29 12:02:09 -06:00
|
|
|
|
|
|
|
|
function ClearCompleteDownloads({callback}) {
|
|
|
|
|
const [disabled, setDisabled] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
async function localClearDownloads() {
|
|
|
|
|
setDisabled(true);
|
|
|
|
|
try {
|
|
|
|
|
await clearCompletedDownloads();
|
|
|
|
|
} finally {
|
|
|
|
|
setDisabled(false);
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <>
|
2023-07-21 15:14:09 -06:00
|
|
|
<APIButton
|
2022-09-29 12:02:09 -06:00
|
|
|
onClick={localClearDownloads}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
color='yellow'
|
2023-07-21 15:14:09 -06:00
|
|
|
obeyWROLMode={true}
|
|
|
|
|
>Clear Completed</APIButton>
|
2022-09-29 12:02:09 -06:00
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ClearFailedDownloads({callback}) {
|
|
|
|
|
async function localDeleteFailed() {
|
|
|
|
|
try {
|
|
|
|
|
await clearFailedDownloads();
|
|
|
|
|
} finally {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
return <APIButton
|
|
|
|
|
color='red'
|
|
|
|
|
onClick={localDeleteFailed}
|
|
|
|
|
confirmContent='Are you sure you want to delete failed downloads? They will not be retried.'
|
|
|
|
|
confirmButton='Delete'
|
|
|
|
|
obeyWROLMode={true}
|
|
|
|
|
>
|
|
|
|
|
Clear Failed
|
|
|
|
|
</APIButton>
|
2022-09-29 12:02:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DownloadRow extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
errorModalOpen: false,
|
2022-11-12 14:17:21 -07:00
|
|
|
deleteOpen: false,
|
2022-09-29 12:02:09 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 14:17:21 -07:00
|
|
|
openDelete = (e) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
this.setState({deleteOpen: true});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closeDelete = (e) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
this.setState({deleteOpen: false});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
handleDelete = async () => {
|
2022-11-12 14:17:21 -07:00
|
|
|
const {id} = this.props;
|
|
|
|
|
try {
|
|
|
|
|
await deleteDownload(id);
|
|
|
|
|
} finally {
|
|
|
|
|
this.closeDelete();
|
2022-11-13 14:36:13 -07:00
|
|
|
if (this.props.fetchDownloads) {
|
|
|
|
|
this.props.fetchDownloads();
|
|
|
|
|
}
|
2022-11-12 14:17:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-09-29 12:02:09 -06:00
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
let {url, frequency, last_successful_download, status, location, next_download, error} = this.props;
|
2022-11-12 14:17:21 -07:00
|
|
|
const {errorModalOpen, deleteOpen} = this.state;
|
2022-09-29 12:02:09 -06:00
|
|
|
|
|
|
|
|
const link = location ?
|
|
|
|
|
(text) => <Link to={location}>{text}</Link> :
|
|
|
|
|
(text) => <a href={url} target='_blank' rel='noopener noreferrer'>{text}</a>;
|
|
|
|
|
|
|
|
|
|
const errorModal = <Modal
|
|
|
|
|
closeIcon
|
|
|
|
|
onClose={() => this.setState({errorModalOpen: false})}
|
|
|
|
|
onOpen={() => this.setState({errorModalOpen: true})}
|
|
|
|
|
open={errorModalOpen}
|
2022-11-12 14:17:21 -07:00
|
|
|
trigger={<Button icon='exclamation circle' color='orange'/>}
|
2022-09-29 12:02:09 -06:00
|
|
|
>
|
2023-04-18 22:03:45 -06:00
|
|
|
<ModalHeader>Download Error</ModalHeader>
|
|
|
|
|
<ModalContent>
|
2022-09-29 12:02:09 -06:00
|
|
|
<pre style={{overflowX: 'scroll'}}>{error}</pre>
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalContent>
|
|
|
|
|
<ModalActions>
|
2023-04-09 17:17:50 -06:00
|
|
|
<SButton onClick={() => this.setState({errorModalOpen: false})}>Close</SButton>
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalActions>
|
2022-09-29 12:02:09 -06:00
|
|
|
</Modal>;
|
|
|
|
|
|
2022-11-12 14:17:21 -07:00
|
|
|
const deleteButton = <>
|
2023-07-21 15:14:09 -06:00
|
|
|
<APIButton
|
|
|
|
|
color='red'
|
|
|
|
|
icon='trash'
|
|
|
|
|
confirmContent='Are you sure you want to delete this download?'
|
2022-11-12 14:17:21 -07:00
|
|
|
confirmButton='Delete'
|
2023-07-21 15:14:09 -06:00
|
|
|
onClick={this.handleDelete}
|
|
|
|
|
obeyWROLMode={true}
|
2022-11-12 14:17:21 -07:00
|
|
|
/>
|
|
|
|
|
</>;
|
|
|
|
|
|
2023-04-02 12:19:15 -06:00
|
|
|
// Show "now" if we have passed the next_download.
|
|
|
|
|
let next = 'now';
|
|
|
|
|
if (next_download) {
|
|
|
|
|
if (new Date() < new Date(next_download)) {
|
|
|
|
|
next = isoDatetimeToElapsedPopup(next_download);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:09:33 -06:00
|
|
|
return <TableRow>
|
2022-09-29 12:02:09 -06:00
|
|
|
<TableCell>
|
|
|
|
|
{link(textEllipsis(url, 50))}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>{secondsToFrequency(frequency)}</TableCell>
|
2022-10-25 15:09:33 -06:00
|
|
|
<TableCell>
|
2022-12-23 16:04:09 -07:00
|
|
|
{last_successful_download ? isoDatetimeToElapsedPopup(last_successful_download) : null}
|
2022-10-25 15:09:33 -06:00
|
|
|
{status === 'pending' ? <Loader active inline size='tiny'/> : null}
|
|
|
|
|
</TableCell>
|
2023-04-02 12:19:15 -06:00
|
|
|
<TableCell>{next}</TableCell>
|
2022-11-12 14:17:21 -07:00
|
|
|
<TableCell>
|
|
|
|
|
{error && errorModal}
|
|
|
|
|
{deleteButton}
|
|
|
|
|
</TableCell>
|
2022-09-29 12:02:09 -06:00
|
|
|
</TableRow>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class StoppableRow extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
errorModalOpen: false,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
handleDelete = async () => {
|
2023-04-01 16:09:05 -06:00
|
|
|
await deleteDownload(this.props.id);
|
|
|
|
|
await this.props.fetchDownloads();
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
handleStop = async () => {
|
2022-09-29 12:02:09 -06:00
|
|
|
await killDownload(this.props.id);
|
|
|
|
|
await this.props.fetchDownloads();
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
handleStart = async () => {
|
2023-04-01 15:34:46 -06:00
|
|
|
let downloader = this.props.downloader;
|
2022-09-29 12:02:09 -06:00
|
|
|
await postDownload(`${this.props.url}`, downloader);
|
|
|
|
|
await this.props.fetchDownloads();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
let {url, last_successful_download, status, location, error} = this.props;
|
2023-07-21 15:14:09 -06:00
|
|
|
let {errorModalOpen} = this.state;
|
2022-09-29 12:02:09 -06:00
|
|
|
|
2023-04-18 22:03:45 -06:00
|
|
|
// Open downloads (/download), or external links in an anchor.
|
|
|
|
|
const link = location && !location.startsWith('/download') ?
|
2022-09-29 12:02:09 -06:00
|
|
|
(text) => <Link to={location}>{text}</Link> :
|
|
|
|
|
(text) => <a href={url} target='_blank' rel='noopener noreferrer'>{text}</a>;
|
|
|
|
|
|
2022-12-23 16:04:09 -07:00
|
|
|
let completedAtCell = last_successful_download ? isoDatetimeToElapsedPopup(last_successful_download) : null;
|
2022-09-29 12:02:09 -06:00
|
|
|
let buttonCell = <TableCell/>;
|
|
|
|
|
if (status === 'pending' || status === 'new') {
|
|
|
|
|
buttonCell = (
|
|
|
|
|
<TableCell>
|
2023-07-21 15:14:09 -06:00
|
|
|
<APIButton
|
2022-09-29 12:02:09 -06:00
|
|
|
color='red'
|
2023-07-21 15:14:09 -06:00
|
|
|
onClick={this.handleStop}
|
|
|
|
|
confirmContent='Are you sure you want to stop this download? It will not be retried.'
|
2022-09-29 12:02:09 -06:00
|
|
|
confirmButton='Stop'
|
2023-07-21 15:14:09 -06:00
|
|
|
obeyWROLMode={true}
|
|
|
|
|
>Stop</APIButton>
|
2022-09-29 12:02:09 -06:00
|
|
|
</TableCell>
|
|
|
|
|
);
|
|
|
|
|
} else if (status === 'failed' || status === 'deferred') {
|
|
|
|
|
buttonCell = (
|
|
|
|
|
<TableCell>
|
2023-07-21 15:14:09 -06:00
|
|
|
<APIButton
|
|
|
|
|
color='red'
|
|
|
|
|
icon='trash'
|
|
|
|
|
onClick={this.handleDelete}
|
|
|
|
|
confirmContent='Are you sure you want to delete this download?'
|
2023-04-01 16:09:05 -06:00
|
|
|
confirmButton='Delete'
|
2023-07-21 15:14:09 -06:00
|
|
|
obeyWROLMode={true}
|
2022-09-29 12:02:09 -06:00
|
|
|
/>
|
2023-07-21 15:14:09 -06:00
|
|
|
<APIButton
|
|
|
|
|
color='green'
|
|
|
|
|
confirmContent='Are you sure you want to restart this download?'
|
2022-09-29 12:02:09 -06:00
|
|
|
confirmButton='Start'
|
2023-07-21 15:14:09 -06:00
|
|
|
onClick={this.handleStart}
|
|
|
|
|
obeyWROLMode={true}
|
|
|
|
|
>Retry</APIButton>
|
2022-09-29 12:02:09 -06:00
|
|
|
</TableCell>
|
|
|
|
|
);
|
|
|
|
|
} else if (status === 'complete' && location) {
|
|
|
|
|
buttonCell = (
|
|
|
|
|
<TableCell>
|
|
|
|
|
{link('View')}
|
|
|
|
|
</TableCell>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
|
|
|
|
completedAtCell = (
|
|
|
|
|
<Modal
|
|
|
|
|
closeIcon
|
|
|
|
|
trigger={<Button icon='exclamation circle' color='red'/>}
|
|
|
|
|
>
|
2023-04-18 22:03:45 -06:00
|
|
|
<ModalHeader>Download Error</ModalHeader>
|
|
|
|
|
<ModalContent>
|
2022-09-29 12:02:09 -06:00
|
|
|
<pre style={{overflowX: 'scroll'}}>{error}</pre>
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalContent>
|
2022-09-29 12:02:09 -06:00
|
|
|
</Modal>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-17 16:09:24 -07:00
|
|
|
return <TableRow>
|
|
|
|
|
<TableCell>
|
|
|
|
|
{link(textEllipsis(url, 50))}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
{completedAtCell}
|
|
|
|
|
{status === 'pending' ? <Loader active inline size='tiny'/> : null}
|
|
|
|
|
</TableCell>
|
|
|
|
|
{buttonCell}
|
|
|
|
|
</TableRow>
|
2022-09-29 12:02:09 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 17:22:28 -07:00
|
|
|
export function OnceDownloadsTable({downloads, fetchDownloads}) {
|
|
|
|
|
if (downloads && _.isEmpty(downloads)) {
|
|
|
|
|
return <Segment>No downloads are scheduled.</Segment>;
|
|
|
|
|
} else if (downloads) {
|
|
|
|
|
return <>
|
|
|
|
|
<ClearCompleteDownloads callback={fetchDownloads}/>
|
|
|
|
|
<ClearFailedDownloads callback={fetchDownloads}/>
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableHeaderCell>URL</TableHeaderCell>
|
|
|
|
|
<TableHeaderCell>Completed At</TableHeaderCell>
|
|
|
|
|
<TableHeaderCell>Control</TableHeaderCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{downloads.map(i => <StoppableRow fetchDownloads={fetchDownloads} key={i.id} {...i}/>)}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</>;
|
|
|
|
|
} else {
|
|
|
|
|
return <Placeholder>
|
|
|
|
|
<PlaceholderLine/>
|
|
|
|
|
<PlaceholderLine/>
|
|
|
|
|
</Placeholder>;
|
2022-09-29 12:02:09 -06:00
|
|
|
}
|
2023-01-07 17:22:28 -07:00
|
|
|
}
|
2022-09-29 12:02:09 -06:00
|
|
|
|
2023-01-07 17:22:28 -07:00
|
|
|
export function RecurringDownloadsTable({downloads, fetchDownloads}) {
|
|
|
|
|
if (downloads && _.isEmpty(downloads)) {
|
|
|
|
|
return <Segment>No downloads are scheduled.</Segment>;
|
|
|
|
|
} else if (downloads) {
|
|
|
|
|
return <Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableHeaderCell>URL</TableHeaderCell>
|
|
|
|
|
<TableHeaderCell>Download Frequency</TableHeaderCell>
|
|
|
|
|
<TableHeaderCell>Completed At</TableHeaderCell>
|
|
|
|
|
<TableHeaderCell>Next</TableHeaderCell>
|
|
|
|
|
<TableHeaderCell>Control</TableHeaderCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
2022-09-29 12:02:09 -06:00
|
|
|
<TableBody>
|
2023-01-07 17:22:28 -07:00
|
|
|
{downloads.map(i => <DownloadRow key={i.id} fetchDownloads={fetchDownloads} {...i}/>)}
|
2022-09-29 12:02:09 -06:00
|
|
|
</TableBody>
|
|
|
|
|
</Table>;
|
2023-01-07 17:22:28 -07:00
|
|
|
} else {
|
|
|
|
|
return <Placeholder>
|
|
|
|
|
<PlaceholderLine/>
|
|
|
|
|
<PlaceholderLine/>
|
|
|
|
|
</Placeholder>;
|
2022-09-29 12:02:09 -06:00
|
|
|
}
|
2023-01-07 17:22:28 -07:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
export function DownloadsPage() {
|
2023-01-07 17:22:28 -07:00
|
|
|
useTitle('Downloads');
|
|
|
|
|
|
|
|
|
|
const {onceDownloads, recurringDownloads, fetchDownloads} = useDownloads();
|
2022-09-29 12:02:09 -06:00
|
|
|
|
|
|
|
|
return <>
|
|
|
|
|
<WROLModeMessage content='Downloads are disabled because WROL Mode is enabled.'/>
|
|
|
|
|
<DisableDownloadsToggle/>
|
2023-01-07 17:22:28 -07:00
|
|
|
|
2022-09-29 12:02:09 -06:00
|
|
|
<Header as='h1'>Downloads</Header>
|
2023-01-07 17:22:28 -07:00
|
|
|
<OnceDownloadsTable downloads={onceDownloads} fetchDownloads={fetchDownloads}/>
|
2022-09-29 12:02:09 -06:00
|
|
|
|
|
|
|
|
<Header as='h1'>Recurring Downloads</Header>
|
2023-01-07 17:22:28 -07:00
|
|
|
<RecurringDownloadsTable downloads={recurringDownloads} fetchDownloads={fetchDownloads}/>
|
2022-09-29 12:02:09 -06:00
|
|
|
</>
|
|
|
|
|
}
|