|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import FlashMessageRender from '@/components/FlashMessageRender'; |
| 3 | +import { ServerContext } from '@/state/server'; |
| 4 | +import loadDirectory, { FileObject } from '@/api/server/files/loadDirectory'; |
| 5 | +import { Actions, useStoreActions } from 'easy-peasy'; |
| 6 | +import { ApplicationStore } from '@/state'; |
| 7 | +import { httpErrorToHuman } from '@/api/http'; |
| 8 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 9 | +import { faFolder } from '@fortawesome/free-solid-svg-icons/faFolder'; |
| 10 | +import { faFileAlt } from '@fortawesome/free-solid-svg-icons/faFileAlt'; |
| 11 | +import format from 'date-fns/format'; |
| 12 | +import differenceInHours from 'date-fns/difference_in_hours'; |
| 13 | +import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'; |
| 14 | +import { bytesToHuman } from '@/helpers'; |
| 15 | +import { CSSTransition } from 'react-transition-group'; |
| 16 | +import { Link } from 'react-router-dom'; |
| 17 | +import Spinner from '@/components/elements/Spinner'; |
| 18 | + |
| 19 | +export default () => { |
| 20 | + const [ loading, setLoading ] = useState(true); |
| 21 | + const [ files, setFiles ] = useState<FileObject[]>([]); |
| 22 | + const server = ServerContext.useStoreState(state => state.server.data!); |
| 23 | + const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); |
| 24 | + |
| 25 | + const currentDirectory = window.location.hash.replace(/^#(\/)+/, '/'); |
| 26 | + |
| 27 | + const load = () => { |
| 28 | + setLoading(true); |
| 29 | + clearFlashes(); |
| 30 | + loadDirectory(server.uuid, currentDirectory) |
| 31 | + .then(files => { |
| 32 | + setFiles(files); |
| 33 | + setLoading(false); |
| 34 | + }) |
| 35 | + .catch(error => { |
| 36 | + if (error.response && error.response.status === 404) { |
| 37 | + window.location.hash = '#/'; |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + console.error(error.message, { error }); |
| 42 | + addError({ message: httpErrorToHuman(error), key: 'files' }); |
| 43 | + }); |
| 44 | + }; |
| 45 | + |
| 46 | + const breadcrumbs = (): { name: string; path?: string }[] => currentDirectory.split('/') |
| 47 | + .filter(directory => !!directory) |
| 48 | + .map((directory, index, dirs) => { |
| 49 | + if (index === dirs.length - 1) { |
| 50 | + return { name: directory }; |
| 51 | + } |
| 52 | + |
| 53 | + return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` }; |
| 54 | + }); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + load(); |
| 58 | + }, [ window.location.hash ]); |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className={'my-10 mb-6'}> |
| 62 | + <FlashMessageRender byKey={'files'}/> |
| 63 | + <React.Fragment> |
| 64 | + <div className={'flex items-center text-sm mb-4 text-neutral-500'}> |
| 65 | + /<span className={'px-1 text-neutral-300'}>home</span>/ |
| 66 | + <Link to={'#'} className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}> |
| 67 | + container |
| 68 | + </Link>/ |
| 69 | + { |
| 70 | + breadcrumbs().map((crumb, index) => ( |
| 71 | + crumb.path ? |
| 72 | + <React.Fragment key={index}> |
| 73 | + <Link |
| 74 | + to={`#${crumb.path}`} |
| 75 | + className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'} |
| 76 | + > |
| 77 | + {crumb.name} |
| 78 | + </Link>/ |
| 79 | + </React.Fragment> |
| 80 | + : |
| 81 | + <span key={index} className={'px-1 text-neutral-300'}>{crumb.name}</span> |
| 82 | + )) |
| 83 | + } |
| 84 | + </div> |
| 85 | + { |
| 86 | + loading ? |
| 87 | + <Spinner large={true} centered={true}/> |
| 88 | + : |
| 89 | + !files.length ? |
| 90 | + <p className={'text-sm text-neutral-600 text-center'}> |
| 91 | + This directory seems to be empty. |
| 92 | + </p> |
| 93 | + : |
| 94 | + <CSSTransition classNames={'fade'} timeout={250} appear={true} in={true}> |
| 95 | + <div> |
| 96 | + { |
| 97 | + files.map(file => ( |
| 98 | + <a |
| 99 | + key={file.name} |
| 100 | + href={`#${currentDirectory}/${file.name}`} |
| 101 | + className={` |
| 102 | + flex px-4 py-3 bg-neutral-700 text-neutral-300 rounded-sm mb-px text-sm |
| 103 | + border border-transparent hover:text-neutral-100 hover:border-neutral-600 |
| 104 | + cursor-pointer items-center no-underline |
| 105 | + `} |
| 106 | + > |
| 107 | + <div className={'flex-none text-neutral-500 mr-4 text-lg'}> |
| 108 | + {file.isFile ? |
| 109 | + <FontAwesomeIcon icon={faFileAlt}/> |
| 110 | + : |
| 111 | + <FontAwesomeIcon icon={faFolder}/> |
| 112 | + } |
| 113 | + </div> |
| 114 | + <div className={'flex-1'}> |
| 115 | + {file.name} |
| 116 | + </div> |
| 117 | + {file.isFile && |
| 118 | + <div className={'w-1/6 text-right mr-4'}> |
| 119 | + {bytesToHuman(file.size)} |
| 120 | + </div> |
| 121 | + } |
| 122 | + <div |
| 123 | + className={'w-1/5 text-right'} |
| 124 | + title={file.modifiedAt.toString()} |
| 125 | + > |
| 126 | + {Math.abs(differenceInHours(file.modifiedAt, new Date())) > 48 ? |
| 127 | + format(file.modifiedAt, 'MMM Do, YYYY h:mma') |
| 128 | + : |
| 129 | + distanceInWordsToNow(file.modifiedAt, { includeSeconds: true }) |
| 130 | + } |
| 131 | + </div> |
| 132 | + </a> |
| 133 | + )) |
| 134 | + } |
| 135 | + </div> |
| 136 | + </CSSTransition> |
| 137 | + } |
| 138 | + </React.Fragment> |
| 139 | + </div> |
| 140 | + ); |
| 141 | +}; |
0 commit comments