Feature/127 archive UI rework (#131)

* Archives have their own page.  Delete/etc buttons are moved into ArchivePage.

* Alternatives for an Archive are displayed in the ArchivePage.

* ArchiveDownloader returns the location.

* Removing color from Details button.

* Making Archive screenshot and title a link.

* Preserving next_download when updating a Channel.

* Removing button icons from Archive page.
This commit is contained in:
lrnselfreliance 2022-05-16 17:56:55 -06:00 • committed by Roland
parent 2314c3462f
commit 0765890b8b
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
12 changed files with 238 additions and 99 deletions

View file

@ -430,6 +430,21 @@ export async function fetchDomains() {
}
}
export async function getArchive(archiveId) {
const response = await apiGet(`${ARCHIVES_API}/${archiveId}`);
if (response.status === 200) {
const data = await response.json();
return [data['archive'], data['alternatives']];
} else {
toast({
type: 'error',
title: 'Archive Error',
description: 'Unable to get Archive. See server logs.',
time: 5000,
});
}
}
export async function postDownload(urls, downloader) {
let body = {urls: urls, downloader: downloader};
let response = await apiPost(`${API_URI}/download`, body);

View file

@ -460,7 +460,7 @@ class StoppableRow extends React.Component {
open={errorModalOpen}
trigger={<Button icon='exclamation circle' color='red'/>}
>
<Modal.Header>Error</Modal.Header>
<Modal.Header>Download Error</Modal.Header>
<Modal.Content>
<pre>{error}</pre>
</Modal.Content>

View file

@ -1,8 +1,9 @@
import React, {useState} from "react";
import {Card, Confirm, Container, Icon, Image, Placeholder} from "semantic-ui-react";
import {Card, Confirm, Container, Header, Image, Loader, Placeholder, Segment} from "semantic-ui-react";
import {
CardGroupCentered,
ClearButton,
HelpHeader,
PageContainer,
Paginator,
SearchInput,
@ -13,75 +14,115 @@ import {
} from "./Common";
import {deleteArchive, postDownload, refreshArchives} from "../api";
import Button from "semantic-ui-react/dist/commonjs/elements/Button";
import {Link, Route} from "react-router-dom";
import {Link, Route, Switch, useHistory} from "react-router-dom";
import {ArchivePlaceholder} from "./Placeholder";
import Table from "semantic-ui-react/dist/commonjs/collections/Table";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
import {useArchives, useDomains} from "../hooks/customHooks";
import {useArchive, useArchives, useDomains} from "../hooks/customHooks";
function ArchiveCard({archive, syncArchive, deleteArchive, setDomain}) {
const [syncOpen, setSyncOpen] = useState(false);
function ArchivePage(props) {
const [deleteOpen, setDeleteOpen] = useState(false);
const [syncOpen, setSyncOpen] = useState(false);
const history = useHistory();
const archiveId = props.match.params.archive_id;
const [archive, alternatives] = useArchive(archiveId);
const localSyncArchive = async () => {
setSyncOpen(false);
await syncArchive(archive.url);
if (archive === null) {
return <Segment><Loader active/></Segment>;
}
if (archive === undefined) {
return <>
<Header as='h2'>Unknown archive</Header>
This archive does not exist
</>
}
const singlefileUrl = archive.singlefile_path ? `/media/${encodeURIComponent(archive.singlefile_path)}` : null;
const screenshotUrl = archive.screenshot_path ? `/media/${encodeURIComponent(archive.screenshot_path)}` : null;
const readabilityUrl = archive.readability_path ? `/media/${encodeURIComponent(archive.readability_path)}` : null;
const singlefileButton = <a href={singlefileUrl} target='_blank'>
<Button content='View' color='blue'/>
</a>;
const readabilityLink = readabilityUrl ?
<a href={readabilityUrl} target='_blank'><Button content='Article'/></a> :
<Button disabled content='Article'/>;
const screenshot = screenshotUrl ?
<Image src={screenshotUrl} size='large' style={{marginTop: '1em', marginBottom: '1em'}}/> :
null;
const localDeleteArchive = async () => {
setDeleteOpen(false);
await deleteArchive(archive.id);
}
let imageSrc = archive.screenshot_path ? `/media/${encodeURIComponent(archive.screenshot_path)}` : null;
let singlefileUrl = archive.singlefile_path ? `/media/${encodeURIComponent(archive.singlefile_path)}` : null;
let readabilityUrl = archive.readability_path ? `/media/${encodeURIComponent(archive.readability_path)}` : null;
let readabilityIcon = <Button icon disabled><Icon name='book' size='large'/></Button>;
if (readabilityUrl) {
readabilityIcon = (
<Button icon href={readabilityUrl} target='_blank' rel='noopener noreferrer'>
<Icon name='book' size='large'/>
</Button>);
const localSyncArchive = async () => {
setSyncOpen(false);
await postDownload(archive.url, 'archive');
}
let syncIcon = null;
if (syncArchive) {
syncIcon = (<>
<Button icon onClick={() => setSyncOpen(true)}>
<Icon name='sync' size='large'/>
</Button>
<Confirm
open={syncOpen}
content='Download the latest version of this URL?'
confirmButton='Confirm'
onCancel={() => setSyncOpen(false)}
onConfirm={localSyncArchive}
/>
</>);
const syncButton = (<>
<Button color='yellow' onClick={() => setSyncOpen(true)} content='Sync'/>
<Confirm
open={syncOpen}
content='Download the latest version of this URL?'
confirmButton='Sync'
onCancel={() => setSyncOpen(false)}
onConfirm={localSyncArchive}
/>
</>);
const deleteButton = (<>
<Button color='red' onClick={() => setDeleteOpen(true)} content='Delete' aria-label='Delete'/>
<Confirm
open={deleteOpen}
content='Are you sure you want to delete this archive? All files will be deleted'
confirmButton='Delete'
onCancel={() => setDeleteOpen(false)}
onConfirm={localDeleteArchive}
/>
</>);
let alternativesList = <Loader active/>;
if (alternatives && alternatives.length > 0) {
alternativesList = <ArchivesList archives={alternatives}/>;
} else if (alternatives && alternatives.length === 0) {
alternativesList = <p>No alternatives available</p>;
}
let deleteIcon = null;
if (deleteArchive) {
deleteIcon = (<>
<Button icon onClick={() => setDeleteOpen(true)}>
<Icon name='trash' size='large'/>
</Button>
<Confirm
open={deleteOpen}
content='Are you sure you want to delete this Archive? All files will be deleted.'
confirmButton='Delete'
onCancel={() => setDeleteOpen(false)}
onConfirm={localDeleteArchive}
return (
<>
<Button icon='arrow left' content='Back' onClick={() => history.goBack()}/>
<Segment>
{screenshot}
<a href={singlefileUrl} target='_blank' className='no-link-underscore card-link'>
<Header as='h2'>{textEllipsis(archive.title || archive.url, 100)}</Header>
</a>
<Header as='h3'>{uploadDate(archive.archive_datetime)}</Header>
<p><a href={archive.url} target='_blank'>{textEllipsis(archive.url, 100)}</a></p>
{singlefileButton}
{readabilityLink}
{syncButton}
{deleteButton}
</Segment>
<Segment>
<HelpHeader
headerContent='Alternatives'
popupContent='Alternative archives are archives that have the same URL.'
/>
</>
);
}
{alternativesList}
</Segment>
</>
)
}
let externalIcon = (
<Button icon href={archive.url} target='_blank' rel='noopener noreferrer'>
<Icon name='sign-out' size='large'/>
</Button>
);
function ArchiveCard({archive, setDomain}) {
const imageSrc = archive.screenshot_path ? `/media/${encodeURIComponent(archive.screenshot_path)}` : null;
const singlefileUrl = archive.singlefile_path ? `/media/${encodeURIComponent(archive.singlefile_path)}` : null;
const domain = archive.domain.domain;
@ -98,36 +139,35 @@ function ArchiveCard({archive, syncArchive, deleteArchive, setDomain}) {
</a>
</Container>
</Card.Header>
<a onClick={() => setDomain(domain)} className="no-link-underscore card-link">
{textEllipsis(domain, 40)}
</a>
{setDomain &&
<a onClick={() => setDomain(domain)} className="no-link-underscore card-link">
{textEllipsis(domain, 40)}
</a>
}
<Card.Meta>
<p>
{uploadDate(archive.archive_datetime)}
</p>
</Card.Meta>
<Card.Description>
<Container textAlign='left'>
{readabilityIcon}
{syncIcon}
{deleteIcon}
{externalIcon}
</Container>
<Link to={`/archive/${archive.id}`}>
<Button icon='file alternate' content='Details'
labelPosition='left'/>
</Link>
<Button icon='external' href={archive.url} target='_blank' rel='noopener noreferrer'/>
</Card.Description>
</Card.Content>
</Card>
)
}
function ArchiveCards({archives, syncArchive, deleteArchive, setDomain}) {
function ArchiveCards({archives, setDomain}) {
return (
<CardGroupCentered>
{archives.map((i) => {
return <ArchiveCard
key={i['id']}
archive={i}
syncArchive={syncArchive}
deleteArchive={deleteArchive}
setDomain={setDomain}
/>
})}
@ -135,7 +175,7 @@ function ArchiveCards({archives, syncArchive, deleteArchive, setDomain}) {
)
}
export function ArchivesList({archives, searchStr, syncArchive, localDeleteArchive, setDomain}) {
export function ArchivesList({archives, searchStr, setDomain}) {
if (archives === null || archives === undefined) {
// Fetching archives.
return <ArchivePlaceholder/>;
@ -152,8 +192,6 @@ export function ArchivesList({archives, searchStr, syncArchive, localDeleteArchi
// Archives fetched successfully!
return <ArchiveCards
archives={archives}
syncArchive={syncArchive}
deleteArchive={localDeleteArchive}
setDomain={setDomain}
/>;
}
@ -163,16 +201,6 @@ export function Archives() {
const {archives, setPage, totalPages, searchStr, activePage, setSearchStr, domain, setDomain, search} =
useArchives(20);
const syncArchive = async (url) => {
await postDownload(url, 'archive');
await search();
}
const localDeleteArchive = async (archive_id) => {
await deleteArchive(archive_id);
await search();
}
let domainClearButton = null;
if (domain) {
domainClearButton = <ClearButton
@ -189,8 +217,6 @@ export function Archives() {
<ArchivesList
archives={archives}
searchStr={searchStr}
syncArchive={syncArchive}
localDeleteArchive={localDeleteArchive}
setDomain={setDomain}
/>
<div style={{marginTop: '3em', textAlign: 'center'}}>
@ -282,8 +308,11 @@ export function ArchiveRoute(props) {
];
return <PageContainer>
<TabLinks links={links}/>
<Route path='/archive' exact component={Archives}/>
<Route path='/archive/domains' component={Domains}/>
<Route path='/archive/manage' component={ManageArchives}/>
<Switch>
<Route path='/archive/domains' exact component={Domains}/>
<Route path='/archive/manage' exact component={ManageArchives}/>
<Route path='/archive/:archive_id' exact component={ArchivePage}/>
<Route path='/archive' exact component={Archives}/>
</Switch>
</PageContainer>
}

View file

@ -4,6 +4,7 @@ import {
Checkbox,
Container,
Form,
Header,
Icon,
Image,
Input,
@ -714,6 +715,17 @@ export function HelpPopup({icon, size, content, position}) {
/>
}
export function HelpHeader({icon, headerSize, iconSize, headerContent, popupContent}) {
return (
<div className='inline-header'>
<Header as={headerSize || 'h2'}>{headerContent}</Header>
<span>
<HelpPopup content={popupContent} size={iconSize} icon={icon}/>
</span>
</div>
)
}
export function minutesToTimestamp(minutes) {
minutes = Math.round(minutes);
const hours = String(Math.floor(minutes / 60)).padStart(1, '0');

View file

@ -3,6 +3,7 @@ import {
fetchDomains,
filesSearch,
getAPIStatus,
getArchive,
getDownloaders,
getFiles,
getHotspotStatus,
@ -152,6 +153,28 @@ export const useArchives = ({defaultLimit = 20}) => {
};
}
export const useArchive = (archiveId) => {
const [archive, setArchive] = useState(null);
const [alternatives, setAlternatives] = useState(null);
const fetchArchive = async () => {
try {
const [a, alt] = await getArchive(archiveId);
setArchive(a);
setAlternatives(alt);
} catch (e) {
console.error(e);
setArchive(undefined);
}
}
useEffect(() => {
fetchArchive();
}, [archiveId]);
return [archive, alternatives];
}
export const useVersion = () => {
const [version, setVersion] = useState('');

View file

@ -2,3 +2,27 @@ textarea.otp {
width: 100%;
color: #333;
}
.inline-header h1 {
display: inline-block;
}
.inline-header h2 {
display: inline-block;
}
.inline-header h3 {
display: inline-block;
}
.inline-header h4 {
display: inline-block;
}
.inline-header h5 {
display: inline-block;
}
.inline-header span {
margin-left: 1em;
}

View file

@ -30,8 +30,8 @@ class ArchiveDownloader(Downloader, ABC):
if download.attempts > 3:
raise UnrecoverableDownloadError(f'Max download attempts reached for {download.url}')
lib.do_archive(download.url)
return DownloadResult(success=True)
archive = lib.do_archive(download.url)
return DownloadResult(success=True, location=f'/archive/{archive.id}')
# Archive downloader is the last downloader which should be used.

View file

@ -18,6 +18,15 @@ bp = get_blueprint('Archive', '/api/archive')
logger = logger.getChild(__name__)
@bp.get('/<archive_id:int>')
@openapi.description('Get an archive')
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)
async def get_archive(_: Request, archive_id: int):
archive = lib.get_archive(archive_id=archive_id)
alternatives = archive.alternatives
return json_response({'archive': archive, 'alternatives': alternatives})
@bp.delete('/<archive_id:int>')
@openapi.description('Delete an individual archive')
@openapi.response(HTTPStatus.NOT_FOUND, JSONErrorResponse)

View file

@ -19,7 +19,7 @@ from modules.archive.models import Domain, Archive
from wrolpi.cmd import which
from wrolpi.common import get_media_directory, logger, chunks, extract_domain, chdir, escape_file_name, walk
from wrolpi.dates import now, Seconds, local_timezone
from wrolpi.db import get_db_session, get_db_curs, get_ranked_models
from wrolpi.db import get_db_session, get_db_curs, get_ranked_models, optional_session
from wrolpi.errors import InvalidDomain, UnknownURL, InvalidArchive
from wrolpi.vars import DOCKERIZED, PYTEST
@ -330,10 +330,17 @@ def get_domain(session, domain: str) -> Domain:
return domain_
@optional_session
def get_archive(session, archive_id: int) -> Archive:
"""Get an Archive."""
archive = session.query(Archive).filter_by(id=archive_id).one_or_none()
if not archive:
raise InvalidArchive(f'Invalid archive id: {archive_id}')
return archive
def delete_archive(archive_id: int):
"""
Delete an Archive and all of it's files.
"""
"""Delete an Archive and all of it's files."""
with get_db_session(commit=True) as session:
archive: Archive = session.query(Archive).filter_by(id=archive_id).one_or_none()
if not archive:

View file

@ -90,6 +90,18 @@ class Archive(Base, ModelHelper):
if not domain_archives:
self.domain.delete()
@property
def alternatives(self):
"""
Get a list of Archives that share my URL.
"""
session = Session.object_session(self)
i = list(session.query(Archive).filter(
Archive.id != self.id,
Archive.url == self.url,
).order_by(Archive.archive_datetime))
return i
class Domain(Base, ModelHelper):
__tablename__ = 'domains' # plural to avoid conflict

View file

@ -1,6 +1,7 @@
import json
import pathlib
import tempfile
import time
from datetime import timedelta
from http import HTTPStatus
@ -171,16 +172,22 @@ def test_channel_download_crud(test_session, simple_channel):
simple_channel.update({'url': 'https://example.com'})
test_session.commit()
# Adding a frequency adds a Download.
simple_channel.update({'download_frequency': 1})
test_session.commit()
assert test_session.query(Download).count() == 1
next_download = simple_channel.get_download().next_download
assert next_download
def test_delete_channel_delete_download(download_channel, test_session):
"""Deleting a Channel deletes it's Download."""
downloads = test_session.query(Download).all()
assert downloads and all(i.url == download_channel.url for i in downloads)
# Next download isn't overwritten.
time.sleep(1)
simple_channel.update({'name': 'new name'})
test_session.commit()
assert simple_channel.get_download().next_download == next_download
delete_channel(channel_id=download_channel.id)
downloads = test_session.query(Download).all()
assert not list(downloads)
# Deleting a Channel deletes it's Download.
delete_channel(channel_id=simple_channel.id)
assert test_session.query(Download).count() == 0
def test_video_search(test_client, test_session):

View file

@ -316,7 +316,8 @@ class Channel(ModelHelper, Base):
elif download and self.download_frequency:
download.frequency = self.download_frequency
download.url = self.url
download.next_download = download_manager.get_next_download(download, session)
# Keep next_download if available.
download.next_download = download.next_download or download_manager.get_next_download(download, session)
elif not download and self.download_frequency and self.url:
download = Download(frequency=self.download_frequency, url=self.url, downloader='video_channel')
session.add(download)