import React, {useContext, useState} from "react";
import {
CardContent,
CardDescription,
CardHeader,
CardMeta,
Container,
Dropdown,
Image,
Input,
PlaceholderHeader,
PlaceholderLine,
TableCell,
TableRow
} from "semantic-ui-react";
import {
APIButton,
BackButton,
CardLink,
cardTitleWrapper,
encodeMediaPath,
ExternalCardLink,
FileIcon,
findPosterPath,
HelpHeader,
humanFileSize,
isoDatetimeToString,
mimetypeColor,
PageContainer,
PreviewLink,
PreviewPath,
SearchInput,
SortButton,
TabLinks,
textEllipsis,
useTitle
} from "./Common";
import {deleteArchives, postDownload, tagFileGroup, untagFileGroup} from "../api";
import {Link, Route, Routes, useNavigate, useParams} from "react-router-dom";
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
import {useArchive, useDomains, useSearchArchives, useSearchDomain} from "../hooks/customHooks";
import {FileCards, FileRowTagIcon, FilesView} from "./Files";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import _ from "lodash";
import {Media, ThemeContext} from "../contexts/contexts";
import {Button, Card, CardIcon, darkTheme, Header, Loader, Placeholder, Segment, Tab, TabPane} from "./Theme";
import {SortableTable} from "./SortableTable";
import {taggedImageLabel, TagsSelector} from "../Tags";
import {toast} from "react-semantic-toasts-2";
function archiveFileLink(path, directory = false) {
if (path) {
const href = directory ?
`/media/${encodeMediaPath(path)}/`
: `/media/${encodeMediaPath(path)}`;
return
{path}
} else {
return
Unknown
}
}
function ArchivePage() {
const navigate = useNavigate();
const {archiveId} = useParams();
const {archiveFile, history, fetchArchive} = useArchive(archiveId);
const {theme} = useContext(ThemeContext);
let title;
if (archiveFile && archiveFile.archive) {
title = archiveFile.archive.title;
}
useTitle(title);
if (archiveFile === null) {
return ;
}
if (archiveFile === undefined) {
return <>
This archive does not exist
>
}
const {data, size} = archiveFile;
const singlefileUrl = data.singlefile_path ? `/media/${encodeMediaPath(data.singlefile_path)}` : null;
const screenshotUrl = data.screenshot_path ? `/media/${encodeMediaPath(data.screenshot_path)}` : null;
const singlefileButton =
;
const screenshot = screenshotUrl ?
:
null;
const localDeleteArchive = async () => {
await deleteArchives([data.id]);
toast({
type: 'success',
title: 'Archive Deleted',
description: 'Archive was successfully deleted.',
time: 2000,
})
navigate(-1);
}
const localUpdateArchive = async () => {
await postDownload(data.url, 'archive');
toast({
type: 'success',
title: 'Archive Downloading',
description: 'Archive update has been scheduled.',
time: 2000,
})
}
const updateButton =
Update
;
const deleteButton =
Delete
;
let historyList = ;
if (history && history.length === 0) {
historyList = No history available
;
} else if (history) {
historyList = ;
}
const domain = data.domain ? data.domain : null;
let domainHeader = Unknown
;
if (domain) {
const domainUrl = `/archive?domain=${domain}`;
domainHeader = ;
}
const localAddTag = async (name) => {
await tagFileGroup(archiveFile, name);
await fetchArchive();
}
const localRemoveTag = async (name) => {
await untagFileGroup(archiveFile, name);
await fetchArchive();
}
const aboutPane = {
menuItem: 'About', render: () =>
{domainHeader}
{humanFileSize(size)}
{data.url ? {data.url} : 'N/A'}
};
const localPreviewPath = (path, mimetype) => {
if (path) {
return {path}
} else {
return 'Unknown'
}
}
const filesPane = {
menuItem: 'Files', render: () =>
{localPreviewPath(data.singlefile_path, 'text/html')}
{localPreviewPath(data.readability_path, 'text/html')}
{localPreviewPath(data.readability_txt_path, 'text/plain')}
{localPreviewPath(data.readability_json_path, 'application/json')}
{data.screenshot_path ?
archiveFileLink(data.screenshot_path)
: 'Unknown'}
{archiveFileLink(archiveFile.directory, true)}
};
const tabPanes = [aboutPane, filesPane];
const tabMenu = theme === darkTheme ? {inverted: true, attached: true} : {attached: true};
return <>
{screenshot}
{textEllipsis(archiveFile.title || data.url)}
{isoDatetimeToString(data.archive_datetime)}
{singlefileButton}
{updateButton}
{deleteButton}
{historyList}
>
}
export function ArchiveCard({file}) {
const {s} = useContext(ThemeContext);
const {data} = file;
const imageSrc = data.screenshot_path ? `/media/${encodeMediaPath(data.screenshot_path)}` : null;
const singlefileUrl = data.singlefile_path ? `/media/${encodeMediaPath(data.singlefile_path)}` : null;
let screenshot = ;
const imageLabel = file.tags && file.tags.length ? taggedImageLabel : null;
if (imageSrc) {
screenshot = ;
}
const domain = data ? data.domain : null;
const domainUrl = `/archive?domain=${domain}`;
return
{screenshot}
{cardTitleWrapper(file.title || data.url)}
{domain &&
{domain}
}
{isoDatetimeToString(data.archive_datetime)}
}
export function DomainsPage() {
useTitle('Archive Domains');
const [domains] = useDomains();
const [searchStr, setSearchStr] = useState('');
if (!domains) {
return <>
>;
} else if (_.isEmpty(domains)) {
return
No domains yet.
Archive some webpages!
;
}
let filteredDomains = domains;
if (searchStr) {
const re = new RegExp(_.escapeRegExp(searchStr), 'i');
filteredDomains = domains.filter(i => re.test(i['domain']));
}
const domainRow = ({domain, url_count, size}) => {
return
{domain}
{url_count}
{humanFileSize(size)}
}
const headers = [
{key: 'domain', text: 'Domain', sortBy: 'domain', width: 12},
{key: 'archives', text: 'Archives', sortBy: 'url_count', width: 2},
{key: 'Size', text: 'Size', sortBy: 'size', width: 2},
];
return <>
setSearchStr(value)}
/>
domainRow(i)}
rowKey='domain'
tableHeaders={headers}
/>
>;
}
export function SearchDomain() {
// A Dropdown which allows the user to filter by Archive domains.
const {domain, domains, setDomain} = useSearchDomain();
const handleChange = (e, {value}) => {
setDomain(value);
}
if (domains && domains.length > 0) {
const domainOptions = domains.map(i => {
return {key: i['domain'], value: i['domain'], text: i['domain']}
});
return <>
>
}
return <>>
}
function ArchivesPage() {
const [selectedArchives, setSelectedArchives] = useState([]);
const [deleteOpen, setDeleteOpen] = useState(false);
useTitle('Archives');
const {
archives,
totalPages,
activePage,
setPage,
searchStr,
setSearchStr,
fetchArchives,
} = useSearchArchives();
let archiveOrders = [
{value: 'date', text: 'Date'},
{value: 'size', text: 'Size'},
];
if (searchStr) {
archiveOrders = [{value: 'rank', text: 'Rank'}, ...archiveOrders];
}
const onSelect = (path, checked) => {
if (checked && path) {
setSelectedArchives([...selectedArchives, path]);
} else if (path) {
setSelectedArchives(selectedArchives.filter(i => i !== path));
}
}
const onDelete = async () => {
setDeleteOpen(false);
const archiveIds = archives.filter(i => selectedArchives.indexOf(i['primary_path']) >= 0)
.map(i => i['data']['id']);
await deleteArchives(archiveIds);
await fetchArchives();
setSelectedArchives([]);
}
const invertSelection = async () => {
const newSelectedArchives = archives.map(archive => archive['key']).filter(i => selectedArchives.indexOf(i) < 0);
setSelectedArchives(newSelectedArchives);
}
const clearSelection = async (e) => {
if (e) e.preventDefault();
setSelectedArchives([]);
}
const selectElm = ;
const {body, paginator, selectButton, viewButton, limitDropdown, tagQuerySelector} = FilesView(
archives,
activePage,
totalPages,
selectElm,
selectedArchives,
onSelect,
setPage,
!!searchStr,
);
const searchInput = ;
return <>
{selectButton}
{viewButton}
{limitDropdown}
{tagQuerySelector}
{searchInput}
{selectButton}
{viewButton}
{limitDropdown}
{tagQuerySelector}
{searchInput}
{body}
{paginator}
>
}
export function ArchiveRowCells({file}) {
const {data} = file;
const archiveUrl = `/archive/${data.id}`;
const posterPath = findPosterPath(file);
const posterUrl = posterPath ? `/media/${encodeMediaPath(posterPath)}` : null;
let poster;
if (posterUrl) {
poster =
;
} else {
poster = ;
}
// Fragment for SelectableRow
return
{poster}
{textEllipsis(file.title || file.stem)}
}
export function ArchiveRoute() {
const links = [
{text: 'Archives', to: '/archive', end: true},
{text: 'Domains', to: '/archive/domains'},
];
return
}/>
}/>
}/>
}