import {Button, Header, Icon, Modal, ModalActions, ModalContent, ModalHeader, Placeholder, Table} from "./Theme"; import { Checkbox, IconGroup, Input, PlaceholderLine, TableBody, TableCell, TableFooter, TableHeader, TableHeaderCell, TableRow } from "semantic-ui-react"; import {APIButton, DirectorySearch, ErrorMessage, FileIcon, humanFileSize} from "./Common"; import React, {useEffect, useState} from "react"; import {deleteFile, makeDirectory, movePaths, renamePath} from "../api"; import _ from 'lodash'; import {SortableTable} from "./SortableTable"; import {useBrowseFiles, useMediaDirectory, useWROLMode} from "../hooks/customHooks"; import {DirectoryRefreshButton, FileRowTagIcon, FilesRefreshButton} from "./Files"; import {FilePreviewContext} from "./FilePreview"; function depthIndentation(path) { // Repeated spaces for every folder a path is in. const depth = (path.match(/\//g) || []).length; return '\xa0\xa0\xa0\xa0'.repeat(depth); } export function pathDirectory(path) { // Return the full path of the path's directory. // pathDirectory('/some/directory/file.txt') -> '/some/directory' return path.substring(0, path.lastIndexOf('/')); } export function pathName(path) { // Return the file's name. // pathName('/some/directory/file.txt') -> 'file.txt' // pathName('/some/directory') -> 'directory' if (path.endsWith('/')) { // Path is a directory, return its name. path = path.substring(0, path.length - 1); } return path.substring(path.lastIndexOf('/') + 1); } export function splitPathParentAndName(path) { if (path.endsWith('/')) { // Path is a directory, return its name. path = path.substring(0, path.length - 1); } const slashIndex = path.lastIndexOf('/'); return [path.substring(0, slashIndex), path.substring(slashIndex + 1, path.length)]; } function Folder({folder, onFolderClick, sortData, selectedPaths, onFileClick, onSelect, disabled}) { // Creates a single table row for a folder, or a row for itself and indented rows for its children. let {path, children, is_empty, size} = folder; const pathWithNoTrailingSlash = path.substring(0, path.length - 1); const name = path.substring(pathWithNoTrailingSlash.lastIndexOf('/') + 1); const f = = 0} onChange={() => onSelect(folder['path'])}/> onFolderClick(path)} className='file-path' colSpan={2} disabled={is_empty}> {depthIndentation(pathWithNoTrailingSlash)} {is_empty ? : } {name} ; if (children) { // Folder has children, recursively display them. children = sortData(children); let childPaths = []; _.forEach(children, (p, k) => { childPaths = [...childPaths, ]; }); return {f} {childPaths} } else { // Folder has no children. return f; } } function File({file, onFileClick, selectedPaths, onSelect, disabled}) { const {path, size} = file; return = 0} onChange={() => onSelect(path)}/> onFileClick(file)} className='file-path'> {depthIndentation(path)} {/*null size to make icon the correct size*/} {pathName(path)} {humanFileSize(size)} } function Path({path, onFolderClick, onFileClick, sortData, selectedPaths, onSelect, disabled}) { if (path['path'].endsWith('/')) { return } else { return } } export function FileBrowser() { const [selectedPaths, setSelectedPaths] = React.useState([]); const [renameOpen, setRenameOpen] = React.useState(false); const [moveOpen, setMoveOpen] = React.useState(false); const [makeDirectoryOpen, setMakeDirectoryOpen] = React.useState(false); const [pending, setPending] = React.useState(false); const selectedPathsCount = selectedPaths ? selectedPaths.length : 0; const {browseFiles, openFolders, setOpenFolders, fetchFiles} = useBrowseFiles(); const wrolModeEnabled = useWROLMode(); const headers = [ {key: 'select', text: ''}, {key: 'path', text: 'Path', sortBy: i => i['path'].toLowerCase()}, {key: 'size', text: 'Size', sortBy: 'size'}, ]; const {setPreviewFile, setCallbacks} = React.useContext(FilePreviewContext); useEffect(() => { // Fetch files after tagging. setCallbacks([() => fetchFiles(openFolders)]); return () => setCallbacks(null); }, []); const onSelect = (path) => { let newSelectedPaths; // Select or deselect if the paths was previously selected. if (selectedPathsCount && selectedPaths.indexOf(path) >= 0) { newSelectedPaths = selectedPaths.filter(i => i !== path); } else { newSelectedPaths = [...selectedPaths, path]; } setSelectedPaths(newSelectedPaths); console.debug('selectedPaths', newSelectedPaths); } const onDelete = async () => { await deleteFile(selectedPaths); await reset(); }; const handleMakeDirectory = async () => { setMakeDirectoryOpen(false); await reset(); } const reset = async () => { setSelectedPaths([]); setPending(true); try { await fetchFiles(); } finally { setPending(false); } } const footer = {selectedPathsCount ? : } setMakeDirectoryOpen(false)} parent={selectedPaths.length ? selectedPaths[0] : null} onSubmit={handleMakeDirectory} disabled={wrolModeEnabled} /> ; const onFolderClick = async (folder) => { let newFolders; if (openFolders.indexOf(folder) >= 0) { // Remove the folder that was clicked on, as well as its sub-folders. newFolders = openFolders.filter(i => !i.startsWith(folder)); } else { // Add the new folder to the opened folders. newFolders = [...openFolders, folder]; } console.debug(`newFolders=${newFolders}`); setOpenFolders(newFolders); } if (browseFiles === null) { return ; } else if (browseFiles === undefined) { return Could not fetch files } return <> setPreviewFile(i)} sortData={sortData} selectedPaths={selectedPaths} onSelect={onSelect} disabled={pending} />} footer={footer} /> } export function RenameModal({open, onClose, path, onSubmit, onPending}) { const [parent, name] = splitPathParentAndName(path); const [value, setValue] = React.useState(name); const disabled = value === name; const handleInputChange = (e, props) => { if (e) { e.preventDefault(); } setValue(props.value); } const handleSubmit = async (e) => { if (e) { e.preventDefault(); } try { console.log(`Renaming: ${path} to ${value}`); onPending(true); await renamePath(path, value); } finally { onClose(); onPending(false); if (onSubmit) { await onSubmit(); } } } useEffect(() => { setValue(pathName(path)); }, [path]); return Rename: {name}
New Name
{parent ? parent + '/' : null}{value}
} export function MoveModal({open, paths, onClose, onSubmit}) { const [destination, setDestination] = React.useState(''); const [pending, setPending] = React.useState(false); const handleDirectory = (value) => { setDestination(value); } const handleMove = async () => { try { setPending(true); await movePaths(destination, paths); onClose(); } catch (e) { console.error(e); setPending(false); } if (onSubmit) { await onSubmit(); } } const fileCell = i =>
{i}
; const destinationRows = paths.map(i => {fileCell(i)} {destination ? fileCell(destination + '/' + pathName(i)) : fileCell(pathName(i))} ); return Move Files/Directories

Search for a directory to move your files into:

Source Destination {destinationRows}
} export function MakeDirectoryModal({open, onClose, parent, onSubmit}) { const [value, setValue] = useState(''); const mediaDirectory = useMediaDirectory(); const handleInputChange = (e, props) => { if (e) { e.preventDefault(); } setValue(props.value); } const handleSubmit = async (e) => { if (e) { e.preventDefault(); } try { const path = parent ? `${parent}/${value}` : value; await makeDirectory(path); setValue(''); if (onSubmit) { await onSubmit(); } } catch (e) { console.error(e); } } return Make Directory
{mediaDirectory}/{parent}{value}
}