2019-07-28 03:36:27 +00:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faFileImport } from '@fortawesome/free-solid-svg-icons/faFileImport';
|
|
|
|
import { faFileAlt } from '@fortawesome/free-solid-svg-icons/faFileAlt';
|
|
|
|
import { faFolder } from '@fortawesome/free-solid-svg-icons/faFolder';
|
|
|
|
import { bytesToHuman } from '@/helpers';
|
|
|
|
import differenceInHours from 'date-fns/difference_in_hours';
|
|
|
|
import format from 'date-fns/format';
|
|
|
|
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
|
|
|
|
import React from 'react';
|
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
2019-07-30 05:10:45 +00:00
|
|
|
import FileDropdownMenu from '@/components/server/files/FileDropdownMenu';
|
2019-08-04 21:58:31 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2019-09-28 21:59:05 +00:00
|
|
|
import { NavLink } from 'react-router-dom';
|
|
|
|
import useRouter from 'use-react-router';
|
2019-08-04 21:58:31 +00:00
|
|
|
|
|
|
|
export default ({ file }: { file: FileObject }) => {
|
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2019-08-06 04:52:48 +00:00
|
|
|
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
|
2019-09-28 21:59:05 +00:00
|
|
|
const { match } = useRouter();
|
2019-07-28 03:36:27 +00:00
|
|
|
|
|
|
|
return (
|
2019-08-06 04:18:32 +00:00
|
|
|
<div
|
2019-07-28 03:36:27 +00:00
|
|
|
key={file.name}
|
|
|
|
className={`
|
2019-08-06 04:18:32 +00:00
|
|
|
flex bg-neutral-700 rounded-sm mb-px text-sm
|
2019-07-28 03:36:27 +00:00
|
|
|
hover:text-neutral-100 cursor-pointer items-center no-underline hover:bg-neutral-600
|
|
|
|
`}
|
|
|
|
>
|
2019-09-28 21:59:05 +00:00
|
|
|
<NavLink
|
|
|
|
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${directory}/${file.name}`}
|
2019-08-06 04:54:16 +00:00
|
|
|
className={'flex flex-1 text-neutral-300 no-underline p-3'}
|
2019-08-06 04:18:32 +00:00
|
|
|
onClick={e => {
|
2019-08-06 04:52:48 +00:00
|
|
|
// Don't rely on the onClick to work with the generated URL. Because of the way this
|
|
|
|
// component re-renders you'll get redirected into a nested directory structure since
|
|
|
|
// it'll cause the directory variable to update right away when you click.
|
|
|
|
//
|
|
|
|
// Just trust me future me, leave this be.
|
|
|
|
if (!file.isFile) {
|
2019-09-28 21:59:05 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2019-08-06 04:52:48 +00:00
|
|
|
window.location.hash = `#${directory}/${file.name}`;
|
|
|
|
setDirectory(`${directory}/${file.name}`);
|
|
|
|
}
|
2019-08-06 04:18:32 +00:00
|
|
|
}}
|
2019-07-28 03:36:27 +00:00
|
|
|
>
|
2019-08-06 04:18:32 +00:00
|
|
|
<div className={'flex-none text-neutral-400 mr-4 text-lg pl-3'}>
|
|
|
|
{file.isFile ?
|
|
|
|
<FontAwesomeIcon icon={file.isSymlink ? faFileImport : faFileAlt}/>
|
|
|
|
:
|
|
|
|
<FontAwesomeIcon icon={faFolder}/>
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
<div className={'flex-1'}>
|
|
|
|
{file.name}
|
|
|
|
</div>
|
|
|
|
{file.isFile &&
|
|
|
|
<div className={'w-1/6 text-right mr-4'}>
|
|
|
|
{bytesToHuman(file.size)}
|
|
|
|
</div>
|
2019-07-28 03:36:27 +00:00
|
|
|
}
|
2019-08-06 04:18:32 +00:00
|
|
|
<div
|
|
|
|
className={'w-1/5 text-right mr-4'}
|
|
|
|
title={file.modifiedAt.toString()}
|
|
|
|
>
|
|
|
|
{Math.abs(differenceInHours(file.modifiedAt, new Date())) > 48 ?
|
|
|
|
format(file.modifiedAt, 'MMM Do, YYYY h:mma')
|
|
|
|
:
|
2019-08-06 05:22:31 +00:00
|
|
|
distanceInWordsToNow(file.modifiedAt, { addSuffix: true })
|
2019-08-06 04:18:32 +00:00
|
|
|
}
|
|
|
|
</div>
|
2019-09-28 21:59:05 +00:00
|
|
|
</NavLink>
|
2019-08-04 21:58:31 +00:00
|
|
|
<FileDropdownMenu uuid={file.uuid}/>
|
2019-08-06 04:18:32 +00:00
|
|
|
</div>
|
2019-07-28 03:36:27 +00:00
|
|
|
);
|
|
|
|
};
|