mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 07:26:18 -05:00
FileBrowser files can be tagged. A Tag icon is displayed on each file row in the browser.
This commit is contained in:
parent
655e944960
commit
ae2b6daa33
29 changed files with 356 additions and 219 deletions
|
|
@ -2,11 +2,12 @@ import React, {useContext, useEffect, useState} from "react";
|
|||
import {Card, Container, IconGroup, Input, Modal, Pagination} from 'semantic-ui-react';
|
||||
import {Link, NavLink, useNavigate} from "react-router-dom";
|
||||
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
|
||||
import {useDirectories, useDownloaders, useHotspot, useSettings, useThrottle} from "../hooks/customHooks";
|
||||
import {useDirectories, useHotspot, useSettings, useThrottle} from "../hooks/customHooks";
|
||||
import {Media, StatusContext, ThemeContext} from "../contexts/contexts";
|
||||
import {Button, CardIcon, darkTheme, Form, Header, Icon, lightTheme, Menu, Popup, Statistic} from "./Theme";
|
||||
import {FilePreviewContext} from "./FilePreview";
|
||||
import _ from "lodash";
|
||||
import {killDownloads, startDownloads} from "../api";
|
||||
|
||||
export const API_URI = process.env && process.env.REACT_APP_API_URI ? process.env.REACT_APP_API_URI : `${window.location.protocol}//${window.location.host}/api`;
|
||||
export const VIDEOS_API = `${API_URI}/videos`;
|
||||
|
|
@ -20,16 +21,32 @@ export function Paginator({activePage, onPageChange, totalPages, showFirstAndLas
|
|||
onPageChange(activePage);
|
||||
}
|
||||
|
||||
return <Pagination
|
||||
activePage={activePage}
|
||||
boundaryRange={1}
|
||||
onPageChange={handlePageChange}
|
||||
size={size}
|
||||
siblingRange={2}
|
||||
totalPages={totalPages}
|
||||
firstItem={showFirstAndLast ? undefined : null}
|
||||
lastItem={showFirstAndLast ? undefined : null}
|
||||
/>
|
||||
return <>
|
||||
<Media at='mobile'>
|
||||
<Pagination
|
||||
activePage={activePage}
|
||||
boundaryRange={1}
|
||||
onPageChange={handlePageChange}
|
||||
size={size}
|
||||
siblingRange={2}
|
||||
totalPages={totalPages}
|
||||
firstItem={showFirstAndLast ? undefined : null}
|
||||
lastItem={showFirstAndLast ? undefined : null}
|
||||
/>
|
||||
</Media>
|
||||
<Media greaterThanOrEqual='tablet'>
|
||||
<Pagination
|
||||
activePage={activePage}
|
||||
boundaryRange={1}
|
||||
onPageChange={handlePageChange}
|
||||
size={size}
|
||||
siblingRange={5}
|
||||
totalPages={totalPages}
|
||||
firstItem={showFirstAndLast ? undefined : null}
|
||||
lastItem={showFirstAndLast ? undefined : null}
|
||||
/>
|
||||
</Media>
|
||||
</>
|
||||
}
|
||||
|
||||
export function divmod(x, y) {
|
||||
|
|
@ -616,7 +633,19 @@ export function Toggle({label, checked, disabled, onChange, icon}) {
|
|||
}
|
||||
|
||||
export function DisableDownloadsToggle() {
|
||||
let {on, setDownloads} = useDownloaders();
|
||||
const {status, fetchStatus} = React.useContext(StatusContext);
|
||||
const {downloads} = status;
|
||||
|
||||
const setDownloads = async (enable) => {
|
||||
if (enable) {
|
||||
await startDownloads();
|
||||
} else {
|
||||
await killDownloads();
|
||||
}
|
||||
await fetchStatus();
|
||||
}
|
||||
|
||||
const on = downloads && downloads['disabled'] === false;
|
||||
return <Form>
|
||||
<Toggle
|
||||
label={on ? 'Downloading Enabled' : 'Downloading Disabled'}
|
||||
|
|
|
|||
|
|
@ -1,58 +1,2 @@
|
|||
import React from "react";
|
||||
import {getByTestId, render, screen} from '@testing-library/react';
|
||||
import {DisableDownloadsToggle} from "./Common";
|
||||
import {useDownloaders} from "../hooks/customHooks";
|
||||
import user from '@testing-library/user-event';
|
||||
|
||||
jest.mock('../hooks/customHooks', () => ({useDownloaders: jest.fn()}));
|
||||
|
||||
test('DownloadsToggle is off when loading', async () => {
|
||||
useDownloaders.mockImplementation(() => ({on: null}));
|
||||
|
||||
const {container} = render(<DisableDownloadsToggle/>);
|
||||
let toggle = screen.getByRole('checkbox');
|
||||
expect(toggle).not.toBeChecked();
|
||||
// Disabled while loading.
|
||||
expect(toggle).toHaveClass('disabled');
|
||||
// Text is "disabled" when downloading is On.
|
||||
const label = getByTestId(container, 'toggle-label');
|
||||
expect(label).toHaveTextContent('Downloading Disabled');
|
||||
});
|
||||
|
||||
test('DownloadsToggle is off if downloading is Off', async () => {
|
||||
useDownloaders.mockImplementation(() => ({on: false}));
|
||||
|
||||
const {container} = render(<DisableDownloadsToggle/>);
|
||||
const toggle = screen.getByRole('checkbox');
|
||||
expect(toggle).not.toBeChecked();
|
||||
// Not disabled when loaded.
|
||||
expect(toggle).not.toHaveClass('disabled');
|
||||
// Text is "disabled" when downloading is On.
|
||||
const label = getByTestId(container, 'toggle-label');
|
||||
expect(label).toHaveTextContent('Downloading Disabled');
|
||||
});
|
||||
|
||||
test('DownloadsToggle is on if downloading is On', async () => {
|
||||
useDownloaders.mockImplementation(() => ({on: true}));
|
||||
|
||||
const {container} = render(<DisableDownloadsToggle/>);
|
||||
const toggle = screen.getByRole('checkbox');
|
||||
expect(toggle).toBeChecked();
|
||||
// Not disabled when loaded.
|
||||
expect(toggle).not.toHaveClass('disabled');
|
||||
// Text is "enabled" when downloading is On.
|
||||
const label = getByTestId(container, 'toggle-label');
|
||||
expect(label).toHaveTextContent('Downloading Enabled');
|
||||
});
|
||||
|
||||
test('DownloadsToggle click', async () => {
|
||||
const mock = jest.fn();
|
||||
useDownloaders.mockImplementation(() => ({on: true, setDownloads: mock}));
|
||||
render(<DisableDownloadsToggle/>);
|
||||
|
||||
// Downloads is toggled when DownloadsToggle is clicked.
|
||||
const toggle = screen.getByRole('checkbox');
|
||||
user.click(toggle);
|
||||
expect(mock).toHaveBeenCalled();
|
||||
expect(mock).toHaveBeenLastCalledWith(false);
|
||||
})
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import {Button, Icon, Placeholder} from "./Theme";
|
||||
import {Checkbox, Confirm, PlaceholderLine, TableCell, TableFooter, TableHeaderCell, TableRow} from "semantic-ui-react";
|
||||
import {FileIcon, humanFileSize} from "./Common";
|
||||
import React from "react";
|
||||
import {deleteFile} from "../api";
|
||||
import React, {useEffect} from "react";
|
||||
import {deleteFile, fetchFile} from "../api";
|
||||
import _ from 'lodash';
|
||||
import {SortableTable} from "./SortableTable";
|
||||
import {useBrowseFiles} from "../hooks/customHooks";
|
||||
import {DirectoryRefreshButton, FilesRefreshButton} from "./Files";
|
||||
import {DirectoryRefreshButton, FileRowTagIcon, FilesRefreshButton} from "./Files";
|
||||
import {FilePreviewContext} from "./FilePreview";
|
||||
|
||||
function depthIndentation(path) {
|
||||
|
|
@ -58,6 +58,7 @@ function File({file, onFileClick, selectedPath, onSelect}) {
|
|||
</TableCell>
|
||||
<TableCell onClick={() => onFileClick(file)} className='file-path'>
|
||||
{depthIndentation(path)}
|
||||
<FileRowTagIcon file={file}/>
|
||||
{/*null size to make icon the correct size*/}
|
||||
<FileIcon file={file} size={null}/>
|
||||
{name}
|
||||
|
|
@ -96,11 +97,20 @@ export function FileBrowser() {
|
|||
|
||||
const {browseFiles, openFolders, setOpenFolders, fetchFiles} = useBrowseFiles();
|
||||
|
||||
const headers = [{key: 'select', text: ''}, {
|
||||
key: 'path', text: 'Path', sortBy: i => i['path'].toLowerCase()
|
||||
}, {key: 'size', text: 'Size', sortBy: 'size'},];
|
||||
const headers = [
|
||||
{key: 'select', text: ''},
|
||||
{key: 'path', text: 'Path', sortBy: i => i['path'].toLowerCase()},
|
||||
{key: 'size', text: 'Size', sortBy: 'size'},
|
||||
];
|
||||
|
||||
const {setPreviewFile} = React.useContext(FilePreviewContext);
|
||||
const {setPreviewFile, setCallbacks} = React.useContext(FilePreviewContext);
|
||||
|
||||
useEffect(() => {
|
||||
// Fetch files after tagging.
|
||||
setCallbacks([fetchFiles]);
|
||||
|
||||
return () => setCallbacks(null);
|
||||
}, []);
|
||||
|
||||
const onSelect = (path) => {
|
||||
if (path.endsWith('/') && selectedPath === path) {
|
||||
|
|
|
|||
|
|
@ -10,9 +10,10 @@ import {
|
|||
ModalContent,
|
||||
ModalHeader
|
||||
} from "semantic-ui-react";
|
||||
import {TagsDisplay} from "../Tags";
|
||||
import {TagsDisplay, TagsProvider} from "../Tags";
|
||||
import {Media} from "../contexts/contexts";
|
||||
import {encodeMediaPath} from "./Common";
|
||||
import {fetchFile} from "../api";
|
||||
|
||||
function getMediaPathURL(previewFile) {
|
||||
if (previewFile['primary_path']) {
|
||||
|
|
@ -126,7 +127,11 @@ function getAudioModal(previewFile) {
|
|||
}
|
||||
|
||||
export const FilePreviewContext = React.createContext({
|
||||
previewFile: null, setPreviewFile: null, previewModal: null, setPreviewModal: null,
|
||||
previewFile: null,
|
||||
setPreviewFile: null,
|
||||
previewModal: null,
|
||||
setPreviewModal: null,
|
||||
setCallbacks: null,
|
||||
});
|
||||
|
||||
const MAXIMUM_TEXT_SIZE = 5_000_000;
|
||||
|
|
@ -134,15 +139,38 @@ const MAXIMUM_TEXT_SIZE = 5_000_000;
|
|||
export function FilePreviewWrapper({children}) {
|
||||
const [previewFile, setPreviewFile] = React.useState(null);
|
||||
const [previewModal, setPreviewModal] = React.useState(null);
|
||||
const [callbacks, setCallbacks] = React.useState(null);
|
||||
|
||||
const value = {
|
||||
previewFile, setPreviewFile, previewModal, setPreviewModal,
|
||||
const localFetchFile = async () => {
|
||||
// Get the file again with its Tags.
|
||||
const {path, primary_path} = previewFile;
|
||||
try {
|
||||
const file = await fetchFile(primary_path ?? path);
|
||||
setPreviewFile(file);
|
||||
} finally {
|
||||
await handleCallbacks();
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
setPreviewModal(null);
|
||||
setPreviewFile(null);
|
||||
}
|
||||
|
||||
const handleCallbacks = async () => {
|
||||
for (const callbacksKey in callbacks) {
|
||||
try {
|
||||
await callbacks[callbacksKey]();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setModalContent(content, url, downloadURL) {
|
||||
const openButton = <SButton color='blue' as='a' href={url}>Open</SButton>;
|
||||
const closeButton = <SButton onClick={() => setPreviewFile(null)}>Close</SButton>;
|
||||
const tagsDisplay = <TagsDisplay fileGroup={previewFile}/>;
|
||||
const closeButton = <SButton onClick={handleClose}>Close</SButton>;
|
||||
const tagsDisplay = <TagsDisplay fileGroup={previewFile} afterTag={localFetchFile}/>;
|
||||
let downloadButton;
|
||||
if (downloadURL) {
|
||||
downloadButton = <SButton color='yellow' as='a' href={downloadURL} floated='left'>
|
||||
|
|
@ -153,7 +181,7 @@ export function FilePreviewWrapper({children}) {
|
|||
setPreviewModal(<Modal closeIcon
|
||||
size='fullscreen'
|
||||
open={true}
|
||||
onClose={() => setPreviewModal(null)}
|
||||
onClose={handleClose}
|
||||
>
|
||||
{content}
|
||||
<ModalActions>
|
||||
|
|
@ -222,6 +250,8 @@ export function FilePreviewWrapper({children}) {
|
|||
}
|
||||
}, [previewFile]);
|
||||
|
||||
const value = {previewFile, setPreviewFile, previewModal, setPreviewModal, setCallbacks};
|
||||
|
||||
return <FilePreviewContext.Provider value={value}>
|
||||
{children}
|
||||
{previewModal}
|
||||
|
|
|
|||
|
|
@ -210,7 +210,7 @@ function VideoPage({videoFile, prevFile, nextFile, fetchVideo, ...props}) {
|
|||
</Segment>
|
||||
|
||||
<Segment>
|
||||
<TagsDisplay fileGroup={videoFile} onClick={fetchVideo}/>
|
||||
<TagsDisplay fileGroup={videoFile} afterTag={fetchVideo}/>
|
||||
</Segment>
|
||||
|
||||
<Tab menu={tabMenu} panes={tabPanes}/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue