wrolpi/app/src/components/admin/Downloads.js

342 lines
11 KiB
JavaScript
Raw Normal View History

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