Archive tabs are handled by TabRouter.

This commit is contained in:
Roland 2022-01-29 21:24:39 -07:00
parent 73f2f7b47c
commit d17ad50d56
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
5 changed files with 107 additions and 73 deletions

View file

@ -1,6 +1,14 @@
import React, {useState} from "react";
import {Card, Confirm, Container, Icon, Image, Placeholder, Tab} from "semantic-ui-react";
import Paginator, {ClearButton, ExternalLink, SearchInput, textEllipsis, uploadDate, WROLModeMessage} from "./Common";
import Paginator, {
ClearButton,
ExternalLink,
SearchInput,
TabRouter,
textEllipsis,
uploadDate,
WROLModeMessage
} from "./Common";
import {deleteArchive, postDownload, refreshArchives} from "../api";
import Button from "semantic-ui-react/dist/commonjs/elements/Button";
import {Link, NavLink} from "react-router-dom";
@ -289,73 +297,41 @@ function Domains() {
}
export class ArchiveRoute extends React.Component {
constructor(props) {
super(props);
this.panesTos = ['/archive', '/archive/domains', '/archive/manage'];
this.state = {
activeIndex: this.matchPaneTo(),
}
this.archivePanes = [
{
menuItem: {
as: NavLink,
content: 'Archives',
id: 'archive',
to: '/archive',
exact: true,
key: 'home',
},
render: () => <Tab.Pane><Archives/></Tab.Pane>
export function ArchiveRoute(props) {
const panes = [
{
menuItem: {
as: NavLink,
content: 'Archives',
id: 'archive',
to: '/archive',
exact: true,
key: 'home',
},
{
menuItem: {
as: NavLink,
content: 'Domains',
id: 'domains',
to: '/archive/domains',
exact: true,
key: 'domains',
},
render: () => <Tab.Pane><Domains/></Tab.Pane>
render: () => <Tab.Pane><Archives/></Tab.Pane>
},
{
menuItem: {
as: NavLink,
content: 'Domains',
id: 'domains',
to: '/archive/domains',
exact: true,
key: 'domains',
},
{
menuItem: {
as: NavLink,
content: 'Manage',
id: 'manage',
to: '/archive/manage',
exact: true,
key: 'manage',
},
render: () => <Tab.Pane><ManageArchives/></Tab.Pane>
render: () => <Tab.Pane><Domains/></Tab.Pane>
},
{
menuItem: {
as: NavLink,
content: 'Manage',
id: 'manage',
to: '/archive/manage',
exact: true,
key: 'manage',
},
];
}
componentDidUpdate(prevProps, prevState, snapshot) {
if (prevProps.location.pathname !== this.props.location.pathname) {
this.setState({activeIndex: this.matchPaneTo()});
}
}
matchPaneTo = () => {
return this.panesTos.indexOf(this.props.location.pathname);
}
render() {
return (
<Container fluid>
<Tab
panes={this.archivePanes}
activeIndex={this.state.activeIndex}
renderActiveOnly={true}
/>
</Container>
)
}
render: () => <Tab.Pane><ManageArchives/></Tab.Pane>
},
];
return <TabRouter panes={panes} {...props} />
}

View file

@ -1,5 +1,5 @@
import React, {useEffect, useState} from "react";
import {Card, Container, Form, Icon, Image, Input, Pagination, Progress} from 'semantic-ui-react';
import {Card, Container, Form, Icon, Image, Input, Pagination, Progress, Tab} from 'semantic-ui-react';
import {Link} from "react-router-dom";
import Button from "semantic-ui-react/dist/commonjs/elements/Button";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
@ -635,3 +635,62 @@ export function textEllipsis(str, maxLength, {side = "end", ellipsis = "..."} =
}
return str;
}
export class TabRouter extends React.Component {
constructor(props) {
super(props);
const panes = props.panes;
let paneTos = [];
for (let i = 0; i < panes.length; i++) {
const pane = panes[i];
paneTos = paneTos.concat([pane['menuItem']['to']]);
}
this.state = {
activeIndex: this.matchPaneTo(paneTos),
paneTos: paneTos,
};
}
matchPaneTo = (paneTos) => {
const pathname = this.props.location.pathname;
// Attempt to find the exact match first.
let activeIndex = paneTos.indexOf(pathname);
if (activeIndex !== -1) {
return activeIndex;
}
// Use the first tab if we can't find one.
activeIndex = 0;
// Try to find the most-exact match.
for (let i = 0; i < paneTos.length; i++) {
let to = paneTos[i];
if (pathname.startsWith(to) && to.length > paneTos[activeIndex].length) {
// "to" is a better match because it is longer than the last one we found.
activeIndex = i;
}
}
return activeIndex;
}
componentDidUpdate(prevProps, prevState, snapshot) {
if (prevProps.location.pathname !== this.props.location.pathname) {
this.setState({activeIndex: this.matchPaneTo(this.state.paneTos)});
}
}
render() {
const {panes, renderActiveOnly} = this.props;
return (
<>
<Tab
panes={panes}
activeIndex={this.state.activeIndex}
renderActiveOnly={renderActiveOnly || true}
/>
</>
)
}
}

View file

@ -14,8 +14,8 @@ const links = [
]
},
{text: 'Inventory', to: '/inventory', exact: true},
{text: 'Archive', to: '/archive', exact: true},
{text: 'Files', to: '/files', exact: true},
{text: 'Archive', to: '/archive'},
{text: 'Files', to: '/files'},
{
text: 'Apps', links: [
{to: '/apps/otp', text: 'One Time Pad', exact: true},

View file

@ -14,6 +14,7 @@ bp = get_blueprint('Files', '/api/files')
def paths_to_files(paths: List[pathlib.Path]):
"""Convert Paths to what the React UI expects."""
media_directory = get_media_directory()
new_files = []
for path in paths:

View file

@ -51,9 +51,7 @@ def list_files(directories: List[str]) -> List[Path]:
@wrol_mode_check
def delete_file(file: str):
"""
Delete a file in the media directory.
"""
"""Delete a file in the media directory."""
file = get_media_directory() / file
if file.is_dir() or not file.is_file():
raise InvalidFile(f'Invalid file {file}')